berkes
berkes

Reputation: 27553

PHP debugger for Vim: Debug Commandline scripts

My vim debugger requires me to set an Xdebug cookie in my browser, by appending ?XDEBUG_SESSION_START=1, after which I can start debugging.

But I cannot set this cookie/session when calling a script on the CLI.

How does one debug commandline php-scripts with vim?

Upvotes: 4

Views: 1839

Answers (2)

Stabledog
Stabledog

Reputation: 3370

I have not found all the pieces for this puzzle in one convenient place, so here's my slightly-more-complete solution. This works for me with vim 7.3, xdebug 2.0.

  1. Get the debugger vim plugin

    • The debugger.py file goes in .vim/plugins, which pathogen does not do automatically.
    • Use F5 to start vim listening for incoming xdebug connections (on port 9000 by default)
  2. Use the right xdebug-related settings in php.ini (use an alternate php.ini, perhaps).:

[Zend]
zend_extension = /full/path/to/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_port =9000
xdebug.remote_host = localhost
; We have to turn on remote_autostart when running php from
; cli.  That's probably a good reason to keep the cli and apache
; versions of php.ini distinct.
xdebug.remote_autostart=1
; idekey can be just about anything, but the value in php.ini needs
; to match the value used in the environment that launches php. 
xdebug.idekey=vim_session
  1. When launching php script from the command line, preset the idekey environment var in the form

export XDEBUG_IDEKEY="idekey=vim_session"

  1. Press F5 in vim to start listening on the remote_port

  2. In the shell with the XDEBUG_IDEKEY value, start php with "php {scriptname}"

So php loads php.ini, finds the xdebug.so extension, which is initialized with those php.ini settings. The xdebug extension intercepts the script execution and tries to connect to localhost:9000, which is where the vim+python extension is listening. Once a connection is established, the xdebug extension coordinates the debugging session, and the vim plugin puts up a bunch of ide-like debugging windows. Voila!

Bonus link: I also use this shell script to launch php. It waits until it sees vim open the debug port, and then starts the php session. Upon completion, it prints the result code and loops back for another run (unless you hit ctrl+c, of course).

Upvotes: 6

romainl
romainl

Reputation: 196546

I think you will find your answer in the docs (search for Starting The Debugger).

Upvotes: 0

Related Questions