Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create xdebug debugger plugin #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 90 additions & 15 deletions .led/plugins/php.plugin.sh
Original file line number Diff line number Diff line change
@@ -5,20 +5,26 @@ _php_plugin_desc()
}

# get env key from a service starting with "php" or "apache"
# _php_get_service_env_key "ENV_VARIABLE"
_php_get_service_env_key()
# _php_get_service_key "ENV_VARIABLE"
_php_get_service_key()
{
local env_var=$1
local service_key=$1
local service_value=$2

local service_keys service_key
local services service
local dc_key

service_keys=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get ".services | keys" | cut -c3-)
for service_key in ${service_keys}
services=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get ".services | keys" | cut -c3-)
for service in ${services}
do
# if a service start with "php" or "apache", stop and get his env key
if [[ "${service_key}" == php* ]] || [[ "${service_key}" == apache* ]]; then
dc_key=".services.${service_key}.environment.${env_var}"
if [[ "${service}" == php* ]] || [[ "${service}" == apache* ]]; then
dc_key=".services.${service}.${service_key}"

if [ ! -z "${service_value}" ]; then
dc_key="${dc_key}.${service_value}"
fi

break
fi
done
@@ -114,7 +120,7 @@ php_xdebug()
exit 1
fi

if ! dc_key=$(_php_get_service_env_key "PHP_XDEBUG"); then
if ! dc_xdebug_key=$(_php_get_service_key "environment" "PHP_XDEBUG"); then
exit 1
fi

@@ -123,33 +129,102 @@ php_xdebug()
enable) xdebug_value=1;;
disable) xdebug_value=0;;
switch)
xdebug_value=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get "${dc_key}")
xdebug_value=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get "${dc_xdebug_key}")
[ "${xdebug_value}" == "null" ] && xdebug_value=0
# value can be 1 or 0, so substract to reverse
xdebug_value=$((1 - xdebug_value))
;;
esac


local dc_xdebug_value=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get "${dc_xdebug_key}")
if [ ${xdebug_value} -eq 0 ]; then
${VENDORS_BIN_JYPARSER} "${dc_file}" del "${dc_key}" > "${dc_file_tmp}" \
_check_xdebug_state ${dc_xdebug_value} ${xdebug_value}

${VENDORS_BIN_JYPARSER} "${dc_file}" del "${dc_xdebug_key}" > "${dc_file_tmp}" \
&& mv "${dc_file_tmp}" "${dc_file}"
_disable_debugger
ret=$?
message="XDebug disabled"
else
${VENDORS_BIN_JYPARSER} "${dc_file}" set "${dc_key}" 1 > "${dc_file_tmp}" \
_check_xdebug_state ${dc_xdebug_value} ${xdebug_value}

${VENDORS_BIN_JYPARSER} "${dc_file}" set "${dc_xdebug_key}" 1 > "${dc_file_tmp}" \
&& mv "${dc_file_tmp}" "${dc_file}"
_enable_debugger
ret=$?
message="XDebug enabled"
fi

# refresh container
if [ $ret -eq 0 ]; then
echo "${message}"
$0 up
# $0 up
fi
}

_enable_debugger()
{
local phpstorm_ip
local plugin_dir="$(_plugin_files_dir "php")"
local origin_xdebug_config_file="${plugin_dir}/90-xdebug-config.ini"
local dc_volumes_key=$(_php_get_service_key "volumes")
local dc_ide_config_key=$(_php_get_service_key "environment" "PHP_IDE_CONFIG")
local dc_volumes_length
local dc_xdebug_env_value

if [[ -f "${origin_xdebug_config_file}" ]]; then
target_xdebug_config=".led/90-xdebug-config.ini"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you must create .led directory if missing, otherwise the sed command can't create output file


#get the ip of docker network
phpstorm_ip=$(docker network inspect bridge | jq -r '.[].IPAM.Config[].Gateway')
#put it into xdebug config file
sed "s/DOCKER_IP/${phpstorm_ip}/" "${origin_xdebug_config_file}" > "${target_xdebug_config}"
dc_xdebug_env_value="${target_xdebug_config}:/etc/php.d/90-xdebug-config.ini"

dc_volumes_length=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get "${dc_volumes_key} | length")
#edit docker-compose file
${VENDORS_BIN_JYPARSER} "${dc_file}" set "${dc_volumes_key}[${dc_volumes_length}]" \"${dc_xdebug_env_value}\" > "${dc_file_tmp}" \
&& mv "${dc_file_tmp}" "${dc_file}"
${VENDORS_BIN_JYPARSER} "${dc_file}" set "${dc_ide_config_key}" \"serverName=localhost\" > "${dc_file_tmp}" \
&& mv "${dc_file_tmp}" "${dc_file}"
fi
}

_disable_debugger()
{
local dc_volumes_key=$(_php_get_service_key "volumes")
local dc_ide_config_key=$(_php_get_service_key "environment" "PHP_IDE_CONFIG")
local dc_volumes_length
target_xdebug_config=".led/90-xdebug-config.ini"
dc_volumes_length=$(${VENDORS_BIN_JYPARSER} "${dc_file}" get "${dc_volumes_key} | length")

#edit docker-compose file
${VENDORS_BIN_JYPARSER} "${dc_file}" del "${dc_volumes_key}[${dc_volumes_length} - 1]" > "${dc_file_tmp}" \
&& mv "${dc_file_tmp}" "${dc_file}"
${VENDORS_BIN_JYPARSER} "${dc_file}" del "${dc_ide_config_key}" > "${dc_file_tmp}" \
&& mv "${dc_file_tmp}" "${dc_file}"

if [[ -e "${target_xdebug_config}" ]]; then
rm "${target_xdebug_config}"
fi
}

# Checks the state of xDebug in led, in order to avoid that some
# manipulations are performed several times in a row
_check_xdebug_state()
{
local dc_xdebug_value=$1
local xdebug_arg_value=$2

if [[ "${dc_xdebug_value}" -eq 0 && "${xdebug_arg_value}" -eq 0 ]]; then
echo "Xdebug is already disabled"
exit 1;
fi

if [[ "${dc_xdebug_value}" -eq 1 && "${xdebug_arg_value}" -eq 1 ]]; then
echo "Xdebug is already enabled"
exit 1;
fi
}

# memory
@@ -182,7 +257,7 @@ php_memory()
exit 1
fi

if ! dc_key=$(_php_get_service_env_key "PHP_MEMORY_LIMIT"); then
if ! dc_key=$(_php_get_service_key "environment" "PHP_MEMORY_LIMIT"); then
exit 1
fi

4 changes: 4 additions & 0 deletions .led/plugins/php/90-xdebug-config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
xdebug.client_port = 9000
xdebug.mode = debug
xdebug.client_host = DOCKER_IP
xdebug.start_with_request = yes