Neovim and Dockerized Intelephense

Goal

Set up Neovim to use a Dockerized version of Intelephense for PHP development.

Prerequisites

  • Neovim >= 0.11
  • neovim/nvim-lspconfig
  • Docker
  • Docker Compose

Creating a Docker Image for Development

FROM wordpress:latest

RUN apt-get update && apt-get install -y \
    curl \
    git \
    nodejs \
    npm \
    socat \
    unzip \
    && rm -rf /var/lib/apt/lists/*

RUN curl -sSLo /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
    && chmod +x /usr/local/bin/wp \
    && groupmod -g1000 www-data \
    && usermod -g1000 -u1000 www-data \
    && npm install -g intelephense

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

âš ī¸ TODO:

  • parametrize UID / GID on Docker build
  • pin versions for all tools
  • add more dev tools (xdebug, …)

Docker Compose file

services:
  # other services...

  intelephense:
    build:
      context: .
    image: local-wp-s3-offloader          # 👇 Prevents "ESRCH" kill signal error
    command: [ "intelephense", "--stdio", "--clientProcessId", "1" ]
    restart: on-failure
    stdin_open: true
    profiles:
      - tools
    environment:
      - NODE_OPTIONS=--max-old-space-size=8192
    user: www-data
    # Makes internal paths match host paths for LSP accuracy
    working_dir: ${PWD}
    volumes:
      - ./:${PWD}

â„šī¸ This service does not run with docker-compose up by default due to the tools profile.

Neovim LSP configuration

return {
    intelephense = {
        config = {
            cmd = { "docker-compose", "run", "--rm", "-T", "-i", "intelephense" },

            filetypes = { "php" },
            settings = {
                intelephense = {
                    -- stylua: ignore
                    stubs = {
                        "wordpress", "wordpress-globals", "wp-cli",
                        "woocommerce", "apache", "bcmath", "bz2",
                        "calendar", "Core", "curl", "date", "dom",
                        "filter", "fileinfo", "gd", "gettext", "hash",
                        "iconv", "imap", "intl", "json", "libxml",
                        "mbstring", "mcrypt", "mysqli", "openssl",
                        "pcre", "pdo", "pdo_mysql", "phar", "reflection",
                        "session", "simplexml", "soap", "sockets",
                        "sodium", "spl", "standard", "tokenizer", "xml",
                        "xmlreader", "xmlwriter", "xsl", "zip", "zlib",
                    },
                    -- stylua: ignore
                },
            },
        },
    },
}

Change WordPress uploads URL

Changes the URL address WordPress renders for the user-uploaded files. The browser will fetch the files from the new address instead of fetching from the WordPress server. [Read More]