diff --git a/src/ProcessManager/WebServerManager.php b/src/ProcessManager/WebServerManager.php index 8351764f..bf63259e 100644 --- a/src/ProcessManager/WebServerManager.php +++ b/src/ProcessManager/WebServerManager.php @@ -35,7 +35,7 @@ final class WebServerManager /** * @throws \RuntimeException */ - public function __construct(string $documentRoot, string $hostname, int $port, string $router = '', string $readinessPath = '') + public function __construct(string $documentRoot, string $hostname, int $port, string $router = '', string $readinessPath = '', array $env = null) { $this->hostname = $hostname; $this->port = $port; @@ -46,9 +46,11 @@ public function __construct(string $documentRoot, string $hostname, int $port, s throw new \RuntimeException('Unable to find the PHP binary.'); } - $env = null; if (isset($_SERVER['PANTHER_APP_ENV'])) { - $env = ['APP_ENV' => $_SERVER['PANTHER_APP_ENV']]; + if (null === $env) { + $env = []; + } + $env['APP_ENV'] = $_SERVER['PANTHER_APP_ENV']; } $this->process = new Process( @@ -69,6 +71,12 @@ public function __construct(string $documentRoot, string $hostname, int $port, s null, null ); + + // Symfony Process 3.4 BC: In newer versions env variables always inherit, + // but in 4.4 inheritEnvironmentVariables is deprecated, but setOptions was removed + if (\is_callable([$this->process, 'inheritEnvironmentVariables']) && \is_callable([$this->process, 'setOptions'])) { + $this->process->inheritEnvironmentVariables(true); + } } public function start(): void diff --git a/tests/ProcessManager/WebServerManagerTest.php b/tests/ProcessManager/WebServerManagerTest.php index 1efc2a91..5e70fae6 100644 --- a/tests/ProcessManager/WebServerManagerTest.php +++ b/tests/ProcessManager/WebServerManagerTest.php @@ -45,4 +45,33 @@ public function testAlreadyRunning() $server1->quit(); } } + + public function testPassEnv() + { + $server = new WebServerManager(__DIR__.'/../fixtures/', '127.0.0.1', 1234, '', '', ['FOO' => 'bar']); + $server->start(); + $this->assertStringContainsString('bar', (string) file_get_contents('http://127.0.0.1:1234/env.php?name=FOO')); + + $server->quit(); + } + + public function testPassPantherAppEnv() + { + $value = $_SERVER['PANTHER_APP_ENV'] ?? null; // store app env + + $_SERVER['PANTHER_APP_ENV'] = 'dev'; + $server = new WebServerManager(__DIR__.'/../fixtures/', '127.0.0.1', 1234); + $server->start(); + $this->assertStringContainsString('dev', (string) file_get_contents('http://127.0.0.1:1234/env.php?name=APP_ENV')); + + $server->quit(); + + // restore app env + if (null === $value) { + unset($_SERVER['PANTHER_APP_ENV']); + + return; + } + $_SERVER['PANTHER_APP_ENV'] = $value; + } } diff --git a/tests/fixtures/env.php b/tests/fixtures/env.php new file mode 100644 index 00000000..e1bba09a --- /dev/null +++ b/tests/fixtures/env.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +if ('APP_ENV' === $_GET['name'] ?? null): ?> + + + +