In this tutorial we're going to create a command line alias to quickly enable or disable xdebug.
When you install xdebug you will need to register the extension in your php.ini file. This will need to point to the xdebug extension file.
zend_extension=/usr/local/opt/php71-xdebug/xdebug.so
This will ensure that xdebug is enabled, the problem with this is that when running your unit tests it will be very slow. Therefore if you want to speed up running your tests a quick solution is to disable xdebug if it's not needed.
An easy way to disable xdebug is to rename the extension so that it can't be found, therefore we can create two scripts that will enable or disable xdebug, then we can create command line aliases making it very easy for us to switch xdebug on or off.
Disable xdebug script
#!/bin/sh
mv /usr/local/opt/php71-xdebug/xdebug.so /usr/local/opt/php71-xdebug/xdebug.so.disabled
brew services restart php71
echo "xdebug is now disabled"
This script will rename the xdebug file by adding a .disabled
to the end of the file name, this will make sure
the php.ini
file can not find the extension file and therefore turn off xdebug.
Then it will restart php by using a homebrew command brew services restart php71
.
Enable xdebug script
#!/bin/sh
mv /usr/local/opt/php71-xdebug/xdebug.so.disabled /usr/local/opt/php71-xdebug/xdebug.so
brew services restart php71
echo "xdebug is now enabled"
This script will do the opposite of the above and rename xdebug.so.disabled to just xdebug.so this will allow php.ini
file to find the extension and enable xdebug again.
Then again it will restart php by using a homebrew command brew services restart php71
.
Command Line Alias
Next we can make sure we can quickly run these scripts from any project we're working on by creating a command line
alias to run these. Open your .bash_profile
file and paste in the following.
alias exd="sh enabled-xdebug.sh"
alias dxd="sh disable-xdebug.sh"
Now we can run exd
from the command line to enable xdebug and dxd
to disable xdebug.