Zum Hauptinhalt springen
  1. Blog/

Easy Mode Switch für Xdebug 3

thumb-xdebug-switcher-1.png

Da Xdebug 3 das Mode-Konzept eingeführt hat, um zwischen verschiedenen Konfigurationen umzuschalten, und ich Xdebug aus Performance-Gründen nur noch aktiviere, wenn ich es brauche, habe ich mein Xdebug-Konsolenskript umgeschrieben, um die Modi einfach umzuschalten.

Das Skript ist für die Verwendung mit der Homebrew-Installation von php unter macOS gedacht. Es modifiziert im Grunde die bestehende “xdebug.mode” in der php.ini und startet php-fpm anschließend neu.

Fügen Sie diesem Skript einen Alias hinzu (in meinem Fall “x”), passen Sie den Pfad zu Ihrer php.ini an und Sie können die Modi einfach umschalten.

Usage: x {o|c|d|s}

o  develop    Enables Development Aids including the overloaded var_dump().
c  coverage   Enables Code Coverage Analysis to generate code coverage reports, mainly in combination with PHPUnit.
d  debug      Enables Step Debugging. This can be used to step through your code while it is running, and analyse values of variables.
s  show xdebug mode

Hier das Skript:

#!/bin/sh

#####################################################################
#
# Enable/disable xdebug via export XDEBUG_CONFIG config and restart php-fpm
#
# Usage:
# xdebug [off|cov|dbg|status]
#
#####################################################################


PHPVER="$(php --version | tail -r | tail -n 1 | cut -d " " -f 2 | cut -d "." -f 1,2)"
PHPINI="/usr/local/etc/php/$PHPVER/php.ini"

setXdebugMode()
{
mode=$1
echo "Set Xdebug mode to $mode"
sed -i -e "/xdebug.mode=/ s/=.*/=$mode/" $PHPINI
}

restartPhpFpm()
{
echo "Restart php-fpm..."
brew services restart php
exit 0
}

showXdebugMode()
{
MODE=$(awk -F "=" '/xdebug.mode/ {print $2}' $PHPINI)
COLOR_REST="$(tput sgr0)"
COLOR_GREEN="$(tput setaf 2)"

printf '%s%s%s%s\n' 'current xdebug mode: ' $COLOR_GREEN $MODE $COLOR_REST
}

showHelp()
{
echo "Usage: x {o|c|d|s}" >&2
echo
echo "   o  develop    Enables Development Aids including the overloaded var_dump()."
echo "   c  coverage   Enables Code Coverage Analysis to generate code coverage reports, mainly in combination with PHPUnit."
echo "   d  debug      Enables Step Debugging. This can be used to step through your code while it is running, and analyse values of variables."
echo "   s  show xdebug mode"
echo
exit 1
}

case $1 in
o)
setXdebugMode develop
restartPhpFpm
;;
c)
setXdebugMode coverage
restartPhpFpm
;;
d)
setXdebugMode debug
restartPhpFpm
;;
s)
showXdebugMode
;;
*)
showHelp
;;
esac
``