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.
#!/bin/bash

PHP=${PHP:-`which php`}
XDEBUG_INI=${XDEBUG_INI:-`${PHP} --ini | awk 'BEGIN { FS=","; } /xdebug.ini/ { print $1; }'`}
XDEBUG_HOST=${XDEBUG_HOST:-1.2.3.1}
XDEBUG_PORT=${XDEBUG_PORT:-9001}
IDE_KEY=${IDE_KEY:-PHPSTORM}

if [ "$1" = "--help" -o "$1" = "" ]
then
    echo "Usage:"
    echo "  source xdebug --on   Enable XDebug in php.ini file."
    echo "  source xdebug --off  Disable XDebug in php.ini file."
    echo "  xdebug --status      Report if XDebug is enabled or disabled and if ."
    echo "  xdebug php-file      Run a php script with XDebug enabled."
    exit 0
fi

OS=`uname`
if [ "$OS" = "Darwin" ]
then
    SED=(sed -i "''")
else
    SED=(sed -i)
fi

restart-services() {
    echo "Restarting services ..."
    if [ "$OS" = "Darwin" ]
    then
        valet restart
    else
        service php-fpm restart
    fi
}

status() {
    grep "^zend_extension" ${XDEBUG_INI} &> /dev/null
    if [ "$?" -eq 0 ]
    then
        echo XDebug enabled in ${XDEBUG_INI}
        echo XDEBUG_CONFIG=${XDEBUG_CONFIG}
    else
        echo XDebug disabled in ${XDEBUG_INI}
        echo XDEBUG_CONFIG=${XDEBUG_CONFIG}
    fi
}

check() {
    # check for a process is listening at host:port
    nc -z ${XDEBUG_HOST} ${XDEBUG_PORT} &> /dev/null

    if [ "$?" -ne 0 ]
    then
        echo "No listener at ${XDEBUG_HOST} ${XDEBUG_PORT}"
        exit 1
    fi

    echo "Found listener at ${XDEBUG_HOST} ${XDEBUG_PORT}"
}

if [ "$1" = "--on" ]
then
    ${SED[@]} 's/;zend_extension/zend_extension/' ${XDEBUG_INI}
    export XDEBUG_CONFIG="idekey=${IDE_KEY} remote_host=${XDEBUG_HOST} remote_port=${XDEBUG_PORT}"
    status
    check
    restart-services

elif [ "$1" = "--off" ]
then
    ${SED[@]} 's/^zend_extension/;zend_extension/' ${XDEBUG_INI}
    unset XDEBUG_CONFIG
    status
    restart-services

elif [ "$1" = "--status" ]
then
    status
    check

else
    check

    XDEBUG_SO=$(awk 'BEGIN { FS="="; } /^;?zend_extension/ { print $NF; }' ${XDEBUG_INI})

    XDEBUG_CONFIG="idekey=${IDE_KEY} remote_host=${XDEBUG_HOST} remote_port=${XDEBUG_PORT}" \
        ${PHP} \
            -didekey=${IDE_KEY} \
            -dzend_extension=${XDEBUG_SO} \
            -dxdebug.remote_enable=1 \
            -dxdebug.remote_mode=req \
            -dxdebug.remote_port=${XDEBUG_PORT} \
            -dxdebug.remote_host=${XDEBUG_HOST} \
              "$@"
fi

Comments are closed.