Skip to content

Instantly share code, notes, and snippets.

@dbalabka
Last active January 11, 2019 09:47
Show Gist options
  • Save dbalabka/9059015 to your computer and use it in GitHub Desktop.
Save dbalabka/9059015 to your computer and use it in GitHub Desktop.
!!! Script is migrated to https://github.com/torinaki/phpdebug-cli !!! XDebug PHP CLI debugging helper console commands with PhpStorm, NetBeans support.
# Installation steps:
# 1. Put this file under ~/.phpdebug
# $ curl https://gist.githubusercontent.com/torinaki/9059015/raw/.phpdebug > ~/.phpdebug
# 2. Put following lines into ~/.bashrc:
# # Get the aliases and functions
# if [ -f ~/.phpdebug ]; then
# . ~/.phpdebug
# fi
# 3. (optional) Script will try to autodetect your machine IP where runs IDE or use 127.0.0.1 otherwise.
# If automatic detection doesn't work for you, set ip directly via IDE_IP enviroment variable:
# $ export IDE_IP=1.2.3.4
# 4. (optional) Change ports if it not same as default (see local variables zend_debugger_config and xdebug_config);
# $ export IDE_ZEND_PORT=10137
# $ export IDE_ZDEBUG_PORT=9000
# 5. (optional) Change IDE specific configuration: serverName, idekey
# $ export IDE_SERVERNAME="Any_server_name"
# $ export IDE_KEY="PHPSTORM"
# 6. Run: source ~/.bashrc
# Usage:
# 1. After installation use phpdebug/phpdebugoff commands to start/stop debugging:
# $ phpdebug
# $ php ./script1_to_debug.php
# $ php ./script2_to_debug.php
# $ phpdebugoff
# 2. Or simply use instead of php command to debug only one script
# $ phpdebug -dmemory_limit=1G ./script_to_debug.php
# Init default evniroment variables
export IDE_ZEND_PORT=10137
export IDE_XDEBUG_PORT=9000
export IDE_KEY="PHPSTORM"
export IDE_SERVERNAME="$(hostname)"
function _check_xdebug_installed()
{
if ! php -i | egrep -q "(xdebug|zend_debugger)"; then
echo "Warning! XDebug or Zend Debugger extension not found"
fi
}
function _print_settings_info()
{
echo -e "\e[93mXDebug connection is $IDE_IP:$IDE_XDEBUG_PORT and server name '$IDE_SERVERNAME'. Zend Debugger connection is $IDE_IP:$IDE_ZEND_PORT with IDE key '$IDE_KEY'"
echo -e "\e[93mAny of this settings can be configured with environment variables: IDE_IP, IDE_XDEBUG_PORT, IDE_SERVERNAME, IDE_ZEND_PORT, IDE_KEY"
}
function _phpdebug()
{
# For Zend debugger
# Set special EVN variables (don't forget to change "debug_host" and "debug_port")
local zend_debugger_config="start_debug=1&debug_stop=0&debug_fastfile=1&debug_covere=1&use_remote=1&send_sess_end=1&debug_session_id=2000&debug_start_session=1&debug_port=${IDE_ZEND_PORT}"
# For Xdebug
# Set special EVN variables.
# PHPSTORM is default idekey key. It must be same like in PhpStorm -> Settings -> PHP -> Debug -> DBGp Proxy
local xdebug_config="idekey=${IDE_KEY} remote_port=${IDE_XDEBUG_PORT}"
# For PhpStorm
# Same for Xdebug and Zend Debugger
# PhpStorm server config name is placed in Settings -> PHP -> Servers
local php_ide_config="serverName=${IDE_SERVERNAME}"
# try to autodetect IP if global variable IDE_IP not set
if [ -z ${IDE_IP+x} ]; then
if [ -z "$SSH_CLIENT" ]; then
IDE_IP="127.0.0.1";
else
IDE_IP=$( echo $SSH_CLIENT | sed s/\ .*$// );
fi
fi
zend_debugger_config="${zend_debugger_config}&debug_host=${IDE_IP}"
xdebug_config="${xdebug_config} remote_host=${IDE_IP}"
_check_xdebug_installed
_print_settings_info
if [ "$*" = "" ]; then
export QUERY_STRING="$zend_debugger_config"
export XDEBUG_CONFIG="$xdebug_config"
export PHP_IDE_CONFIG="$php_ide_config"
else
PHP_IDE_CONFIG="$php_ide_config" XDEBUG_CONFIG="$xdebug_config" QUERY_STRING="$zend_debugger_config" php $*
fi
}
alias phpdebug="_phpdebug"
alias phpdebugoff="unset QUERY_STRING && unset PHP_IDE_CONFIG && unset XDEBUG_CONFIG"
@pquerner
Copy link

Line #32 shouldnt hardcode the IP either, if you have $IDE_IP set.
Otherwise it looks great, but can really be shortend down to something like that:

(yes, this doesnt have comments)

#!/bin/bash
IP=$(echo $SSH_CLIENT | cut -d '=' -f 2 | awk '{print $1}');
PHP_IDE_CONFIG="serverName=CHANGEME" XDEBUG_CONFIG=idekey=pq-phpstorm /usr/bin/php5.5 -dxdebug.remote_autostart=1 -dxdebug.remote_port=60050 -dxdebug.remote_connect_back=0 -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dx $*
//script.sh index.php param1 param2

@dbalabka
Copy link
Author

@pquerner sorry for so not quick response 😊. Agree with you that implementation may be less verbose, but it is better than short and hard undestandable.
I have moved IDE_IP to global variable and made possible to debug only one script(same as in you implementation):

$ phpdebug ./script.php

@pquerner
Copy link

pquerner commented Jan 7, 2016

I think I like this the best:

export IDE_IP=127.0.0.1
export DEBUG_SERVERNAME=pq
#THX https://gist.github.com/torinaki/9059015
# Installation steps:
#1. Put these lines into ~/.bashrc;
#2. Script will try to autodetect your machine IP where runs IDE or use 127.0.0.1 otherwise.
#    If automatic detection doesn't work for you, set ip directly via IDE_IP enviroment variable:
#       $ export IDE_IP=1.2.3.4
#3. Change ports if it not same as default (see local variables zend_debugger_config and xdebug_config);
#4. Change IDE specific configuration: serverName, idekey (see local variables php_ide_config and xdebug_config)
#5. Run: source ~/.bashrc

# Usage:
#1. After installation use phpdebug/phpdebugoff commands to start/stop debugging:
#   $ phpdebug
#   $ php ./script1_to_debug.php
#   $ php ./script2_to_debug.php
#   $ phpdebugoff
#2. Or simply use instead of php command to debug only one script
#   $ phpdebug -dmemory_limit=1G ./script_to_debug.php

# User specific aliases and functions
function _phpdebug() {
    # For Zend debugger
    # Set special EVN variables (don't forget to change "debug_host" and "debug_port")
    local zend_debugger_config="start_debug=1&debug_stop=0&debug_fastfile=1&debug_covere=1&use_remote=1&send_sess_end=1&debug_session_id=2000&debug_start_session=1&debug_port=10137"
    # For Xdebug
    # Set special EVN variables.
    # PHPSTORM is default idekey key. It must be same like in PhpStorm -> Settings -> PHP -> Debug -> DBGp Proxy
    #local xdebug_config="idekey=pq-phpstorm"
    # case for remote debugging (don't forget to change "remote_host" and "remote_port")
    local xdebug_config="idekey=pq-phpstorm remote_host=127.0.0.1 remote_port=60050"
    # For PhpStorm
    # Same for Xdebug and Zend Debugger
    # PhpStorm server config name is placed in Settings -> PHP -> Servers
    local php_ide_config="serverName=${DEBUG_SERVERNAME}"

    # try to autodetect IP if global variable IDE_IP not set
    if [ -z ${IDE_IP+x} ]; then
        if [ -z "$SSH_CLIENT" ]; then
           IDE_IP="127.0.0.1";
        else
           IDE_IP=$( echo $SSH_CLIENT | sed s/\ .*$// );
        fi
    fi
    zend_debugger_config="${zend_debugger_config}&debug_host=${IDE_IP}"

    if [ "$*" == "" ]; then
        export QUERY_STRING="$zend_debugger_config"
export XDEBUG_CONFIG="$xdebug_config_"
        export PHP_IDE_CONFIG="$php_ide_config"
    else
        PHP_IDE_CONFIG="$php_ide_config" XDEBUG_CONFIG="$xdebug_config_" QUERY_STRING="$zend_debugger_config" php5.5 $*
    fi
}
alias phpdebug="_phpdebug"
alias phpdebugoff="unset QUERY_STRING && unset PHP_IDE_CONFIG && unset XDEBUG_CONFIG"
export -f _phpdebug

New is:

export -f _phpdebug

to have the function available global - from any bash script.

export DEBUG_SERVERNAME=pq
local php_ide_config="serverName=${DEBUG_SERVERNAME}"

To enable dynamic servernames instead of hardcoded onces. Although they're not meant to be changed..

Which you can trigger in a separete shell script like this:

#!/bin/bash
#//script.sh servername script.php <param1> <param2>
export DEBUG_SERVERNAME=$1
#echo ${DEBUG_SERVERNAME}
#call function defined in .bashrc
_phpdebug
php5.5 -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=60050 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_c_back=0 $2 $3 $4

(because sometimes you have multiple instances of an app running, with different servernames (prod,dev,staging) etc.)

@dbalabka
Copy link
Author

dbalabka commented Dec 6, 2016

@pquerner please check. I have implemented dynamic servername

@dbalabka
Copy link
Author

I have moved this script to https://github.com/torinaki/phpdebug-cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment