Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions src/ProcessManager/WebServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/ProcessManager/WebServerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Panther project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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): ?>
<?=$_ENV['APP_ENV']; ?>
<?php else: ?>
<?=$_ENV['FOO']; ?>
<?php endif; ?>