PHP Cli With Optional Xdebug: Separate Cli Ini File

 

I ran into a problem with a composer script timing out on my machine because I had xdebug installed on the cli.

Now, I sometimes actually use xdebug on the cli, so I wanted a cli config that allowed xdebug be switched on and off, but defaults to off.

I found this global variable: PHP_INI_SCAN_DIR

I'm on Homebrew, so I made a new directory no-xdebug-conf.d alongside /usr/local/etc/php/7.1/conf.d directory, symlinking all the files that exist in /usr/local/etc/php/7.1/conf.d from /usr/local/etc/php/7.1/no-xdebug-conf.d to /usr/local/etc/php/7.1/conf.dexcept, of course ext-xdebug.ini.

  • You can find your configuration directory by executing php -r "phpinfo();" | grep ini.

If you just want xdebug off in the cli, add export PHP_INI_SCAN_DIR='/usr/local/etc/php/7.1/no-xdebug-conf.d' to your ~/.bash_profile.

export PHP_INI_SCAN_DIR='/usr/local/etc/php/7.1/no-xdebug-conf.d'
function xdebug_cli_on() {
	unset PHP_INI_SCAN_DIR
}
 
function xdebug_cli_off() {
	export PHP_INI_SCAN_DIR='/usr/local/etc/php/7.1/no-xdebug-conf.d'
}

Now the script executes in half the time!

I'll need to update this for switching versions of PHP, and I'll edit this post when I get around to that. Incremental improvements. Back to billable work for today!

  • I discovered this doesn't work with PHPStorm, because PHPStorm appears to check for the presence of xdebug using the cli, so the debug icon is grayed out when xdebug for the cli is disabled. I'll be working on a fix for this issue shortly and editing this post.

References:

  • Read about PHP_INI_SCAN_DIR here.

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.