New php helper for dockerized cli scripts

This commit is contained in:
Anthony Axenov 2023-03-27 08:32:21 +08:00
parent 283bac61eb
commit fec161f77b
Signed by: anthony
GPG Key ID: EA9EC32FF7CCD4EC
10 changed files with 187 additions and 13 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/nbproject/private/

View File

@ -1,6 +1,5 @@
<?php
$url = 'https://axenov.dev';
echo "Hello from php!" . PHP_EOL;
$data = ['phpstorm', 'netbeans'];
phpinfo();
//var_dump($data);
var_dump($data);
$url = 'https://axenov.dev';

2
app/subdir/file.php Normal file
View File

@ -0,0 +1,2 @@
<?php
echo 'Hello from subdir!';

View File

@ -13,7 +13,7 @@ services:
- test
volumes:
- ./php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
- ./index.php:/var/www/index.php
- ./app:/var/www
nginx:
container_name: test-nginx
@ -23,7 +23,7 @@ services:
- test
volumes:
- ./nginx/vhost.conf:/etc/nginx/conf.d/default.conf
- ./index.php:/var/www/index.php
- ./app:/var/www
ports:
- '8888:80'
links:

View File

1
nbproject/php Symbolic link
View File

@ -0,0 +1 @@
../php/php

View File

@ -0,0 +1,7 @@
include.path=${php.global.include.path}
php.version=PHP_81
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.

9
nbproject/project.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>nb-test</name>
</data>
</configuration>
</project>

View File

@ -2,7 +2,7 @@ FROM php:8.1-fpm
# https://pecl.php.net/package/xdebug
RUN pecl channel-update pecl.php.net && \
pecl install xdebug-3.1.6
pecl install xdebug-3.2.0
EXPOSE 9000
WORKDIR /var/www

167
php/php
View File

@ -1,7 +1,162 @@
#!/bin/bash
#echo ${BASH_ARGV[*]}
docker exec test-php php \
-dxdebug.mode=debug \
-dxdebug.start_with_request=1 \
`basename ${BASH_ARGV[0]}` \
"${@:1:$#-1}"
# Welcome to amusement park!
[[ "$1" = '--help' ]] || [[ "$1" = '-h' ]] && cat <<EOF && exit
NetBeans docker wrapper for php
===============================
Anthony Axenov (c) 2023, The MIT License
https://axenov.dev
https://opensource.org/license/mit
Replacement host php interpreter with dockerized one to run & debug cli php scripts.
Usage:
./$(basename $0) --container=<NAME> [--map=<PATH1>:<PATH2>] [PHP_ARGS] <SCRIPT> [SCRIPT_ARGS]
Arguments:
--container : docker container where your SCRIPT is located. Required.
--map : sources path mapped from the host to container. Not required.
PATH1 is an absolute path to php sources directory on the host.
PATH2 is an absolute path of the same directory inside of container.
Delimiter ':' is required. If PATH1, PATH2 or delimiter is missed
or value is empty then error will be thrown.
PHP_ARGS : arguments you can pass to real php interpreter according to its --help.
Not required.
SCRIPT : a path to script file (.php) to be executed in container. Required.
Note that this file must exist inside or be available from that container.
SCRIPT_ARGS : arguments to call your script with. They will be passed to script as is.
Not required.
Read this article to know how to set this helper as interpreter for NetBeans:
ru: https://axenov.dev/netbeans-php-docker-xdebug-cli
en: https://axenov.dev/en/netbeans-php-docker-xdebug-cli-en
EOF
pwd=$(pwd) # current working directory
cmdline=($@) # copy currently called command line to array
collect_php_args=1 # should we collect php args or script ones?
quiet=0 # should we print some useful data before executing?
# find a path where this wrapper is located
wrapper_dir="$(dirname $0)"
# find a path where project is probably located
project_dir="$(dirname $wrapper_dir)"
# here we check if this wrapper is global or local
# but if it is set as global from nbproject dir of
# current project then it is not detected as global
# anyway behavior will be correct
nbproject="$(basename $wrapper_dir)"
[ "$nbproject" = 'nbproject' ] && is_global=0 || is_global=1
# prepare new array to collect php args
declare -a php_cmd=("docker" "exec")
# and another one for script args
declare -a script_args=()
# and one more for directory mapping
declare -a map_arr=()
# iterate over arguments we received from netbeans
for arg in "${cmdline[@]}"; do
# if this is a container name
if [ "${arg::11}" = '--container' ]; then
container="${arg:12}" # save it
php_cmd+=("$container" 'php') # add php itself
continue # jump to next iteration
fi
# if this is a path map
if [ "${arg::5}" = '--map' ]; then
map="${arg:6}" # save it
map_arr=(${map//:/ }) # split it and check if it is correct
if [ -z "${map_arr[0]}" ] || [ -z "${map_arr[1]}" ]; then
echo "ERROR: directory map is incorrect!"
echo "Use $0 --help to get info about how to use this wrapper."
echo "Exit code 3."
exit 3
fi
continue # jump to next iteration
fi
# if this is a container name
if [ "${arg::7}" = '--quiet' ]; then
quiet=1
continue # jump to next iteration
fi
# if this is an absolute path to a script file
if [ -f "$arg" ]; then
# make its path correct for container
if [ "$map" ]; then # when paths are mapped
# remove first part of map from an absolute filepath and append result to second map part
filepath="${map_arr[1]}${arg##${map_arr[0]}}"
else # when paths are NOT mapped
# remove project path from absolute filepath
filepath="${arg##$project_dir/}"
fi
php_cmd+=("$filepath") # append php args with filepath
collect_php_args=0 # now we need to collect script args
continue # jump to next iteration
fi
if [ "$collect_php_args" = 1 ]; then # if we collect php args
php_cmd+=("$arg") # add current arg to php args as is
continue # jump to next iteration
fi
script_args+=("$arg") # otherwise add current arg to script args as is
done
# docker container name is required so we must halt here if there is no one
if [ -z "$container" ]; then
echo "ERROR: no docker container is specified!" >&2
echo "Use $0 --help to get info about how to use this wrapper." >&2
echo "Exit code 1." >&2
exit 1
fi
# path to php script is also required so we must halt here too if there is no one
if [ -z "$filepath" ]; then
echo "ERROR: no script filepath is specified!" >&2
echo "Use $0 --help to get info about how to use this wrapper." >&2
echo "Exit code 2." >&2
exit 2
fi
cmdline="${php_cmd[*]} ${script_args[*]}" # make a command to execute
# print some important data collected above
if [ "$quiet" = 0 ]; then
echo "NetBeans docker wrapper for php"
echo "==============================="
echo -e "Container name: $container"
echo -e "Script path: $filepath"
echo -e "Directory mapping: ${map:-(none)}"
echo -e "Command line:\n$cmdline\n"
fi
# some debug output
# echo "=== some debug output ========="
# cat <<EOF | column -t
# is_global $is_global
# container $container
# pwd $pwd
# wrapper_dir $wrapper_dir
# nbproject $nbproject
# project_dir $project_dir
# map $map
# map_arr[0] ${map_arr[0]}
# map_arr[1] ${map_arr[1]}
# filepath $filepath
# EOF
# echo "==============================="
$cmdline # execute
# that's folks!