Skip to content

Instantly share code, notes, and snippets.

@jimdelois
Last active September 5, 2017 15:38
Show Gist options
  • Save jimdelois/245c50bd7d31baa774c07644a39f30f9 to your computer and use it in GitHub Desktop.
Save jimdelois/245c50bd7d31baa774c07644a39f30f9 to your computer and use it in GitHub Desktop.
Create a quick executable to toggle XDebug on your local environment.

XDebug Toggler

A script that generates an executable which will toggle XDebug on/off on your local development environment.

Download

$> curl -o install_xdebug_toggle.sh https://gist.githubusercontent.com/jimdelois/245c50bd7d31baa774c07644a39f30f9/raw/install_xdebug_toggle.sh
$> chmod +x install_xdebug_toggle.sh

Usage

The script requires the location of an XDebug INI file to verify that it can install the toggler. If you're installing this toggler, then you probably already have the XDebug extension installed. In this case, you can find the location of the XDebug INI file by running:

$> php -i | grep xdebug.ini
/usr/local/etc/php/7.0/conf.d/ext-xdebug.ini

Provide this location as the first argument to the script. An OPTIONAL second argument is the path to which you'd like the toggler executable installed. By default, this is /usr/local/bin/xdebug. E.g.,

$> ./install_xdebug_toggler.sh /usr/local/etc/php/7.0/conf.d/ext-xdebug.ini
Writing toggle script at /usr/local/bin/xdebug...
Updating execute permissions on /usr/local/bin/xdebug...
Done.

Now you can invoke /usr/local/bin/xdebug (or the custom script location if you provided one) to toggle XDebug on/off accordingly:

$> php -v
PHP 7.0.9 (cli) (built: Jul 21 2016 14:50:53) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

$> xdebug off
Disabling XDebug Extension...
Done.

$> php -v
PHP 7.0.9 (cli) (built: Jul 21 2016 14:50:53) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

$> xdebug on
Enabling XDebug Extension...
Done.

$> php -v
PHP 7.0.9 (cli) (built: Jul 21 2016 14:50:53) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

Now, you can enjoy lightning fast unit tests and composer updates again!

OUTPUT_FILE=/usr/local/bin/xdebug
if [[ ! $@ ]]; then
echo "Must provide path to XDebug INI file as parameter"
exit 1
fi
INI_FILE=$1
if [[ ! -f $INI_FILE ]]; then
echo No such INI file found: $INI_FILE
exit 1
fi
if [[ $2 ]]; then
OUTPUT_FILE=$2
fi
echo Writing toggle script at $OUTPUT_FILE...
printf "FILE=%s
if [[ ! -f \$FILE ]]; then
echo No file \$FILE found. Consider regenerating this script.
exit 1
fi
if [[ \"\$@\" == \"on\" ]]; then
echo Enabling XDebug Extension...
sed -i.BAK 's/^;zend_extension/zend_extension/' \$FILE
elif [[ \"\$@\" == \"off\" ]]; then
echo Disabling XDebug Extension...
sed -i.BAK 's/^zend_extension/;zend_extension/' \$FILE
elif [[ \"\$@\" == \"help\" ]]; then
printf \"
Target INI File: %%s
Available Commands: on, off, help
\" \$FILE
exit 0
else
echo Invalid. Input must be \"on\", \"off\" or \"help\"
exit 2
fi
echo Done.
" \
"$INI_FILE" \
> $OUTPUT_FILE
echo Updating execute permissions on $OUTPUT_FILE...
chmod ug+x $OUTPUT_FILE
echo Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment