The reload container (docker/web-dev/live.js, browser-sync) now publishes the site's port itself, so the page's socket can connect
(the old proxy ws option moved it to an unpublished port: the tab never followed). The IDE tells it each saved file (POST /__jumphp/notify),
so it works without file events (Docker Desktop also polls); CSS is injected without a reload, anything else reloads once.
Status bar item Live: N (browser tabs connected), open the site at the live-reload address, turn it off ('live': 'off' in web.json).
bin/jumphp web:live, LiveReload (IDE) + tests, end-to-end test with headless Chrome on a Symfony site, docs, 14 languages.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
862 lines
48 KiB
Shell
Executable file
862 lines
48 KiB
Shell
Executable file
#!/usr/bin/env bash
|
|
# Web projects (sites, not games) for the JumPHP IDE and the command line, on ANY folder (no JumPHP needed in it):
|
|
# bin/jumphp web:new <symfony|wordpress|prestashop|sylius|laravel|php> <folder> [--db=mariadb|postgres|sqlite|none] [--docker=dunglas]
|
|
# a new site, made in Docker (--docker=dunglas: Symfony in https://github.com/dunglas/symfony-docker)
|
|
# bin/jumphp web:db <folder> <mariadb|postgres|sqlite|none> the database of a site (its DATABASE_URL / DB_* and the service)
|
|
# bin/jumphp web:dockerize <folder> an existing Symfony site moved to symfony-docker's files (its own Dockerfile + compose)
|
|
# bin/jumphp web:serve [folder] FrankenPHP (+ MariaDB when the site needs one) + live reload (browser-sync) + assets
|
|
# (npm run watch / dev when package.json has it, else SCSS compiled by sass --watch);
|
|
# prints "[web] open <url>" (the IDE opens the browser there), then the logs until stopped
|
|
# bin/jumphp web:stop [folder] stops it (the database stays, in a Docker volume)
|
|
# bin/jumphp web:up [folder] applies a change of the stack (compose.override.yaml...): the changed containers again, no logs
|
|
# bin/jumphp web:config [folder] the stack as docker compose reads it (generated file + the site's compose.override.yaml)
|
|
# bin/jumphp web:info [folder] what kind of site, its addresses
|
|
# bin/jumphp web:composer <folder> <args...> / web:console <folder> <args...> composer / bin/console (or artisan) in the PHP
|
|
# container (the running site's own container when it is up: its database is reachable)
|
|
# bin/jumphp web:php <folder> [8.x|auto] [--json] the site's PHP version, shown or chosen (.jumphp/web.json "php"); the same
|
|
# for serve, composer and console, so the dependencies and the PHP that runs them agree
|
|
# bin/jumphp web:docker <folder> [jumphp|dunglas|compose [service] [file]] its Docker setup, shown or changed
|
|
# bin/jumphp web:status <folder> JSON for the IDE: setup, PHP (and why), image, running container
|
|
# Everything runs in Docker (images jumphp/web:php<x.y> = FrankenPHP + extensions, one per PHP version, mariadb:11.4,
|
|
# node:22-alpine), as the user.
|
|
# The stack is written into <site>/.jumphp/compose.yaml at each start; the site's own changes go into <site>/compose.override.yaml
|
|
# (kept in git), merged over it (docker compose -f .jumphp/compose.yaml -f compose.override.yaml, the site as project directory).
|
|
set -euo pipefail
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
# the PHP versions offered (dunglas/frankenphp:1-php<x.y> exists for them), newest first; toolkit-feature: web-php
|
|
WEB_PHP_VERSIONS="8.5 8.4 8.3 8.2"
|
|
WEB_PHP_DEFAULT=8.4
|
|
WEB_REV=4
|
|
web_image() { echo "jumphp/web:php${1:-$WEB_PHP_DEFAULT}"; }
|
|
say() { printf '\033[1;36m==> %s\033[0m\n' "$*"; }
|
|
die() { printf '\033[1;31m[x] %s\033[0m\n' "$*" >&2; exit 1; }
|
|
[ "${WEB_SH_LIB:-}" = 1 ] || command -v docker >/dev/null || die "Docker is needed (https://docs.docker.com/get-docker/)"
|
|
|
|
dir_of() { local d="${1:-$PWD}"; [ -d "$d" ] || die "no folder $d"; (cd "$d" && pwd); }
|
|
|
|
# what kind of site a folder holds
|
|
kind_of() {
|
|
local d="$1" k
|
|
if [ -f "$d/.jumphp/web.json" ]; then
|
|
k="$(sed -n 's/.*"kind": *"\([a-z-]*\)".*/\1/p' "$d/.jumphp/web.json" | head -1)"
|
|
[ -n "$k" ] && { echo "$k"; return; }
|
|
fi
|
|
if [ -f "$d/wp-load.php" ] || [ -f "$d/wp-config.php" ]; then echo wordpress
|
|
elif [ -f "$d/config/defines.inc.php" ] && [ -d "$d/classes" ]; then echo prestashop
|
|
elif grep -qs '"sylius/sylius"' "$d/composer.json"; then echo sylius
|
|
elif [ -f "$d/artisan" ]; then echo laravel
|
|
elif [ -f "$d/bin/console" ] && grep -qs 'symfony/' "$d/composer.json"; then echo symfony
|
|
else echo php; fi
|
|
}
|
|
|
|
docroot_of() {
|
|
case "$1" in
|
|
symfony|sylius|laravel) echo public ;;
|
|
*) if [ -f "$2/public/index.php" ] && [ ! -f "$2/index.php" ]; then echo public; else echo .; fi ;;
|
|
esac
|
|
}
|
|
|
|
# the database of a site: .jumphp/web.json "db", else what its configuration says, else the kind's default
|
|
db_of() {
|
|
local kind="$1" d="$2" v url
|
|
if [ -f "$d/.jumphp/web.json" ]; then
|
|
v="$(sed -n 's/.*"db": *"\([a-z]*\)".*/\1/p' "$d/.jumphp/web.json" | head -1)"
|
|
[ -n "$v" ] && { echo "$v"; return; }
|
|
fi
|
|
case "$kind" in
|
|
wordpress|prestashop) echo mariadb; return ;;
|
|
esac
|
|
url="$(grep -hsE '^(DATABASE_URL|DB_CONNECTION)=' "$d/.env.local" "$d/.env" 2>/dev/null | tail -1 | cut -d= -f2- | tr -d '"'"'"'')"
|
|
case "$url" in
|
|
postgres*|pgsql*) echo postgres ;;
|
|
sqlite*) echo sqlite ;;
|
|
mysql*|mariadb*) echo mariadb ;;
|
|
"") case "$kind" in sylius) echo mariadb ;; *) echo none ;; esac ;;
|
|
*) echo mariadb ;;
|
|
esac
|
|
}
|
|
# a database server to start (sqlite and none: no service)
|
|
needs_db() { case "$(db_of "$1" "$2")" in mariadb|postgres) return 0 ;; *) return 1 ;; esac; }
|
|
|
|
# .jumphp/web.json: flat string keys only (kind, db, docker, php, compose, service; the IDE adds runner, local_php)
|
|
web_json_get() { local f="$1/.jumphp/web.json"; [ -f "$f" ] || return 0; sed -n "s/.*\"$2\": *\"\([^\"]*\)\".*/\1/p" "$f" | head -1; }
|
|
|
|
# a key written (an empty value removes it); the other keys are kept as they are
|
|
web_json_set() {
|
|
local d="$1" key="$2" value="$3" f line k out=()
|
|
mkdir -p "$d/.jumphp"; f="$d/.jumphp/web.json"
|
|
if [ -f "$f" ]; then
|
|
while IFS= read -r line; do
|
|
k="${line#\"}"; k="${k%%\"*}"
|
|
[ "$k" = "$key" ] || out+=("$line")
|
|
done < <(grep -o '"[A-Za-z0-9_]*": *"[^"]*"' "$f" || true)
|
|
fi
|
|
[ -z "$value" ] || out+=("\"$key\": \"$value\"")
|
|
local joined=""; for line in "${out[@]+"${out[@]}"}"; do joined="${joined:+$joined, }$line"; done
|
|
printf '{%s}\n' "$joined" > "$f"
|
|
}
|
|
|
|
# symfony-docker (dunglas): the project has its own Dockerfile + compose.yaml with a php service built on FrankenPHP
|
|
has_dunglas_files() { [ -f "$1/compose.yaml" ] && [ -d "$1/frankenphp" ] && grep -qs 'frankenphp' "$1/Dockerfile"; }
|
|
|
|
# the Docker setup of a site: jumphp (the stack web:serve writes), dunglas (symfony-docker's files), compose (the site's own
|
|
# compose file, one of its services runs PHP); .jumphp/web.json "docker" says, else symfony-docker's files, else jumphp
|
|
setup_of() {
|
|
local v; v="$(web_json_get "$1" docker)"
|
|
case "$v" in
|
|
jumphp|dunglas|compose) echo "$v" ;;
|
|
*) if has_dunglas_files "$1"; then echo dunglas; else echo jumphp; fi ;;
|
|
esac
|
|
}
|
|
is_dunglas() { [ "$(setup_of "$1")" = dunglas ]; }
|
|
is_own() { [ "$(setup_of "$1")" = compose ]; }
|
|
|
|
# the site's own compose stack: its compose file (web.json "compose", else what docker compose reads by itself), its PHP service
|
|
own_service() { local s; s="$(web_json_get "$1" service)"; echo "${s:-php}"; }
|
|
own_compose() {
|
|
local d="$1"; shift
|
|
local f; f="$(web_json_get "$d" compose)"
|
|
if [ -n "$f" ]; then (cd "$d" && docker compose -f "$f" "$@"); else (cd "$d" && docker compose "$@"); fi
|
|
}
|
|
own_file() {
|
|
local d="$1" f; f="$(web_json_get "$d" compose)"
|
|
if [ -z "$f" ]; then for f in compose.yaml compose.yml docker-compose.yml docker-compose.yaml ""; do [ -z "$f" ] || [ -f "$d/$f" ] && break; done; fi
|
|
echo "$f"
|
|
}
|
|
|
|
# PHP ------------------------------------------------------------------------------------------------------------
|
|
|
|
offered() { printf ' %s ' "$WEB_PHP_VERSIONS" | grep -q " $1 "; }
|
|
|
|
# what Composer needs (composer.json / composer.lock: tools/web-php.php, JumPHP\Ide\Web\PhpVersions), "<x.y>\t<why>"; kept in
|
|
# .jumphp/php.auto until those files change
|
|
php_auto() {
|
|
local d="$1" key cache out
|
|
if [ ! -f "$d/composer.json" ] && [ ! -f "$d/composer.lock" ]; then printf '%s\tdefault: no composer.json\n' "$WEB_PHP_DEFAULT"; return; fi
|
|
key="$(cat "$d/composer.json" "$d/composer.lock" 2>/dev/null | cksum | cut -d' ' -f1) $WEB_PHP_DEFAULT $WEB_PHP_VERSIONS"
|
|
cache="$d/.jumphp/php.auto"
|
|
if [ -f "$cache" ] && [ "$(head -1 "$cache")" = "$key" ]; then sed -n 2p "$cache"; return; fi
|
|
if [ -n "${WEB_PHP_RESOLVER:-}" ]; then out="$($WEB_PHP_RESOLVER "$d" "$WEB_PHP_VERSIONS" "$WEB_PHP_DEFAULT" 2>/dev/null || true)"
|
|
else out="$(docker run --rm --entrypoint php -v "$ROOT:/jumphp:ro" -v "$d:/app:ro" composer:2 /jumphp/tools/web-php.php /app "$WEB_PHP_VERSIONS" "$WEB_PHP_DEFAULT" 2>/dev/null | tail -1 || true)"; fi
|
|
if ! offered "${out%%$'\t'*}"; then printf '%s\tdefault: composer.json could not be read\n' "$WEB_PHP_DEFAULT"; return; fi
|
|
mkdir -p "$d/.jumphp" && printf '%s\n%s\n' "$key" "$out" > "$cache" 2>/dev/null || true
|
|
echo "$out"
|
|
}
|
|
|
|
# the PHP of symfony-docker's Dockerfile (FROM dunglas/frankenphp:1-php8.5 AS frankenphp_upstream)
|
|
dockerfile_php() { sed -nE 's/^[[:space:]]*FROM[[:space:]]+[^[:space:]]*frankenphp:[^[:space:]]*php([0-9]+\.[0-9]+).*/\1/p' "$1/Dockerfile" 2>/dev/null | head -1; }
|
|
|
|
# the site's PHP, "<x.y>\t<why>": .jumphp/web.json "php", else symfony-docker's Dockerfile, else (JumPHP stack) what Composer
|
|
# needs; an own compose stack runs the PHP of its image (empty when web.json does not say)
|
|
php_info() {
|
|
local d="$1" v
|
|
v="$(web_json_get "$d" php)"
|
|
if [ -n "$v" ]; then printf '%s\tweb.json: chosen for this project\n' "$v"; return; fi
|
|
case "$(setup_of "$d")" in
|
|
dunglas) v="$(dockerfile_php "$d")"; printf '%s\tDockerfile: symfony-docker'"'"'s image\n' "$v" ;;
|
|
compose) printf '\tcompose: the image of the service %s\n' "$(own_service "$d")" ;;
|
|
*) php_auto "$d" ;;
|
|
esac
|
|
}
|
|
php_of() { php_info "$1" | cut -f1; }
|
|
|
|
# a symfony-docker Dockerfile on another PHP: its FROM dunglas/frankenphp:...php<x.y> (and an ARG PHP_VERSION)
|
|
dunglas_set_php() {
|
|
local d="$1" v="$2"
|
|
[ -f "$d/Dockerfile" ] || return 0
|
|
sed -i -E "s#^([[:space:]]*FROM[[:space:]]+[^[:space:]]*frankenphp:[^[:space:]]*php)[0-9]+\.[0-9]+#\1$v#; s#^([[:space:]]*ARG[[:space:]]+PHP_VERSION=)[0-9.]+#\1$v#" "$d/Dockerfile"
|
|
[ "$(dockerfile_php "$d")" = "$v" ] && say "Dockerfile: FROM dunglas/frankenphp:1-php$v" || say "the Dockerfile has no dunglas/frankenphp FROM line: set its PHP $v by hand"
|
|
}
|
|
|
|
# an own compose stack on another PHP: the image tag of its PHP service (php:8.3-fpm -> php:8.5-fpm), else the FROM of the
|
|
# Dockerfile next to it; reported when neither says a PHP version
|
|
own_set_php() {
|
|
local d="$1" v="$2" f svc before
|
|
f="$(own_file "$d")"; svc="$(own_service "$d")"
|
|
[ -n "$f" ] && [ -f "$d/$f" ] || { say "no compose file: set PHP $v in your image by hand"; return 0; }
|
|
before="$(cksum < "$d/$f")"
|
|
awk -v svc="$svc" -v ver="$v" '
|
|
/^services:/ { inS = 1; print; next }
|
|
inS && /^[^[:space:]#]/ { inS = 0 }
|
|
inS && match($0, /^[[:space:]]+[A-Za-z0-9_.-]+:[[:space:]]*$/) {
|
|
line = $0; sub(/^[[:space:]]+/, "", line); ind = length($0) - length(line)
|
|
if (base == "" || ind <= base) { base = ind; name = line; sub(/:.*/, "", name); cur = name }
|
|
}
|
|
inS && cur == svc && /^[[:space:]]+image:/ && /php/ {
|
|
if (match($0, /:[^:]*$/)) {
|
|
tag = substr($0, RSTART); head = substr($0, 1, RSTART - 1)
|
|
if (sub(/[0-9]+\.[0-9]+(\.[0-9]+)?/, ver, tag)) { $0 = head tag }
|
|
}
|
|
}
|
|
{ print }' "$d/$f" > "$d/$f.new" && mv "$d/$f.new" "$d/$f"
|
|
if [ "$(cksum < "$d/$f")" != "$before" ]; then say "$f: the image of $svc now runs PHP $v"; return 0; fi
|
|
if [ -f "$d/Dockerfile" ] && grep -qiE '^[[:space:]]*FROM[[:space:]]+[^[:space:]]*php[^[:space:]]*:[^[:space:]]*[0-9]+\.[0-9]+' "$d/Dockerfile"; then
|
|
sed -i -E "0,/^[[:space:]]*FROM[[:space:]]+[^[:space:]]*php[^[:space:]]*:/s#^([[:space:]]*FROM[[:space:]]+[^[:space:]]*php[^[:space:]]*:[^0-9[:space:]]*)[0-9]+\.[0-9]+(\.[0-9]+)?#\1$v#" "$d/Dockerfile"
|
|
say "Dockerfile: its PHP image now $v"; return 0
|
|
fi
|
|
say "the image of $svc does not name a PHP version: change it to PHP $v by hand in $f"
|
|
}
|
|
|
|
# DATABASE_URL (Symfony, Sylius) / DB_* (Laravel) for a database, from the site's containers (host db)
|
|
db_url() {
|
|
case "$1" in
|
|
mariadb) echo 'mysql://app:app@db:3306/app?serverVersion=11.4.0-MariaDB&charset=utf8mb4' ;;
|
|
postgres) echo 'postgresql://app:app@db:5432/app?serverVersion=16&charset=utf8' ;;
|
|
sqlite) echo 'sqlite:///%kernel.project_dir%/var/data.db' ;;
|
|
esac
|
|
}
|
|
|
|
# the site configured for a database: written in .env.local (Symfony, Sylius) or .env (Laravel), and remembered
|
|
set_db() {
|
|
local d="$1" db="$2" kind
|
|
kind="$(kind_of "$d")"
|
|
case "$db" in mariadb|postgres|sqlite|none) ;; *) die "unknown database $db: mariadb, postgres, sqlite or none" ;; esac
|
|
case "$kind" in
|
|
wordpress|prestashop) [ "$db" = mariadb ] || die "$kind needs MariaDB / MySQL" ;;
|
|
esac
|
|
case "$kind" in
|
|
symfony|sylius)
|
|
touch "$d/.env.local"
|
|
sed -i '/^DATABASE_URL=/d' "$d/.env.local"
|
|
[ "$db" = none ] || printf 'DATABASE_URL="%s"\n' "$(db_url "$db")" >> "$d/.env.local" ;;
|
|
laravel)
|
|
local conn host
|
|
case "$db" in mariadb) conn=mysql; host=db ;; postgres) conn=pgsql; host=db ;; sqlite) conn=sqlite; host= ;; none) conn=sqlite; host= ;; esac
|
|
sed -i -e "s|^DB_CONNECTION=.*|DB_CONNECTION=$conn|" "$d/.env" 2>/dev/null || true
|
|
if [ -n "$host" ]; then
|
|
sed -i -e 's|^# *DB_HOST=.*|DB_HOST=db|' -e 's|^DB_HOST=.*|DB_HOST=db|' -e "s|^# *DB_PORT=.*|DB_PORT=$([ "$db" = postgres ] && echo 5432 || echo 3306)|" \
|
|
-e "s|^DB_PORT=.*|DB_PORT=$([ "$db" = postgres ] && echo 5432 || echo 3306)|" -e 's|^# *DB_DATABASE=.*|DB_DATABASE=app|' -e 's|^DB_DATABASE=.*|DB_DATABASE=app|' \
|
|
-e 's|^# *DB_USERNAME=.*|DB_USERNAME=app|' -e 's|^DB_USERNAME=.*|DB_USERNAME=app|' -e 's|^# *DB_PASSWORD=.*|DB_PASSWORD=app|' -e 's|^DB_PASSWORD=.*|DB_PASSWORD=app|' "$d/.env" 2>/dev/null || true
|
|
fi ;;
|
|
esac
|
|
web_json_set "$d" db "$db"
|
|
say "database of $d: $db"
|
|
}
|
|
|
|
hash_of() { printf '%s' "$1" | cksum | cut -d' ' -f1; }
|
|
name_of() { echo "jumphp-web-$(basename "$1" | tr -c 'a-zA-Z0-9\n' '-' | tr 'A-Z' 'a-z' | cut -c1-20)-$(( $(hash_of "$1") % 100000 ))"; }
|
|
port_of() { echo $(( 8100 + $(hash_of "$1") % 700 * 3 )); } # 3 ports: the site (live reload), PHP directly, the database
|
|
|
|
# the image of a PHP version (jumphp/web:php<x.y>), built the first time
|
|
image_built() { [ "$(docker image inspect -f '{{index .Config.Labels "jumphp.web.rev"}}' "$(web_image "$1")" 2>/dev/null)" = "$WEB_REV" ]; }
|
|
ensure_image() {
|
|
local v="${1:-$WEB_PHP_DEFAULT}"
|
|
offered "$v" || die "PHP $v is not offered (FrankenPHP images: $WEB_PHP_VERSIONS): bin/jumphp web:php <folder> <version>"
|
|
if ! image_built "$v"; then
|
|
say "building $(web_image "$v") (FrankenPHP + PHP $v + extensions, the first time: a few minutes)" >&2
|
|
docker build -q --build-arg PHP_VERSION="$v" -t "$(web_image "$v")" "$ROOT/docker/web-dev" >/dev/null || die "the web image of PHP $v did not build"
|
|
fi
|
|
}
|
|
|
|
# JUMPHP_LIVE_POLL=1|0 forces polling; else on for Docker Desktop (Windows / macOS), whose bind mounts send no file events
|
|
live_poll() {
|
|
case "${JUMPHP_LIVE_POLL:-auto}" in 1|true|yes) echo 1; return ;; 0|false|no) echo 0; return ;; esac
|
|
if docker info --format '{{.OperatingSystem}}' 2>/dev/null | grep -qi 'docker desktop'; then echo 1; else echo 0; fi
|
|
}
|
|
|
|
# the assets of a site: npm watch / dev, else SCSS (folders holding .scss files that are not partials)
|
|
assets_command() {
|
|
local d="$1"
|
|
if [ -f "$d/package.json" ]; then
|
|
local script
|
|
for script in watch dev; do
|
|
if grep -qE "\"$script\" *:" "$d/package.json"; then
|
|
echo "npm install --no-audit --no-fund && npm run $script"; return
|
|
fi
|
|
done
|
|
fi
|
|
local dirs
|
|
dirs="$(find "$d" \( -name node_modules -o -name vendor -o -name .git -o -name var -o -name .jumphp \) -prune -o -name '[!_]*.scss' -print 2>/dev/null \
|
|
| sed "s|^$d/||" | xargs -r -n1 dirname | sort -u | head -20 | sed 's|\(.*\)|/app/\1:/app/\1|' | tr '\n' ' ')"
|
|
[ -n "$dirs" ] && echo "npx --yes sass@1 --watch --no-source-map $dirs"
|
|
}
|
|
|
|
# the IDE's Debug: JUMPHP_XDEBUG=<port> (the IDE listens there on this computer)
|
|
xdebug_env() {
|
|
[[ "${JUMPHP_XDEBUG:-}" =~ ^[0-9]{2,5}$ ]] || return 0
|
|
printf ', XDEBUG_MODE: debug, XDEBUG_SESSION: jumphp, XDEBUG_CONFIG: "client_host=host.docker.internal client_port=%s start_with_request=yes"' "$JUMPHP_XDEBUG"
|
|
}
|
|
|
|
compose_file() {
|
|
local d="$1" kind="$2" root="$3" port="$4" db="$5" assets="$6" php="${7:-$WEB_PHP_DEFAULT}" uid gid
|
|
uid="$(id -u)"; gid="$(id -g)"
|
|
mkdir -p "$d/.jumphp"
|
|
{
|
|
echo "# made by bin/jumphp web:serve (tools/web.sh): written again at each start"
|
|
echo "services:"
|
|
echo " app:"
|
|
echo " image: $(web_image "$php")"
|
|
echo " user: \"$uid:$gid\""
|
|
echo " working_dir: /app"
|
|
echo " volumes: [\"$d:/app\"]"
|
|
echo " environment: { DOC_ROOT: \"/app/${root#./}\", HOME: /tmp$(xdebug_env)$( [ -n "$db" ] && [ "$db" != sqlite ] && [ "$db" != none ] && echo ', DATABASE_HOST: db, WORDPRESS_DB_HOST: db') }"
|
|
# live reload off (web.json "live": "off", the IDE's status bar): no reload container, the site is served on its own address
|
|
if [ "$(web_json_get "$d" live)" = off ]; then
|
|
echo " ports: [\"127.0.0.1:$port:8080\", \"127.0.0.1:$((port + 1)):8080\"]"
|
|
else
|
|
echo " ports: [\"127.0.0.1:$((port + 1)):8080\"]"
|
|
fi
|
|
# the IDE's Debug (JUMPHP_XDEBUG=<port>): Xdebug on, every request connects back to the IDE (breakpoints in the gutter);
|
|
# the site is /app in the container (the IDE maps it to the project). Off otherwise: no cost.
|
|
if [[ "${JUMPHP_XDEBUG:-}" =~ ^[0-9]{2,5}$ ]]; then
|
|
echo " extra_hosts: [\"host.docker.internal:host-gateway\"]"
|
|
fi
|
|
{ [ "$db" = mariadb ] || [ "$db" = postgres ]; } && echo " depends_on: [db]"
|
|
if [ "$(web_json_get "$d" live)" != off ]; then
|
|
echo " reload:"
|
|
echo " image: node:22-alpine"
|
|
echo " user: \"$uid:$gid\""
|
|
echo " working_dir: /tmp"
|
|
echo " environment: { HOME: /tmp, npm_config_cache: /tmp/.npm, NODE_PATH: /tmp/live/node_modules, JUMPHP_LIVE_POLL: \"$(live_poll)\", JUMPHP_LIVE_PORT: \"$port\" }"
|
|
echo " volumes: [\"$d:/app:ro\"]"
|
|
echo " command: [\"sh\", \"-c\", \"npm install --prefix /tmp/live --no-audit --no-fund --loglevel=error browser-sync@3 && exec node /app/.jumphp/live.js\"]"
|
|
echo " ports: [\"127.0.0.1:$port:$port\"]"
|
|
echo " depends_on: [app]"
|
|
fi
|
|
if [ -n "$assets" ]; then
|
|
echo " assets:"
|
|
echo " image: node:22-alpine"
|
|
echo " user: \"$uid:$gid\""
|
|
echo " working_dir: /app"
|
|
echo " environment: { HOME: /tmp, npm_config_cache: /tmp/.npm }"
|
|
echo " volumes: [\"$d:/app\"]"
|
|
echo " command: [\"sh\", \"-c\", $(printf '%s' "$assets" | sed 's/["\\]/\\&/g; s/^/"/; s/$/"/')]"
|
|
fi
|
|
if [ "$db" = mariadb ]; then
|
|
echo " db:"
|
|
echo " image: mariadb:11.4"
|
|
echo " environment: { MARIADB_ROOT_PASSWORD: root, MARIADB_DATABASE: app, MARIADB_USER: app, MARIADB_PASSWORD: app }"
|
|
echo " volumes: [\"db:/var/lib/mysql\"]"
|
|
echo " ports: [\"127.0.0.1:$((port + 2)):3306\"]"
|
|
echo "volumes:"
|
|
echo " db:"
|
|
elif [ "$db" = postgres ]; then
|
|
echo " db:"
|
|
echo " image: postgres:16-alpine"
|
|
echo " environment: { POSTGRES_DB: app, POSTGRES_USER: app, POSTGRES_PASSWORD: app }"
|
|
echo " volumes: [\"pgdata:/var/lib/postgresql/data\"]"
|
|
echo " ports: [\"127.0.0.1:$((port + 2)):5432\"]"
|
|
echo "volumes:"
|
|
echo " pgdata:"
|
|
fi
|
|
} > "$d/.jumphp/compose.yaml"
|
|
# browser-sync (docker/web-dev/live.js) keeps the browser's Host (changeOrigin false): the site makes its links with localhost:<port>,
|
|
# not "app:8080" (Symfony's debug toolbar and profiler, WordPress's URLs, redirects all need it). It also answers the IDE's
|
|
# "this file was saved" notice and says how many browser tabs are connected (/__jumphp/notify, /__jumphp/status): the IDE reads
|
|
# live_port from web.json
|
|
cp "$ROOT/docker/web-dev/live.js" "$d/.jumphp/live.js"
|
|
rm -f "$d/.jumphp/bs-config.js"
|
|
web_json_set "$d" live_port "$port"
|
|
grep -qsx '/.jumphp/' "$d/.gitignore" 2>/dev/null || { [ -d "$d/.git" ] && echo '/.jumphp/' >> "$d/.gitignore"; } || true
|
|
}
|
|
|
|
# is the site's compose.override.yaml merged into the JumPHP stack? Yes, unless it is Symfony Flex's: its recipes (doctrine,
|
|
# mailer...) write blocks for the services of compose.yaml ("database" ports...), which the JumPHP stack does not have; then
|
|
# it is merged only when Compose accepts it (checked once per change of the file, the answer kept in .jumphp/override.check)
|
|
override_merged() {
|
|
local d="$1" o="$1/compose.override.yaml" key c
|
|
[ -f "$o" ] || return 1
|
|
grep -qs '^###> ' "$o" && [ -f "$d/compose.yaml" ] || return 0
|
|
key="$(cksum < "$o" | cut -d' ' -f1)"
|
|
c="$d/.jumphp/override.check"
|
|
if [ ! -f "$c" ] || [ "$(cut -d' ' -f1 "$c")" != "$key" ]; then
|
|
if [ -f "$d/.jumphp/compose.yaml" ] && docker compose -f "$d/.jumphp/compose.yaml" -f "$o" --project-directory "$d" config -q >/dev/null 2>&1; then
|
|
echo "$key ok" > "$c"
|
|
else
|
|
echo "$key skip" > "$c"
|
|
printf '\033[1;33m[!] compose.override.yaml holds Symfony Flex recipes for compose.yaml'"'"'s services: not merged into the JumPHP stack, until docker compose accepts it with the stack (or use them: bin/jumphp web:docker %s compose)\033[0m\n' "$d" >&2
|
|
fi
|
|
fi
|
|
[ "$(cut -d' ' -f2 "$c")" = ok ]
|
|
}
|
|
|
|
# docker compose on a JumPHP site's stack: the generated file, then the site's compose.override.yaml when it has one
|
|
stack_compose() {
|
|
local d="$1"; shift
|
|
local extra=()
|
|
override_merged "$d" && extra=(-f "$d/compose.override.yaml" --project-directory "$d")
|
|
docker compose -p "$(name_of "$d")" -f "$d/.jumphp/compose.yaml" "${extra[@]+"${extra[@]}"}" "$@"
|
|
}
|
|
|
|
info() {
|
|
local d="$1" kind port
|
|
kind="$(kind_of "$d")"; port="$(port_of "$d")"
|
|
echo "kind: $kind"
|
|
echo "PHP: $(php_info "$d" | sed 's/\t/ (/; s/$/)/; s/^ (/(/')"
|
|
if is_own "$d"; then
|
|
echo "docker: its own compose stack, PHP in the service $(own_service "$d")"
|
|
return 0
|
|
fi
|
|
echo "site (live reload): http://localhost:$port"
|
|
echo "PHP directly: http://localhost:$((port + 1))"
|
|
if is_dunglas "$d"; then
|
|
echo "docker: symfony-docker (dunglas): its own compose.yaml, PHP at http://localhost:$port"
|
|
fi
|
|
case "$(db_of "$kind" "$d")" in
|
|
mariadb) echo "database: MariaDB mysql://app:[email protected]:$((port + 2))/app (from the site: host db, port 3306, user app, password app, database app)" ;;
|
|
postgres) echo "database: PostgreSQL postgresql://app:[email protected]:$((port + 2))/app (from the site: host db, port 5432, user app, password app, database app)" ;;
|
|
sqlite) echo "database: SQLite (var/data.db)" ;;
|
|
*) echo "database: none" ;;
|
|
esac
|
|
true
|
|
}
|
|
|
|
# a symfony-docker site: its own compose.yaml, on this site's ports, plain http (no certificate to accept)
|
|
dunglas_env() {
|
|
local port; port="$(port_of "$1")"
|
|
echo "HTTP_PORT=$port HTTPS_PORT=$((port + 1)) HTTP3_PORT=$((port + 1)) SERVER_NAME=:80"
|
|
# its compose.override.yaml reads XDEBUG_MODE; its Xdebug calls host.docker.internal:9003 (the IDE's Debug listens there when it can)
|
|
[[ "${JUMPHP_XDEBUG:-}" =~ ^[0-9]{2,5}$ ]] && echo "XDEBUG_MODE=debug"
|
|
true
|
|
}
|
|
dunglas_compose() { local d="$1"; shift; (cd "$d" && env $(dunglas_env "$d") docker compose -p "$(name_of "$d")" "$@"); }
|
|
|
|
serve_dunglas() {
|
|
local d="$1" port; port="$(port_of "$d")"
|
|
say "symfony-docker site in $d (its own Dockerfile and compose.yaml: the first start builds its image)"
|
|
# no --wait: on its first start the PHP container waits for the database to be made, longer than its health check allows
|
|
# --build: a recipe may have changed its Dockerfile (Doctrine adds pdo_pgsql there); Docker's cache keeps it quick
|
|
dunglas_compose "$d" up -d --build || die "the site did not start (docker compose of symfony-docker)"
|
|
local i
|
|
for i in $(seq 1 180); do
|
|
curl -s -o /dev/null --max-time 2 "http://localhost:$port/" && break
|
|
sleep 1
|
|
done
|
|
trap 'echo; say "stopping"; dunglas_compose "$d" stop >/dev/null 2>&1; exit 0' INT TERM
|
|
info "$d"
|
|
echo "[web] open http://localhost:$port"
|
|
dunglas_compose "$d" logs -f --tail 20 &
|
|
wait $!
|
|
}
|
|
|
|
# the site's own compose stack: started as it is, its PHP service named in .jumphp/web.json
|
|
serve_own() {
|
|
local d="$1" svc p url=""
|
|
svc="$(own_service "$d")"
|
|
say "site in $d with its own compose stack (PHP: the service $svc)"
|
|
own_compose "$d" up -d --build || die "the site did not start (docker compose up)"
|
|
trap 'echo; say "stopping"; own_compose "$d" stop >/dev/null 2>&1; exit 0' INT TERM
|
|
for p in 80 8080 8000 443; do
|
|
url="$(own_compose "$d" port "$svc" "$p" 2>/dev/null | tail -1)"
|
|
[ -n "$url" ] && { url="http://localhost:${url##*:}"; break; }
|
|
done
|
|
info "$d"
|
|
[ -n "$url" ] && echo "[web] open $url"
|
|
own_compose "$d" logs -f --tail 20 &
|
|
wait $!
|
|
}
|
|
|
|
serve() {
|
|
local d="$1" kind root port db="" assets project file php
|
|
if is_dunglas "$d"; then serve_dunglas "$d"; return; fi
|
|
if is_own "$d"; then serve_own "$d"; return; fi
|
|
kind="$(kind_of "$d")"; root="$(docroot_of "$kind" "$d")"; port="$(port_of "$d")"
|
|
db="$(db_of "$kind" "$d")"
|
|
assets="$(assets_command "$d" || true)"
|
|
php="$(php_of "$d")"
|
|
ensure_image "$php"
|
|
compose_file "$d" "$kind" "$root" "$port" "$db" "$assets" "$php"
|
|
project="$(name_of "$d")"
|
|
say "$kind site in $d (PHP $php, document root: $root, database: $db)$([ -n "$assets" ] && echo ", assets: $assets")$(override_merged "$d" && echo ", with compose.override.yaml")"
|
|
stack_compose "$d" up -d --remove-orphans || die "the site did not start (docker compose)"
|
|
trap 'echo; say "stopping"; stack_compose "$d" stop >/dev/null 2>&1; exit 0' INT TERM
|
|
# the site answers: the browser can open it
|
|
local i
|
|
for i in $(seq 1 120); do
|
|
if docker run --rm --network "${project}_default" curlimages/curl:8.10.1 -s -o /dev/null --max-time 2 http://$([ "$(web_json_get "$d" live)" = off ] && echo app:8080 || echo reload:$port)/ 2>/dev/null; then break; fi
|
|
sleep 1
|
|
done
|
|
info "$d"
|
|
[ "$db" = mariadb ] && [ "$kind" = wordpress ] && echo "WordPress: the installer asks the database: name app, user app, password app, host db"
|
|
echo "[web] open http://localhost:$port"
|
|
stack_compose "$d" logs -f --tail 20 &
|
|
wait $!
|
|
}
|
|
|
|
# a change of the stack applied: the generated file written again, the containers whose settings changed made again
|
|
up() {
|
|
local d="$1" kind root port db assets php
|
|
if is_dunglas "$d"; then dunglas_compose "$d" up -d --build --remove-orphans || die "docker compose up failed"; say "applied (symfony-docker)"; return; fi
|
|
if is_own "$d"; then own_compose "$d" up -d --build --remove-orphans || die "docker compose up failed"; say "applied (its own compose stack)"; return; fi
|
|
kind="$(kind_of "$d")"; root="$(docroot_of "$kind" "$d")"; port="$(port_of "$d")"; db="$(db_of "$kind" "$d")"
|
|
assets="$(assets_command "$d" || true)"
|
|
php="$(php_of "$d")"
|
|
ensure_image "$php"
|
|
compose_file "$d" "$kind" "$root" "$port" "$db" "$assets" "$php"
|
|
stack_compose "$d" up -d --remove-orphans || die "docker compose up failed"
|
|
say "applied$(override_merged "$d" && echo " (with compose.override.yaml)"): http://localhost:$port"
|
|
}
|
|
|
|
# the stack as docker compose reads it (merged); a mistake is reported with its file
|
|
config() {
|
|
local d="$1" kind
|
|
if is_dunglas "$d"; then dunglas_compose "$d" config; return; fi
|
|
if is_own "$d"; then own_compose "$d" config; return; fi
|
|
kind="$(kind_of "$d")"
|
|
compose_file "$d" "$kind" "$(docroot_of "$kind" "$d")" "$(port_of "$d")" "$(db_of "$kind" "$d")" "$(assets_command "$d" || true)" "$(php_of "$d")"
|
|
stack_compose "$d" config
|
|
}
|
|
|
|
stop() {
|
|
local d="$1"
|
|
if is_dunglas "$d"; then dunglas_compose "$d" stop; return; fi
|
|
if is_own "$d"; then own_compose "$d" stop; return; fi
|
|
[ -f "$d/.jumphp/compose.yaml" ] || { say "not started"; return; }
|
|
stack_compose "$d" stop
|
|
}
|
|
|
|
# symfony-docker (https://github.com/dunglas/symfony-docker): its files (Dockerfile, compose*.yaml, frankenphp/), from its newest
|
|
# version; into an empty folder (a new project: its first start makes the Symfony project) or an existing Symfony project
|
|
dunglas_files() {
|
|
local d="$1" tmp
|
|
tmp="$(mktemp -d)"
|
|
docker run --rm --user "$(id -u):$(id -g)" -v "$tmp:/out" curlimages/curl:8.10.1 -fsSL -o /out/sd.tar.gz https://codeload.github.com/dunglas/symfony-docker/tar.gz/refs/heads/main \
|
|
|| die "symfony-docker could not be downloaded"
|
|
mkdir -p "$tmp/sd" && tar -xzf "$tmp/sd.tar.gz" -C "$tmp/sd" --strip-components=1
|
|
rm -rf "$tmp/sd/.git" "$tmp/sd/LICENSE" "$tmp/sd/README.md" "$tmp/sd/docs" "$tmp/sd/.github"
|
|
# an existing project keeps its own files: compose*.yaml of the project are kept aside (symfony-docker's go in)
|
|
local f
|
|
for f in compose.yaml compose.override.yaml compose.prod.yaml; do
|
|
if [ -f "$d/$f" ] && [ -f "$tmp/sd/$f" ] && ! cmp -s "$d/$f" "$tmp/sd/$f"; then mv "$d/$f" "$d/$f.before-symfony-docker"; say "$f kept as $f.before-symfony-docker (merge its services by hand if needed)"; fi
|
|
done
|
|
cp -an "$tmp/sd/." "$d/"
|
|
cp -a "$tmp/sd/Dockerfile" "$tmp/sd/frankenphp" "$tmp/sd/compose.yaml" "$tmp/sd/compose.override.yaml" "$tmp/sd/compose.prod.yaml" "$d/" 2>/dev/null || true
|
|
rm -rf "$tmp"
|
|
web_json_set "$d" docker dunglas
|
|
}
|
|
|
|
# the files the containers of symfony-docker made as root become the user's again
|
|
dunglas_chown() { local d="$1"; docker run --rm -v "$d:/app" alpine:3.20 chown -R "$(id -u):$(id -g)" /app; }
|
|
|
|
# symfony-docker with MariaDB instead of the PostgreSQL its Doctrine recipe writes (as its docs/mysql.md says): the database service
|
|
# (between the recipe's markers of compose.yaml), its port in compose.override.yaml, pdo_mysql in the Dockerfile, DATABASE_URL
|
|
dunglas_mariadb() {
|
|
local d="$1" svc
|
|
svc="$(mktemp)"
|
|
cat > "$svc" <<'YAML'
|
|
database:
|
|
image: mariadb:${MARIADB_VERSION:-11.4}
|
|
environment:
|
|
MARIADB_DATABASE: ${MARIADB_DATABASE:-app}
|
|
# You should definitely change the passwords in production
|
|
MARIADB_USER: ${MARIADB_USER:-app}
|
|
MARIADB_PASSWORD: ${MARIADB_PASSWORD:-!ChangeMe!}
|
|
MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD:-!ChangeMeRoot!}
|
|
healthcheck:
|
|
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
|
|
timeout: 5s
|
|
retries: 10
|
|
start_period: 60s
|
|
volumes:
|
|
- database_data:/var/lib/mysql:rw
|
|
YAML
|
|
if [ -f "$d/compose.yaml" ] && grep -q 'image: postgres' "$d/compose.yaml"; then
|
|
# the first doctrine block that holds the postgres service: replaced; the others (the volume) kept
|
|
awk -v svc="$svc" '
|
|
/###> doctrine\/doctrine-bundle ###/ && !done { inblock = 1; buf = $0 "\n"; next }
|
|
inblock && /###< doctrine\/doctrine-bundle ###/ {
|
|
if (buf ~ /image: postgres/) { printf "%s", "###> doctrine/doctrine-bundle ###\n"; while ((getline l < svc) > 0) print l; done = 1 }
|
|
else printf "%s", buf
|
|
print; inblock = 0; next }
|
|
inblock { buf = buf $0 "\n"; next }
|
|
{ print }' "$d/compose.yaml" > "$d/compose.yaml.new" && mv "$d/compose.yaml.new" "$d/compose.yaml"
|
|
fi
|
|
rm -f "$svc"
|
|
sed -i -E 's#^([[:space:]]*DATABASE_URL:).*#\1 mysql://${MARIADB_USER:-app}:${MARIADB_PASSWORD:-!ChangeMe!}@database:3306/${MARIADB_DATABASE:-app}?serverVersion=${MARIADB_VERSION:-11.4}.0-MariaDB\&charset=utf8mb4#' "$d/compose.yaml"
|
|
[ -f "$d/compose.override.yaml" ] && sed -i 's/"5432"/"3306"/; s/- 5432/- 3306/' "$d/compose.override.yaml"
|
|
[ -f "$d/Dockerfile" ] && sed -i 's/pdo_pgsql/pdo_mysql/' "$d/Dockerfile"
|
|
[ -f "$d/.env" ] && sed -i -E 's#^DATABASE_URL="postgresql://[^"]*"#DATABASE_URL="mysql://app:!ChangeMe!@database:3306/app?serverVersion=11.4.0-MariaDB\&charset=utf8mb4"#' "$d/.env"
|
|
say "symfony-docker: MariaDB ($(grep -c 'image: mariadb' "$d/compose.yaml" 2>/dev/null || echo 0) service) instead of PostgreSQL"
|
|
}
|
|
|
|
new_dunglas() {
|
|
local d="$1" db="$2" php="${3:-}"
|
|
say "Symfony with symfony-docker (dunglas): its image is built, then its first start makes the project (a few minutes)"
|
|
dunglas_files "$d"
|
|
[ -z "$php" ] || dunglas_set_php "$d" "$php"
|
|
dunglas_compose "$d" build --pull || die "the symfony-docker image did not build"
|
|
dunglas_compose "$d" up -d --wait || die "the symfony-docker stack did not start"
|
|
# the first start wrote composer.json and the skeleton: the web app pack, like a JumPHP Symfony site
|
|
dunglas_compose "$d" exec -T php composer require --no-interaction webapp || true
|
|
case "$db" in
|
|
postgres) dunglas_compose "$d" exec -T php composer require --no-interaction symfony/orm-pack || true # its recipe adds the database service
|
|
dunglas_compose "$d" build || true ;; # and pdo_pgsql to its Dockerfile
|
|
sqlite) printf 'DATABASE_URL="%s"\n' "$(db_url sqlite)" >> "$d/.env.local" ;;
|
|
mariadb) dunglas_compose "$d" exec -T php composer require --no-interaction symfony/orm-pack || true # the recipe writes PostgreSQL...
|
|
dunglas_mariadb "$d" # ...turned into MariaDB here
|
|
dunglas_compose "$d" build || true ;;
|
|
esac
|
|
dunglas_compose "$d" stop || true
|
|
dunglas_chown "$d"
|
|
web_json_set "$d" db "$db"
|
|
}
|
|
|
|
# an existing Symfony site moved to symfony-docker
|
|
dockerize() {
|
|
local d="$1"
|
|
[ "$(kind_of "$d")" = symfony ] || [ -f "$d/bin/console" ] || die "$d is not a Symfony site"
|
|
is_dunglas "$d" && { say "$d already uses symfony-docker"; return; }
|
|
local php; php="$(php_of "$d")"
|
|
[ -f "$d/.jumphp/compose.yaml" ] && stack_compose "$d" stop >/dev/null 2>&1 || true
|
|
if has_dunglas_files "$d"; then web_json_set "$d" docker dunglas; say "symfony-docker's files were already there: used again"
|
|
else dunglas_files "$d"; fi
|
|
# the same PHP as before: its Dockerfile on it
|
|
[ -n "$php" ] && dunglas_set_php "$d" "$php"
|
|
say "done: $d now runs in symfony-docker's Dockerfile and compose.yaml (PHP ${php:-of its Dockerfile}). Start it: bin/jumphp web:serve $d (the first start builds its image)"
|
|
}
|
|
|
|
# a new site in an empty (or new) folder
|
|
new() {
|
|
local kind="$1" d="$2" db="${3:-}" docker="${4:-}" php="${5:-}"
|
|
[ -z "$php" ] || offered "$php" || die "PHP $php is not offered: $WEB_PHP_VERSIONS"
|
|
mkdir -p "$d"; d="$(dir_of "$d")"
|
|
[ -z "$(ls -A "$d" 2>/dev/null | grep -v '^\.git$\|^\.jumphp$' || true)" ] || die "$d is not empty"
|
|
local run=(docker run --rm -i --user "$(id -u):$(id -g)" -e HOME=/tmp -e COMPOSER_HOME=/tmp/composer -v "$d:/app" -w /app)
|
|
# the database: asked, else the kind's own (a WordPress / PrestaShop needs MariaDB, a plain PHP site none)
|
|
if [ -z "$db" ]; then case "$kind" in php) db=none ;; *) db=mariadb ;; esac; fi
|
|
if [ "$docker" = dunglas ]; then
|
|
[ "$kind" = symfony ] || die "--docker=dunglas is for Symfony"
|
|
new_dunglas "$d" "$db" "$php"
|
|
printf '{"kind": "symfony", "db": "%s", "docker": "dunglas"}\n' "$db" > "$d/.jumphp/web.json"
|
|
[ -d "$d/.git" ] || (cd "$d" && git init -q 2>/dev/null) || true
|
|
grep -qsx '/.jumphp/' "$d/.gitignore" || printf '/.jumphp/\n' >> "$d/.gitignore"
|
|
say "done: $d (Symfony, symfony-docker, database: $db). Next: bin/jumphp web:serve $d"
|
|
return
|
|
fi
|
|
# the PHP it is made with is the PHP it runs: remembered in .jumphp/web.json (bin/jumphp web:php to change it)
|
|
php="${php:-$WEB_PHP_DEFAULT}"
|
|
ensure_image "$php"
|
|
local WEB_IMAGE; WEB_IMAGE="$(web_image "$php")"
|
|
case "$kind" in
|
|
symfony)
|
|
say "Symfony (skeleton + webapp)"
|
|
"${run[@]}" "$WEB_IMAGE" sh -c 'composer create-project --no-interaction symfony/skeleton /tmp/s && cp -a /tmp/s/. /app/ && composer require --no-interaction webapp' ;;
|
|
sylius)
|
|
say "Sylius (sylius-standard: a few minutes)"
|
|
"${run[@]}" "$WEB_IMAGE" sh -c 'composer create-project --no-interaction sylius/sylius-standard /tmp/s && cp -a /tmp/s/. /app/' ;;
|
|
laravel)
|
|
say "Laravel"
|
|
"${run[@]}" "$WEB_IMAGE" sh -c 'composer create-project --no-interaction laravel/laravel /tmp/s && cp -a /tmp/s/. /app/' ;;
|
|
wordpress)
|
|
say "WordPress (the latest, from wordpress.org)"
|
|
"${run[@]}" "$WEB_IMAGE" sh -c 'curl -fsSL https://wordpress.org/latest.tar.gz | tar -xz -C /tmp && cp -a /tmp/wordpress/. /app/' ;;
|
|
prestashop)
|
|
say "PrestaShop (the latest release, from GitHub)"
|
|
"${run[@]}" "$WEB_IMAGE" sh -c 'set -e; url=$(curl -fsSL https://api.github.com/repos/PrestaShop/PrestaShop/releases/latest | grep -o "https://[^\"]*prestashop_[^\"]*\.zip" | head -1); [ -n "$url" ] || { echo "no PrestaShop release found"; exit 1; }; echo "$url"; curl -fsSL "$url" -o /tmp/ps.zip; cd /tmp && unzip -q ps.zip -d ps && cd /app && unzip -q -o /tmp/ps/prestashop.zip' ;;
|
|
php)
|
|
printf '<?php\n// %s\necho "<h1>Hello from JumPHP</h1>";\n' "$(basename "$d")" > "$d/index.php" ;;
|
|
*) die "unknown kind $kind: symfony, wordpress, prestashop, sylius, laravel or php" ;;
|
|
esac
|
|
mkdir -p "$d/.jumphp"
|
|
printf '{"kind": "%s", "php": "%s"}\n' "$kind" "$php" > "$d/.jumphp/web.json"
|
|
set_db "$d" "$db"
|
|
[ -d "$d/.git" ] || (cd "$d" && git init -q 2>/dev/null && printf '/.jumphp/\n' >> .gitignore) || true
|
|
say "done: $d ($kind, PHP $php, database: $db). Next: bin/jumphp web:serve $d"
|
|
}
|
|
|
|
# composer / bin/console: in the running site's own container when it is up (its database is reachable), else a new one
|
|
# symfony-docker / an own stack: in its running PHP container, else a throwaway one of its service (no database, no entrypoint:
|
|
# symfony-docker's waits for the database)
|
|
in_app() {
|
|
local d="$1"; shift
|
|
local tty=(-T); [ -t 0 ] && [ -t 1 ] && tty=()
|
|
if is_dunglas "$d"; then
|
|
local code=0
|
|
if [ -n "$(dunglas_compose "$d" ps -q php 2>/dev/null)" ]; then dunglas_compose "$d" exec "${tty[@]}" php "$@" || code=$?
|
|
else dunglas_compose "$d" run --rm --no-deps "${tty[@]}" --entrypoint docker-php-entrypoint php "$@" || code=$?; fi
|
|
dunglas_chown "$d" >/dev/null 2>&1 || true; return $code
|
|
fi
|
|
if is_own "$d"; then
|
|
local svc; svc="$(own_service "$d")"
|
|
if [ -n "$(own_compose "$d" ps -q "$svc" 2>/dev/null)" ]; then own_compose "$d" exec "${tty[@]}" "$svc" "$@"
|
|
else own_compose "$d" run --rm --no-deps "${tty[@]}" "$svc" "$@"; fi
|
|
return
|
|
fi
|
|
if [ -f "$d/.jumphp/compose.yaml" ] && [ -n "$(stack_compose "$d" ps -q app 2>/dev/null)" ]; then
|
|
stack_compose "$d" exec "${tty[@]}" -e COMPOSER_HOME=/tmp/composer app "$@"; return
|
|
fi
|
|
local php; php="$(php_of "$d")"
|
|
ensure_image "$php"
|
|
local it=(-i); [ -t 0 ] && [ -t 1 ] && it=(-it)
|
|
docker run --rm "${it[@]}" --user "$(id -u):$(id -g)" -e HOME=/tmp -e COMPOSER_HOME=/tmp/composer -v "$d:/app" -w /app "$(web_image "$php")" "$@"
|
|
}
|
|
|
|
# the console program of a site: bin/console (Symfony, Sylius), artisan (Laravel)
|
|
console_tool() { if [ ! -f "$1/bin/console" ] && [ -f "$1/artisan" ]; then echo artisan; else echo bin/console; fi; }
|
|
|
|
# is the site's PHP container up? its name (empty when not)
|
|
running_container() {
|
|
local d="$1" id=""
|
|
case "$(setup_of "$d")" in
|
|
dunglas) id="$(dunglas_compose "$d" ps -q php 2>/dev/null | head -1)" ;;
|
|
compose) id="$(own_compose "$d" ps -q "$(own_service "$d")" 2>/dev/null | head -1)" ;;
|
|
*) [ -f "$d/.jumphp/compose.yaml" ] && id="$(stack_compose "$d" ps -q app 2>/dev/null | head -1)" ;;
|
|
esac
|
|
[ -n "$id" ] && docker inspect -f '{{.Name}}' "$id" 2>/dev/null | sed 's#^/##'
|
|
true
|
|
}
|
|
|
|
json_str() { printf '"%s"' "$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr -d '\n\t')"; }
|
|
|
|
# live reload: `web:live <folder>` (status: the address, the browser tabs connected) / `web:live <folder> notify <file>...`
|
|
# (a saved file: the page reloads, a .css is injected, with no file event needed: what the IDE does on each save)
|
|
live() {
|
|
local d="$1" what="${2:-status}" port files="" f
|
|
shift 2 2>/dev/null || true
|
|
port="$(grep -o '"live_port": *"[0-9]*"' "$d/.jumphp/web.json" 2>/dev/null | grep -o '[0-9]*' | tail -1)"
|
|
[ -n "$port" ] || port="$(port_of "$d")"
|
|
case "$what" in
|
|
status) curl -s --max-time 2 "http://127.0.0.1:$port/__jumphp/status" || { echo '{"ok": false}'; return 1; }; echo ;;
|
|
notify)
|
|
for f in "$@"; do files="$files${files:+,}\"$(printf '%s' "$f" | sed 's/["\\]/\\&/g')\""; done
|
|
curl -s --max-time 2 -X POST -H 'Content-Type: application/json' -d "{\"files\": [$files]}" "http://127.0.0.1:$port/__jumphp/notify" || { echo '{"ok": false}'; return 1; }; echo ;;
|
|
*) die "web:live <folder> [status | notify <file>...]" ;;
|
|
esac
|
|
}
|
|
|
|
# what the IDE shows and needs: the setup, the PHP and why, its image, the running container
|
|
status() {
|
|
local d="$1" setup info php why image="" built=false container service="" running=false v avail=""
|
|
setup="$(setup_of "$d")"; info="$(php_info "$d")"; php="${info%%$'\t'*}"; why="${info#*$'\t'}"
|
|
case "$setup" in
|
|
jumphp) image="$(web_image "$php")"; image_built "$php" && built=true; service=app ;;
|
|
dunglas) service=php; built=true ;;
|
|
compose) service="$(own_service "$d")"; built=true ;;
|
|
esac
|
|
container="$(running_container "$d")"; [ -n "$container" ] && running=true
|
|
for v in $WEB_PHP_VERSIONS; do avail="${avail:+$avail, }\"$v\""; done
|
|
printf '{"setup": "%s", "php": %s, "source": %s, "setting": %s, "available": [%s], "default": "%s", "image": %s, "imageBuilt": %s, "running": %s, "container": %s, "service": %s, "compose": %s, "tool": "%s", "dunglasFiles": %s}\n' \
|
|
"$setup" "$(json_str "$php")" "$(json_str "$why")" "$(json_str "$(web_json_get "$d" php)")" "$avail" "$WEB_PHP_DEFAULT" "$(json_str "$image")" "$built" "$running" \
|
|
"$(json_str "$container")" "$(json_str "$service")" "$(json_str "$(own_file "$d")")" "$(console_tool "$d")" "$(has_dunglas_files "$d" && echo true || echo false)"
|
|
}
|
|
|
|
# web:php: the site's PHP shown, or chosen (a version, or auto: what Composer needs) and applied to its Docker setup
|
|
php_cmd() {
|
|
local d="$1" v="${2:-}" setup info
|
|
if [ -z "$v" ]; then
|
|
info="$(php_info "$d")"
|
|
echo "PHP ${info%%$'\t'*} (${info#*$'\t'})"
|
|
echo "offered: $WEB_PHP_VERSIONS (bin/jumphp web:php $d <version|auto>)"
|
|
return
|
|
fi
|
|
case "$v" in
|
|
auto|default) web_json_set "$d" php "" ;;
|
|
*) v="$(printf '%s' "$v" | sed -E 's/^php//; s/^([0-9]+\.[0-9]+).*/\1/')"
|
|
offered "$v" || die "PHP $v is not offered: $WEB_PHP_VERSIONS (the FrankenPHP images)"
|
|
web_json_set "$d" php "$v" ;;
|
|
esac
|
|
setup="$(setup_of "$d")"
|
|
# symfony-docker / an own stack: "auto" = what Composer needs, written into their files too
|
|
if [ "$setup" != jumphp ]; then case "$v" in auto|default) v="$(php_auto "$d" | cut -f1)" ;; esac; fi
|
|
case "$setup" in
|
|
jumphp) v="$(php_of "$d")"; ensure_image "$v"
|
|
if [ -n "$(running_container "$d")" ]; then up "$d"; fi ;;
|
|
dunglas) dunglas_set_php "$d" "$v"
|
|
if [ -n "$(running_container "$d")" ]; then dunglas_compose "$d" up -d --build || die "symfony-docker did not rebuild"; else say "its image is rebuilt at the next start"; fi ;;
|
|
compose) own_set_php "$d" "$v"
|
|
if [ -n "$(running_container "$d")" ]; then own_compose "$d" up -d --build || die "docker compose up failed"; fi ;;
|
|
esac
|
|
info="$(php_info "$d")"
|
|
say "PHP of $d: ${info%%$'\t'*} (${info#*$'\t'}): web:serve, web:composer and web:console all run it"
|
|
}
|
|
|
|
# web:docker: the Docker setup shown, or changed (what changes is said)
|
|
docker_cmd() {
|
|
local d="$1" to="${2:-}" from php f
|
|
from="$(setup_of "$d")"
|
|
if [ -z "$to" ]; then
|
|
echo "$from"
|
|
case "$from" in
|
|
jumphp) echo "the JumPHP stack: .jumphp/compose.yaml written by web:serve (FrankenPHP image jumphp/web:php<x.y>) + compose.override.yaml" ;;
|
|
dunglas) echo "symfony-docker: the site's own Dockerfile, compose.yaml and compose.override.yaml" ;;
|
|
compose) echo "its own compose stack ($(own_file "$d")), PHP in the service $(own_service "$d")" ;;
|
|
esac
|
|
return
|
|
fi
|
|
php="$(php_of "$d")"
|
|
case "$to" in
|
|
jumphp)
|
|
[ "$from" = jumphp ] && { say "$d already uses the JumPHP stack"; return; }
|
|
[ "$from" = dunglas ] && dunglas_compose "$d" stop >/dev/null 2>&1 || true
|
|
[ "$from" = compose ] && own_compose "$d" stop >/dev/null 2>&1 || true
|
|
# symfony-docker's override (its php service) is not for the JumPHP stack, which merges compose.override.yaml
|
|
if [ "$from" = dunglas ] && [ -f "$d/compose.override.yaml" ] && grep -qs 'frankenphp\|php:' "$d/compose.override.yaml"; then
|
|
mv "$d/compose.override.yaml" "$d/compose.override.yaml.symfony-docker"
|
|
say "compose.override.yaml (symfony-docker's) kept as compose.override.yaml.symfony-docker"
|
|
fi
|
|
web_json_set "$d" docker jumphp; web_json_set "$d" service ""; web_json_set "$d" compose ""
|
|
# the PHP it had, kept (symfony-docker's Dockerfile, an own image)
|
|
[ -n "$php" ] && [ -z "$(web_json_get "$d" php)" ] && offered "$php" && web_json_set "$d" php "$php"
|
|
[ "$(kind_of "$d")" = symfony ] && [ "$from" = dunglas ] && set_db "$d" "$(db_of symfony "$d")"
|
|
say "done: the JumPHP stack (FrankenPHP, PHP $(php_of "$d"), database host db). symfony-docker's Dockerfile, compose.yaml and frankenphp/ stay in the folder, unused: bin/jumphp web:docker $d dunglas goes back" ;;
|
|
dunglas)
|
|
[ -f "$d/bin/console" ] || die "symfony-docker is for Symfony sites"
|
|
[ "$from" = compose ] && own_compose "$d" stop >/dev/null 2>&1 || true
|
|
if [ -f "$d/compose.override.yaml.symfony-docker" ]; then
|
|
[ -f "$d/compose.override.yaml" ] && mv "$d/compose.override.yaml" "$d/compose.override.yaml.jumphp" && say "compose.override.yaml (the JumPHP stack's) kept as compose.override.yaml.jumphp"
|
|
mv "$d/compose.override.yaml.symfony-docker" "$d/compose.override.yaml"
|
|
fi
|
|
web_json_set "$d" service ""; web_json_set "$d" compose ""
|
|
if has_dunglas_files "$d"; then
|
|
[ -f "$d/.jumphp/compose.yaml" ] && stack_compose "$d" stop >/dev/null 2>&1 || true
|
|
web_json_set "$d" docker dunglas; [ -n "$php" ] && dunglas_set_php "$d" "$php"
|
|
say "done: symfony-docker again (its Dockerfile, PHP ${php:-of its Dockerfile}; database: its compose.yaml's)"
|
|
else
|
|
web_json_set "$d" docker ""; dockerize "$d"
|
|
fi ;;
|
|
compose)
|
|
local svc="${3:-}" file="${4:-}"
|
|
[ -n "$file" ] && [ ! -f "$d/$file" ] && die "no file $file in $d"
|
|
web_json_set "$d" compose "$file"
|
|
[ -n "$(own_file "$d")" ] || die "$d has no compose.yaml / docker-compose.yml: name the file (bin/jumphp web:docker $d compose <service> <file>)"
|
|
if [ -z "$svc" ]; then
|
|
svc="$(own_compose "$d" config --services 2>/dev/null | grep -m1 -iE '^(php|app|php-fpm|fpm|web|frankenphp)$' || true)"
|
|
[ -n "$svc" ] || die "which service runs PHP? bin/jumphp web:docker $d compose <service> ($(own_compose "$d" config --services 2>/dev/null | tr '\n' ' '))"
|
|
elif ! own_compose "$d" config --services 2>/dev/null | grep -qx "$svc"; then
|
|
die "no service $svc in $(own_file "$d") ($(own_compose "$d" config --services 2>/dev/null | tr '\n' ' '))"
|
|
fi
|
|
[ "$from" = jumphp ] && [ -f "$d/.jumphp/compose.yaml" ] && stack_compose "$d" stop >/dev/null 2>&1 || true
|
|
[ "$from" = dunglas ] && dunglas_compose "$d" stop >/dev/null 2>&1 || true
|
|
web_json_set "$d" docker compose; web_json_set "$d" service "$svc"
|
|
# its image decides the PHP from now on (web:php <x.y> rewrites its tag)
|
|
[ "$from" = compose ] || web_json_set "$d" php ""
|
|
say "done: $(own_file "$d") is the site's stack, PHP runs in its service $svc (web:serve = docker compose up, web:console / web:composer = exec there)" ;;
|
|
*) die "web:docker <folder> jumphp | dunglas | compose [service] [file]" ;;
|
|
esac
|
|
}
|
|
|
|
# options of web:new: --db=... --docker=dunglas, anywhere after the kind
|
|
[ "${WEB_SH_LIB:-}" = 1 ] && return 0 # tools/tests/web.test.sh: the functions only
|
|
|
|
opt_db=""; opt_docker=""; opt_php=""; opt_json=""; args=()
|
|
for a in "$@"; do
|
|
case "$a" in
|
|
--db=*) opt_db="${a#--db=}" ;;
|
|
--docker=*) opt_docker="${a#--docker=}" ;;
|
|
--php=*) opt_php="${a#--php=}" ;;
|
|
--json) opt_json=1 ;;
|
|
*) args+=("$a") ;;
|
|
esac
|
|
done
|
|
set -- "${args[@]+"${args[@]}"}"
|
|
|
|
cmd="${1:-}"; shift || true
|
|
case "$cmd" in
|
|
new) [ $# -ge 2 ] || die "usage: bin/jumphp web:new <symfony|wordpress|prestashop|sylius|laravel|php> <folder> [--db=mariadb|postgres|sqlite|none] [--docker=dunglas] [--php=8.x]"
|
|
new "$1" "$2" "$opt_db" "$opt_docker" "$opt_php" ;;
|
|
serve) serve "$(dir_of "${1:-}")" ;;
|
|
stop) stop "$(dir_of "${1:-}")" ;;
|
|
up) up "$(dir_of "${1:-}")" ;;
|
|
config) config "$(dir_of "${1:-}")" ;;
|
|
info) info "$(dir_of "${1:-}")" ;;
|
|
db) [ $# -ge 2 ] || die "usage: bin/jumphp web:db <folder> <mariadb|postgres|sqlite|none>"; set_db "$(dir_of "$1")" "$2"; say "restart the site (web:stop, web:serve) to start its database" ;;
|
|
dockerize) dockerize "$(dir_of "${1:-}")" ;;
|
|
composer) d="$(dir_of "${1:-}")"; shift || true; in_app "$d" composer "$@" ;;
|
|
console) d="$(dir_of "${1:-}")"; shift || true; in_app "$d" php "$(console_tool "$d")" "$@" ;;
|
|
php) d="$(dir_of "${1:-}")"; shift || true
|
|
if [ -n "$opt_json" ] && [ $# -eq 0 ]; then status "$d"; else php_cmd "$d" "${1:-}"; fi ;;
|
|
docker) d="$(dir_of "${1:-}")"; shift || true; docker_cmd "$d" "$@" ;;
|
|
status) status "$(dir_of "${1:-}")" ;;
|
|
live) d="$(dir_of "${1:-}")"; shift || true; live "$d" "$@" ;;
|
|
*) die "usage: bin/jumphp web:new | web:serve | web:stop | web:up | web:config | web:info | web:db | web:dockerize | web:composer | web:console | web:php | web:docker | web:status | web:live" ;;
|
|
esac
|