xdebug bash script improved

I use a bash script to help me manage enabling and disabling xdebug (gist).

This version works on both my OSX laptop and my Centos virtual machine.

When using the script to toggle xdebug on or off you need to source it in order for it to be able to update XDEBUG_CONFIG in your current shell environment.

The function restart-services is called to restart php-fpm when xdebug is toggled on or off so that it will use the updated php.ini file.

Usage:
    source xdebug --on   Enable XDebug in php.ini file.
    source xdebug --off  Disable XDebug in php.ini file.
    xdebug --status      Report if XDebug is enabled or disabled.
    xdebug php-file      Run a php script with XDebug enabled.

<br />
#!/bin/bash</p>
<p>PHP=${PHP:-`which php`}<br />
XDEBUG_INI=${XDEBUG_INI:-`${PHP} --ini | awk 'BEGIN { FS=&quot;,&quot;; } /xdebug.ini/ { print $1; }'`}<br />
XDEBUG_HOST=${XDEBUG_HOST:-1.2.3.1}<br />
XDEBUG_PORT=${XDEBUG_PORT:-9001}<br />
IDE_KEY=${IDE_KEY:-PHPSTORM}</p>
<p>if [ &quot;$1&quot; = &quot;--help&quot; -o &quot;$1&quot; = &quot;&quot; ]<br />
then<br />
    echo &quot;Usage:&quot;<br />
    echo &quot;  source xdebug --on   Enable XDebug in php.ini file.&quot;<br />
    echo &quot;  source xdebug --off  Disable XDebug in php.ini file.&quot;<br />
    echo &quot;  xdebug --status      Report if XDebug is enabled or disabled and if .&quot;<br />
    echo &quot;  xdebug php-file      Run a php script with XDebug enabled.&quot;<br />
    exit 0<br />
fi</p>
<p>OS=`uname`<br />
if [ &quot;$OS&quot; = &quot;Darwin&quot; ]<br />
then<br />
    SED=(sed -i &quot;''&quot;)<br />
else<br />
    SED=(sed -i)<br />
fi</p>
<p>restart-services() {<br />
    echo &quot;Restarting services ...&quot;<br />
    if [ &quot;$OS&quot; = &quot;Darwin&quot; ]<br />
    then<br />
        valet restart<br />
    else<br />
        service php-fpm restart<br />
    fi<br />
}</p>
<p>status() {<br />
    grep &quot;^zend_extension&quot; ${XDEBUG_INI} &amp;&gt; /dev/null<br />
    if [ &quot;$?&quot; -eq 0 ]<br />
    then<br />
        echo XDebug enabled in ${XDEBUG_INI}<br />
        echo XDEBUG_CONFIG=${XDEBUG_CONFIG}<br />
    else<br />
        echo XDebug disabled in ${XDEBUG_INI}<br />
        echo XDEBUG_CONFIG=${XDEBUG_CONFIG}<br />
    fi<br />
}</p>
<p>check() {<br />
    # check for a process is listening at host:port<br />
    nc -z ${XDEBUG_HOST} ${XDEBUG_PORT} &amp;&gt; /dev/null</p>
<p>    if [ &quot;$?&quot; -ne 0 ]<br />
    then<br />
        echo &quot;No listener at ${XDEBUG_HOST} ${XDEBUG_PORT}&quot;<br />
        exit 1<br />
    fi</p>
<p>    echo &quot;Found listener at ${XDEBUG_HOST} ${XDEBUG_PORT}&quot;<br />
}</p>
<p>if [ &quot;$1&quot; = &quot;--on&quot; ]<br />
then<br />
    ${SED[@]} 's/;zend_extension/zend_extension/' ${XDEBUG_INI}<br />
    export XDEBUG_CONFIG=&quot;idekey=${IDE_KEY} remote_host=${XDEBUG_HOST} remote_port=${XDEBUG_PORT}&quot;<br />
    status<br />
    check<br />
    restart-services</p>
<p>elif [ &quot;$1&quot; = &quot;--off&quot; ]<br />
then<br />
    ${SED[@]} 's/^zend_extension/;zend_extension/' ${XDEBUG_INI}<br />
    unset XDEBUG_CONFIG<br />
    status<br />
    restart-services</p>
<p>elif [ &quot;$1&quot; = &quot;--status&quot; ]<br />
then<br />
    status<br />
    check</p>
<p>else<br />
    check</p>
<p>    XDEBUG_SO=$(awk 'BEGIN { FS=&quot;=&quot;; } /^;?zend_extension/ { print $NF; }' ${XDEBUG_INI})</p>
<p>    XDEBUG_CONFIG=&quot;idekey=${IDE_KEY} remote_host=${XDEBUG_HOST} remote_port=${XDEBUG_PORT}&quot; \<br />
        ${PHP} \<br />
            -didekey=${IDE_KEY} \<br />
            -dzend_extension=${XDEBUG_SO} \<br />
            -dxdebug.remote_enable=1 \<br />
            -dxdebug.remote_mode=req \<br />
            -dxdebug.remote_port=${XDEBUG_PORT} \<br />
            -dxdebug.remote_host=${XDEBUG_HOST} \<br />
              &quot;$@&quot;<br />
fi<br />

Comments are closed.