Skip to content

Instantly share code, notes, and snippets.

@nopolabs
Last active June 6, 2019 19:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nopolabs/3757aa1d5981f31d88fcf3b29955ced0 to your computer and use it in GitHub Desktop.
Save nopolabs/3757aa1d5981f31d88fcf3b29955ced0 to your computer and use it in GitHub Desktop.
bash script to run php 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment