Since Xdebug 3 introduced the mode-concept to switch different configurations and i already enable Xdebug only when i need it due to performance reasons, i rewrote my Xdebug console script to easy switch the modes.

The script is for use with homebrew installation of php on macOS. It basically modifies the existing „xdebug.mode“ in your php.ini and restarts php-fpm afterwards.

Add an alias to this script (in my case „x“), adjust the path to your php.ini and you can easily switch the modes.

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

Here it is:

#!/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

Leave a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.