Skip to content

Commit 8d3058b

Browse files
committed
Use PHP_INI_SCAN_DIR to more reliably inject into PHP (e.g. in Docker)
With this in place, ddev works (using nginx + php-fpm) as does the php:apache official image.
1 parent e770357 commit 8d3058b

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

overrides/path/php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ PATH="${PATH//`dirname "$0"`:/}"
66
real_php=`command -v php`
77
PATH="`dirname "$0"`:$PATH"
88

9+
# Strip out our PHP_INI_SCAN_DIR - if this file has been run succesfully, then it's not necessary,
10+
# and it can cause problems (since it overrides any default scan directories configured)
11+
export PHP_INI_SCAN_DIR="${PHP_INI_SCAN_DIR//:`echo $HTTP_TOOLKIT_OVERRIDE_PATH/php`/}"
12+
export PHP_INI_SCAN_DIR="${PHP_INI_SCAN_DIR//`echo $HTTP_TOOLKIT_OVERRIDE_PATH/php`/}"
13+
if [ -z "$PHP_INI_SCAN_DIR" ]; then
14+
unset PHP_INI_SCAN_DIR
15+
fi
16+
917
# Call PHP with the given arguments, and a few extra
1018
PHP_ARGS=(
1119
# Make OpenSSL trust us
1220
-d "openssl.cafile=$SSL_CERT_FILE" \
1321
# Make cURL trust us
1422
-d "curl.cainfo=$SSL_CERT_FILE" \
1523
# Prepend a script that enables the proxy
16-
-d "auto_prepend_file=`dirname "$0"`/prepend.php" \
24+
-d "auto_prepend_file=`dirname "$0"`/../php/prepend.php" \
1725
# Pass through all other provided arguments
1826
"$@"
1927
)

overrides/path/php.bat

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ FOR /F "tokens=*" %%g IN ('where php') do (SET REAL_PHP=%%g)
1616
REM Reset PATH, so its visible to php & subprocesses
1717
set PATH=%ORIGINALPATH%
1818

19+
REM Reset PHP_INI_SCAN_DIR, removing our override path
20+
call set PHP_INI_SCAN_DIR=%%PHP_INI_SCAN_DIR:%HTTP_TOOLKIT_OVERRIDE_PATH%\php;=%%
21+
call set PHP_INI_SCAN_DIR=%%PHP_INI_SCAN_DIR:%HTTP_TOOLKIT_OVERRIDE_PATH%\php=%%
22+
1923
REM Start PHP for real, with extra args to override certain configs
20-
"%REAL_PHP%" -d "openssl.cafile=%SSL_CERT_FILE%" -d "curl.cainfo=%SSL_CERT_FILE%" -d "auto_prepend_file=%WRAPPER_FOLDER%\prepend.php" %*
24+
"%REAL_PHP%" -d "openssl.cafile=%SSL_CERT_FILE%" -d "curl.cainfo=%SSL_CERT_FILE%" -d "auto_prepend_file=%WRAPPER_FOLDER%\..\php\prepend.php" %*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Make OpenSSL trust us
2+
openssl.cafile=${SSL_CERT_FILE}
3+
# Make cURL trust us
4+
curl.cainfo=${SSL_CERT_FILE}
5+
# Prepend a script that enables the proxy
6+
auto_prepend_file=${HTTP_TOOLKIT_OVERRIDE_PATH}/php/prepend.php
7+
8+
# Intercepting PHP using this file via PHP_INI_SCAN_DIR isn't a perfect solution. It's better
9+
# to use the 'php' wrapper (overrides/path/php) which sets this configuration, because when
10+
# PHP_INI_SCAN_DIR is left blank it defaults to a system config directory, and overriding this
11+
# means that is not loaded.
12+
# Unfortunately, it's not always possible to inject the 'php' wrapper where we need it, due to
13+
# how PHP is often launched (managed by another process, not launched & injectable by HTTP Toolkit).
14+
# This is a fallback solution for that case that seems to work well in practice.
15+
16+
# Where this doesn't work, you may be able to replace the relevant env vars above and place this
17+
# file directly into your PHP_INI_SCAN_DIR directory (run php --ini to find this).
18+
19+
# (In future, we could consider more complicated fixes: e.g. a prepend script that launches a PHP
20+
# subprocess which runs with the default configuration, just explicitly overridden by CLI args.
21+
# That would have some performance implications, but probably nothing notable in dev. Not worthwhile
22+
# for now unless this causes serious problems though)
File renamed without changes.

src/interceptors/terminal/terminal-env-overrides.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getDockerPipePath } from '../docker/docker-proxy';
55

66
const BIN_OVERRIDE_DIR = 'path';
77
const RUBY_OVERRIDE_DIR = 'gems';
8+
const PHP_OVERRIDE_DIR = 'php';
89
const PYTHON_OVERRIDE_DIR = 'pythonpath';
910
const NODE_PREPEND_SCRIPT = ['js', 'prepend-node.js'];
1011
const JAVA_AGENT_JAR = 'java-agent.jar';
@@ -61,6 +62,7 @@ export function getTerminalEnvVars(
6162

6263
const rubyGemsPath = joinPath(overridePath, RUBY_OVERRIDE_DIR);
6364
const pythonPath = joinPath(overridePath, PYTHON_OVERRIDE_DIR);
65+
const phpPath = joinPath(overridePath, PHP_OVERRIDE_DIR);
6466
const nodePrependScript = joinPath(overridePath, ...NODE_PREPEND_SCRIPT);
6567
const nodePrependOption = `--require ${
6668
// Avoid quoting except when necessary, because node 8 doesn't support quotes here
@@ -107,6 +109,8 @@ export function getTerminalEnvVars(
107109

108110
// Flag used by subprocesses to check they're running in an intercepted env
109111
'HTTP_TOOLKIT_ACTIVE': 'true',
112+
// Useful downstream to derive the raw paths elsewhere, e.g. in php.ini override config.
113+
'HTTP_TOOLKIT_OVERRIDE_PATH': overridePath,
110114

111115
// Prepend our bin overrides into $PATH
112116
'PATH': `${binPath}${pathVarSeparator}${
@@ -141,6 +145,12 @@ export function getTerminalEnvVars(
141145
? `${currentEnv.JAVA_TOOL_OPTIONS} ${javaAgentOption}`
142146
: javaAgentOption,
143147

148+
'PHP_INI_SCAN_DIR': runtimeInherit
149+
? `${runtimeInherit('PHP_INI_SCAN_DIR')}${pathVarSeparator}${phpPath}`
150+
: currentEnv.PHP_INI_SCAN_DIR
151+
? `${currentEnv.PHP_INI_SCAN_DIR}${pathVarSeparator}${phpPath}`
152+
: phpPath,
153+
144154
// Run all Docker operations through our Docker-hooking proxy:
145155
'DOCKER_HOST': dockerHost,
146156
// For now, we don't support intercepting BuildKit builds - disable them:

0 commit comments

Comments
 (0)