Skip to content

Commit d9b40b7

Browse files
authored
Merge pull request symfony#106 from maidmaid/router
Add support of router script
2 parents 5f94cec + 2c53f85 commit d9b40b7

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ The following environment variables can be set to change some Panther behaviors:
217217
* `PANTHER_CHROME_DRIVER_BINARY`: to use another `chromedriver` binary, instead of relying on the ones already provided by Panther
218218
* `PANTHER_CHROME_ARGUMENTS`: to customize `chromedriver` arguments. You need to set `PANTHER_NO_HEADLESS` to fully customize.
219219
* `PANTHER_WEB_SERVER_PORT`: to change the web server's port (default to `9000`)
220+
* `PANTHER_WEB_SERVER_ROUTER`: to use a web server router script which is run at the start of each HTTP request
220221

221222
## Docker Integration
222223

src/PantherTestCaseTrait.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ trait PantherTestCaseTrait
5656
*/
5757
protected static $pantherClient;
5858

59+
/**
60+
* @var array
61+
*/
62+
protected static $defaultOptions = [
63+
'webServerDir' => __DIR__.'/../../../../public', // the Flex directory structure
64+
'hostname' => '127.0.0.1',
65+
'port' => 9000,
66+
'router' => '',
67+
];
68+
5969
public static function tearDownAfterClass()
6070
{
6171
if (self::$stopServerOnTeardown) {
@@ -82,28 +92,35 @@ public static function stopWebServer()
8292
self::$baseUri = null;
8393
}
8494

85-
public static function startWebServer(?string $webServerDir = null, string $hostname = '127.0.0.1', int $port = 9000): void
95+
/**
96+
* @param array $options see {@see $defaultOptions}
97+
*/
98+
public static function startWebServer(array $options = []): void
8699
{
87100
if (null !== static::$webServerManager) {
88101
return;
89102
}
90103

91-
if (null === $webServerDir) {
92-
// Try the local $webServerDir property, or the PANTHER_WEB_SERVER_DIR env var or default to the Flex directory structure
93-
$webServerDir = static::$webServerDir ?? $_SERVER['PANTHER_WEB_SERVER_DIR'] ?? __DIR__.'/../../../../public';
94-
}
104+
$options = [
105+
'webServerDir' => $options['webServerDir'] ?? static::$webServerDir ?? $_SERVER['PANTHER_WEB_SERVER_DIR'] ?? self::$defaultOptions['webServerDir'],
106+
'hostname' => $options['webServerDir'] ?? self::$defaultOptions['hostname'],
107+
'port' => (int) ($options['port'] ?? $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? self::$defaultOptions['port']),
108+
'router' => $options['router'] ?? $_SERVER['PANTHER_WEB_SERVER_ROUTER'] ?? self::$defaultOptions['router'],
109+
];
95110

96-
self::$webServerManager = new WebServerManager($webServerDir, $hostname, $port);
111+
self::$webServerManager = new WebServerManager(...array_values($options));
97112
self::$webServerManager->start();
98113

99-
self::$baseUri = "http://$hostname:$port";
114+
self::$baseUri = sprintf('http://%s:%s', $options['hostname'], $options['port']);
100115
}
101116

102-
protected static function createPantherClient(string $hostname = '127.0.0.1', ?int $port = null, array $kernelOptions = []): PantherClient
117+
/**
118+
* @param array $options see {@see $defaultOptions}
119+
* @param array $kernelOptions
120+
*/
121+
protected static function createPantherClient(array $options = [], array $kernelOptions = []): PantherClient
103122
{
104-
$port = (int) ($port ?? $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? 9000);
105-
106-
self::startWebServer(null, $hostname, $port);
123+
self::startWebServer($options);
107124
if (null === self::$pantherClient) {
108125
self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);
109126
}
@@ -115,15 +132,17 @@ protected static function createPantherClient(string $hostname = '127.0.0.1', ?i
115132
return self::$pantherClient;
116133
}
117134

118-
protected static function createGoutteClient(string $hostname = '127.0.0.1', ?int $port = null, array $kernelOptions = []): GoutteClient
135+
/**
136+
* @param array $options see {@see $defaultOptions}
137+
* @param array $kernelOptions
138+
*/
139+
protected static function createGoutteClient(array $options = [], array $kernelOptions = []): GoutteClient
119140
{
120141
if (!\class_exists(GoutteClient::class)) {
121142
throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".');
122143
}
123144

124-
$port = (int) ($port ?? $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? 9000);
125-
126-
self::startWebServer(null, $hostname, $port);
145+
self::startWebServer($options);
127146
if (null === self::$goutteClient) {
128147
$goutteClient = new GoutteClient();
129148
$goutteClient->setClient(new GuzzleClient(['base_uri' => self::$baseUri]));

src/ProcessManager/WebServerManager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class WebServerManager
3434
/**
3535
* @throws \RuntimeException
3636
*/
37-
public function __construct(string $documentRoot, string $hostname, int $port)
37+
public function __construct(string $documentRoot, string $hostname, int $port, string $router = '')
3838
{
3939
$this->hostname = $hostname;
4040
$this->port = $port;
@@ -45,7 +45,7 @@ public function __construct(string $documentRoot, string $hostname, int $port)
4545
}
4646

4747
$this->process = new Process(
48-
array_merge(
48+
array_filter(array_merge(
4949
[$binary],
5050
$finder->findArguments(),
5151
[
@@ -54,8 +54,9 @@ public function __construct(string $documentRoot, string $hostname, int $port)
5454
sprintf('%s:%d', $this->hostname, $this->port),
5555
'-t',
5656
$documentRoot,
57+
$router,
5758
]
58-
),
59+
)),
5960
$documentRoot,
6061
null,
6162
null,

0 commit comments

Comments
 (0)