2015-12-02 14:23:39 +00:00
#+TITLE : PHP layer
2016-04-13 03:31:38 +00:00
2019-05-07 08:53:56 +00:00
#+TAGS : general|layer|multi-paradigm|programming
2019-05-05 17:26:40 +00:00
2015-06-10 16:44:30 +00:00
[[file:img/php.png ]]
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description ][Description ]]
- [[#features ][Features: ]]
- [[#install ][Install ]]
2019-01-02 21:34:56 +00:00
- [[#layer ][Layer ]]
2019-09-17 17:10:26 +00:00
- [[#backends ][Backends ]]
2020-04-18 11:26:47 +00:00
- [[#lsp ][LSP ]]
- [[#intelephense ][intelephense ]]
- [[#php-language-server ][php-language-server ]]
- [[#debugging ][Debugging ]]
2019-09-17 17:10:26 +00:00
- [[#ac-php-core ][ac-php-core ]]
- [[#setup ][Setup ]]
2019-12-06 18:59:15 +00:00
- [[#refactoring ][Refactoring ]]
2020-04-18 11:26:47 +00:00
- [[#debugging-1 ][Debugging ]]
2017-05-22 14:16:12 +00:00
- [[#key-bindings ][Key bindings ]]
2018-12-06 19:10:05 +00:00
- [[#general ][General ]]
2019-12-06 18:59:15 +00:00
- [[#refactoring-for-non-lsp-backends ][Refactoring for non LSP backends ]]
- [[#debugging-for-non-lsp-backends ][Debugging for non LSP backends ]]
2019-12-09 21:44:48 +00:00
- [[#lsp-key-bindings ][LSP key bindings ]]
2020-04-18 11:26:47 +00:00
- [[#debugging-for-lsp-backends ][Debugging for LSP backends ]]
2015-06-10 16:44:30 +00:00
* Description
This layer adds PHP language support to Spacemacs.
2015-06-10 21:16:01 +00:00
** Features:
2015-06-10 16:44:30 +00:00
- Edit PHP files using [[https://github.com/ejmr/php-mode ][php-mode ]]
- Edit Drupal files
2017-05-24 05:18:41 +00:00
- Complete and jump to define with [[https://github.com/xcwen/ac-php ][company-php ]]
2015-06-10 16:44:30 +00:00
- Run tests with PHPUnit
- Reformat code with PHP CBF
2020-04-18 11:26:47 +00:00
- Debug your programs with XDebug (via [[https://github.com/ahungry/geben ][geben ]] or [[https://github.com/emacs-lsp/dap-mode ][dap-mode ]])
2018-12-06 19:10:05 +00:00
- Refactor source files with help of [[https://github.com/emacs-php/phpactor.el ][phpactor.el ]]
2019-01-02 21:34:56 +00:00
- Support for the [[https://langserver.org/ ][Language Server Protocol ]] (experimental)
2015-06-10 16:44:30 +00:00
The =gtags= layer is recommended to benefit from better =eldoc= and
=helm-gtags= .
* Install
2019-01-02 21:34:56 +00:00
** Layer
2016-01-06 05:21:55 +00:00
To use this configuration layer, add it to your =~/.spacemacs= . You will need to
add =php= to the existing =dotspacemacs-configuration-layers= list in this
file.
2015-06-10 16:44:30 +00:00
2019-09-17 17:10:26 +00:00
** Backends
2019-12-06 18:59:15 +00:00
For php you have the choice between a set of possible backends with
2019-09-30 04:49:44 +00:00
different setup instructions and different capabilities.
2020-04-18 11:26:47 +00:00
*** LSP
2019-09-30 04:49:44 +00:00
Note that you'll need to use the =lsp= layer to enable these backends.
2019-09-17 17:10:26 +00:00
2020-04-18 11:26:47 +00:00
**** intelephense
2019-09-17 17:10:26 +00:00
This is the recommended LSP server solution. To activate it set the
layer variable =php-backend= :
2019-01-02 21:34:56 +00:00
2020-05-06 22:02:44 +00:00
#+BEGIN_SRC emacs-lisp
2019-01-02 21:34:56 +00:00
(php :variables php-backend 'lsp)
#+END_SRC
2019-09-17 17:10:26 +00:00
and install the npm server with:
2019-01-02 21:34:56 +00:00
#+BEGIN_SRC sh
2019-09-17 17:10:26 +00:00
npm install -g intelephense
2019-01-02 21:34:56 +00:00
#+END_SRC
You can find further information on the project's [[http://intelephense.net/ ][website ]] and [[https://github.com/bmewburn/vscode-intelephense ][GitHub page ]].
2020-04-18 11:26:47 +00:00
**** php-language-server
2020-05-06 22:02:44 +00:00
This is an alternative LSP server implementation working on PHP basis rather
than nodejs. To activate it set the layer variable =php-backend= like for the
intelephense backend and install the server via [[https://getcomposer.org/ ][composer ]]:
2019-01-02 21:34:56 +00:00
#+BEGIN_SRC sh
composer require felixfbecker/language-server
composer run-script --working-dir=vendor/felixfbecker/language-server parse-stubs
#+END_SRC
2019-11-10 14:11:03 +00:00
You can find further information on the project's [[https://github.com/felixfbecker/php-language-server ][GitHub page ]].
2019-01-02 21:34:56 +00:00
2020-04-18 11:26:47 +00:00
**** Debugging
In case of using any of LSP backends You can debug using [[https://microsoft.github.io/debug-adapter-protocol ][dap ]].
2020-05-06 22:02:44 +00:00
First of all You have to add =dap= to layer list of Your =dotspacemacs/layers=
function of a =.spacemacs= config file.
As mentioned in [[https://github.com/emacs-lsp/dap-mode#php ][dap-mode doc for php ]], dap-mode uses a [[https://marketplace.visualstudio.com/items?itemName=webfreak.debug ][VSCode extension ]] as a
debugging backend and includes a convenient =dap-php-setup= command to install
it into Your emacs. To embrace it open any PHP file in any PHP project just
after restarting emacs with =dap= layer enabled. Now You can call the extension
installation command: =<M-x> dap-php-setup= .
After that You can try to debug something. For example add a breakpoint to any
of Your phpunit tests with =SPC m d b a= . And start debugging with =SPC m d d d=
and selecting a debug template. Now run the test to ensure everything is
working. The command to run the test while debugging could look like this:
#+BEGIN_SRC sh
2020-05-06 22:11:32 +00:00
php -d xdebug.idekey=PHPSTORM -d xdebug.remote_autostart=1 -d xdebug.remote_enable=1 -d xdebug.remote_host=127.0.0.1 -d xdebug.remote_port=9000 bin/phpunit ./path/to/Test.php
2020-05-06 22:02:44 +00:00
#+END_SRC
The test is now expected to be paused by emacs/dap when it catches code at the
breakpoint.
You may wish to propagate some config options for the VSCode extension which is
used as a debug server. For example if You are running Your code in a docker
container the project source path may differ between the container (and Xdebug
PHP extension consequently) and Emacs. The VSCode extension provides a config
option to map the path. In VSCode it is done by adding an appropriate json
config. For spacemacs it could be done for example by adding a next call to
=dotspacemacs/user-config= function of Your =.spacemacs= config:
2020-05-06 22:11:32 +00:00
#+BEGIN_SRC emacs-lisp
2020-05-06 22:02:44 +00:00
(with-eval-after-load 'dap-php
(dap-register-debug-template "PHP debug with custom path"
(list :type "php"
:cwd nil
:request "launch"
:name "Php Debug with path"
:args '("--server=4711")
:pathMappings (ht ("/docker/src/path" "/emacs/src/path"))
:sourceMaps t)))
2020-05-06 22:11:32 +00:00
#+END_SRC
2020-04-18 11:26:47 +00:00
2019-09-17 17:10:26 +00:00
*** ac-php-core
This is a non server solution working entirely from an elisp package.
This requires no installation of external services but also delivers
the least amount of IDE like integrations with spacemacs.
2015-06-10 16:44:30 +00:00
2019-09-17 17:10:26 +00:00
To activate it just don't set the variable =php-backend= in your dotfile.
2020-05-06 22:02:44 +00:00
Remember that additional setup instructions are necessary on a per project basis
which you can find below.
2017-11-18 22:34:08 +00:00
2019-09-17 17:10:26 +00:00
**** Setup
2020-05-06 22:02:44 +00:00
Because of the way that the ac-php-core package works, there are a couple of
simple initialization tasks which must occur to get the completion working as it
should. On any new project make sure to perform the following initialization
tasks:
2017-11-18 22:34:08 +00:00
1. Run the following
2018-09-19 03:54:47 +00:00
2018-10-17 04:03:28 +00:00
#+BEGIN_SRC shell
cd /root/of/project
touch .ac-php-conf.json
#+END_SRC
2018-09-19 03:54:47 +00:00
2017-11-18 22:34:08 +00:00
2. Inside of spacemacs run:
2019-04-08 16:59:32 +00:00
= ac-php-remake-tags =
The =.ac-php-conf.json= file is required to enable auto-completion. When you run
2020-05-06 22:02:44 +00:00
=ac-php-remake-tags= and your =.ac-php-conf.json= file is empty the default
configuration will be used and inserted in the file.
2019-04-08 16:59:32 +00:00
If your project contains the following files at the root folder:
1. =.projectile=
2. =vendor/autoload.php=
2020-05-06 22:02:44 +00:00
the necessary configuration file (=.ac-php-conf.json= ) will be created
automatically if it does not exist.
2018-12-06 19:10:05 +00:00
2019-12-06 18:59:15 +00:00
**** Refactoring
2020-05-06 22:02:44 +00:00
This backend provides refactoring and class auto-completion capabilities via
[[https://github.com/emacs-php/phpactor.el ][phpactor.el ]]. To ensure that the phpactor package is intact, just run
=M-x phpactor-install-or-update= and the package itself will make sure that
2019-12-06 18:59:15 +00:00
you're good to go.
2018-12-06 19:10:05 +00:00
2020-04-18 11:26:47 +00:00
**** Debugging
2020-05-06 22:02:44 +00:00
While using ac-php-core debug capabilities are provided via the [[https://github.com/ahungry/geben ][geben package ]].
Please refer for details to the project page.
2019-09-17 17:10:26 +00:00
* Key bindings
2018-12-06 19:10:05 +00:00
** General
2019-12-09 21:44:48 +00:00
| Key binding | Description |
|-------------+-------------------------|
| ~SPC m g g~ | jump to define at point |
| ~C-t~ | jump back |
2018-12-06 19:10:05 +00:00
2019-12-09 21:44:48 +00:00
** Refactoring for non LSP backends
2020-05-06 22:02:44 +00:00
For more precise insights on the meaning of the key bindings please refer to
[[https://phpactor.github.io/phpactor/refactorings.html ][phpactor API reference. ]]
2019-12-09 21:44:48 +00:00
| Key binding | Description |
|---------------+---------------------------------------------------------|
| ~SPC m r i~ | import class under cursor |
| ~SPC m r r~ | rename local variable |
| ~SPC m r R~ | rename variable in a whole file |
| ~SPC m r n~ | synchronize namespace with file location |
| ~SPC m r v~ | toggle method visibility (public->protected->private) |
| ~SPC m r g a~ | generate unknown property accessors |
| ~SPC m r g m~ | generate a method signature by a call example |
| ~SPC m r c n~ | create a new class at a given path |
| ~SPC m r c c~ | copy current class elsewhere |
| ~SPC m r c m~ | move (rename) current class |
| ~SPC m r c i~ | generate an interface from class' public methods |
| ~SPC m r p c~ | declare class properties by constructor signature |
| ~SPC m r p p~ | add missing class properties |
| ~SPC m r e c~ | extract constant under cursor from a class |
| ~SPC m r e e~ | extract expression to a variable |
| ~SPC m r e m~ | extract a code hunk to a method |
| ~SPC m r m c~ | add non-implemented stubs from parent classes/contracts |
| ~SPC m P s~ | ask phpactor about it's status |
| ~SPC m P u~ | install/update phpactor package |
2018-12-06 19:10:05 +00:00
2019-12-06 18:59:15 +00:00
** Debugging for non LSP backends
2019-12-09 21:44:48 +00:00
XDebug client management:
| Key binding | Description |
|-------------+---------------------------------------------|
| ~SPC m d x~ | start XDebug client |
| ~SPC m d X~ | stop XDebug client |
| ~SPC m d b~ | set a predefined breakpoint on current line |
| ~SPC m d C~ | clear predefined breakpoints |
Debugger interaction:
| Key binding | Description |
|-------------+------------------------------------------------------------------|
| ~o~ or ~n~ | step over statement |
| ~s~ or ~i~ | step into current call |
| ~r~ | step out of function |
| ~c~ | resume execution until cursor position or next breakpoint |
| ~e~ | evaluate expression in local context |
| ~L~ | focus line the execution stopped on |
| ~v~ | display context (local/global variables, user-defined constants) |
| ~b b~ | set breakpoint here |
| ~b c~ | set conditional breakpoint here |
| ~b e~ | set breakpoint on exception here |
| ~u~ | unset breakpoint here |
| ~U~ | clear all breakpoints (in all files!) |
| ~w~ | show current stack trace |
| ~g f~ | find debugged file in a worktree |
| ~q~ | quit debugging |
Variable listing:
| Key binding | Description |
|-------------+---------------------------------|
| ~j~ | next variable or section |
| ~k~ | previous variable or section |
| ~TAB~ | fold/unfold variable or section |
| ~q~ | close variable listing |
** LSP key bindings
For a detailed list of key bindings in =lsp-mode= please checkout the README.org
2019-12-06 18:59:15 +00:00
file of the =lsp layer= .
2020-04-18 11:26:47 +00:00
** Debugging for LSP backends
See README.org file of the =dap-layer= for key bindings available in =dap-mode=