Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e14b6e

Browse files
jschaedlfabpot
authored andcommitted
[WebServerBundle] Change the default pidfile location to cache directory
1 parent 77f642e commit 2e14b6e

File tree

8 files changed

+78
-7
lines changed

8 files changed

+78
-7
lines changed

src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ class ServerRunCommand extends Command
3131
{
3232
private $documentRoot;
3333
private $environment;
34+
private $pidFileDirectory;
3435

3536
protected static $defaultName = 'server:run';
3637

37-
public function __construct(string $documentRoot = null, string $environment = null)
38+
public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
3839
{
3940
$this->documentRoot = $documentRoot;
4041
$this->environment = $environment;
42+
$this->pidFileDirectory = $pidFileDirectory;
4143

4244
parent::__construct();
4345
}
@@ -129,7 +131,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
129131
}
130132

131133
try {
132-
$server = new WebServer();
134+
$server = new WebServer($this->pidFileDirectory);
133135
$config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));
134136

135137
$message = sprintf('Server listening on http://%s', $config->getAddress());

src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ class ServerStartCommand extends Command
3131
{
3232
private $documentRoot;
3333
private $environment;
34+
private $pidFileDirectory;
3435

3536
protected static $defaultName = 'server:start';
3637

37-
public function __construct(string $documentRoot = null, string $environment = null)
38+
public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
3839
{
3940
$this->documentRoot = $documentRoot;
4041
$this->environment = $environment;
42+
$this->pidFileDirectory = $pidFileDirectory;
4143

4244
parent::__construct();
4345
}
@@ -133,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
133135
$this->getApplication()->setDispatcher(new EventDispatcher());
134136

135137
try {
136-
$server = new WebServer();
138+
$server = new WebServer($this->pidFileDirectory);
137139
if ($server->isRunning($input->getOption('pidfile'))) {
138140
$io->error(sprintf('The web server has already been started. It is currently listening on http://%s. Please stop the web server before you try to start it again.', $server->getAddress($input->getOption('pidfile'))));
139141

src/Symfony/Bundle/WebServerBundle/Command/ServerStatusCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class ServerStatusCommand extends Command
3030
{
3131
protected static $defaultName = 'server:status';
3232

33+
private $pidFileDirectory;
34+
35+
public function __construct(string $pidFileDirectory = null)
36+
{
37+
$this->pidFileDirectory = $pidFileDirectory;
38+
39+
parent::__construct();
40+
}
41+
3342
/**
3443
* {@inheritdoc}
3544
*/
@@ -64,7 +73,7 @@ protected function configure()
6473
protected function execute(InputInterface $input, OutputInterface $output)
6574
{
6675
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
67-
$server = new WebServer();
76+
$server = new WebServer($this->pidFileDirectory);
6877
if ($filter = $input->getOption('filter')) {
6978
if ($server->isRunning($input->getOption('pidfile'))) {
7079
list($host, $port) = explode(':', $address = $server->getAddress($input->getOption('pidfile')));

src/Symfony/Bundle/WebServerBundle/Command/ServerStopCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class ServerStopCommand extends Command
2828
{
2929
protected static $defaultName = 'server:stop';
3030

31+
private $pidFileDirectory;
32+
33+
public function __construct(string $pidFileDirectory = null)
34+
{
35+
$this->pidFileDirectory = $pidFileDirectory;
36+
37+
parent::__construct();
38+
}
39+
3140
/**
3241
* {@inheritdoc}
3342
*/
@@ -55,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5564
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
5665

5766
try {
58-
$server = new WebServer();
67+
$server = new WebServer($this->pidFileDirectory);
5968
$server->stop($input->getOption('pidfile'));
6069
$io->success('Stopped the web server.');
6170
} catch (\Exception $e) {

src/Symfony/Bundle/WebServerBundle/DependencyInjection/WebServerExtension.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public function load(array $configs, ContainerBuilder $container)
3131
$container->getDefinition('web_server.command.server_run')->replaceArgument(0, $publicDirectory);
3232
$container->getDefinition('web_server.command.server_start')->replaceArgument(0, $publicDirectory);
3333

34+
$pidFileDirectory = $this->getPidFileDirectory($container);
35+
$container->getDefinition('web_server.command.server_run')->replaceArgument(2, $pidFileDirectory);
36+
$container->getDefinition('web_server.command.server_start')->replaceArgument(2, $pidFileDirectory);
37+
$container->getDefinition('web_server.command.server_stop')->replaceArgument(0, $pidFileDirectory);
38+
$container->getDefinition('web_server.command.server_status')->replaceArgument(0, $pidFileDirectory);
39+
3440
if (!class_exists(ConsoleFormatter::class)) {
3541
$container->removeDefinition('web_server.command.server_log');
3642
}
@@ -54,4 +60,16 @@ private function getPublicDirectory(ContainerBuilder $container)
5460

5561
return $kernelProjectDir.'/'.$publicDir;
5662
}
63+
64+
private function getPidFileDirectory(ContainerBuilder $container): string
65+
{
66+
$kernelCacheDir = $container->getParameter('kernel.cache_dir');
67+
$environment = $container->getParameter('kernel.environment');
68+
69+
if (basename($kernelCacheDir) !== $environment) {
70+
return $container->getParameter('kernel.project_dir');
71+
}
72+
73+
return \dirname($container->getParameter('kernel.cache_dir'));
74+
}
5775
}

src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@
1010
<service id="web_server.command.server_run" class="Symfony\Bundle\WebServerBundle\Command\ServerRunCommand">
1111
<argument>%kernel.project_dir%/public</argument>
1212
<argument>%kernel.environment%</argument>
13+
<argument>%kernel.project_dir%/var/cache</argument>
1314
<tag name="console.command" command="server:run" />
1415
</service>
1516

1617
<service id="web_server.command.server_start" class="Symfony\Bundle\WebServerBundle\Command\ServerStartCommand">
1718
<argument>%kernel.project_dir%/public</argument>
1819
<argument>%kernel.environment%</argument>
20+
<argument>%kernel.project_dir%/var/cache</argument>
1921
<tag name="console.command" command="server:start" />
2022
</service>
2123

2224
<service id="web_server.command.server_stop" class="Symfony\Bundle\WebServerBundle\Command\ServerStopCommand">
25+
<argument>%kernel.project_dir%/var/cache</argument>
2326
<tag name="console.command" command="server:stop" />
2427
</service>
2528

2629
<service id="web_server.command.server_status" class="Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand">
30+
<argument>%kernel.project_dir%/var/cache</argument>
2731
<tag name="console.command" command="server:status" />
2832
</service>
2933

src/Symfony/Bundle/WebServerBundle/Tests/DependencyInjection/WebServerExtensionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function testLoad()
2222
{
2323
$container = new ContainerBuilder();
2424
$container->setParameter('kernel.project_dir', __DIR__);
25+
$container->setParameter('kernel.cache_dir', __DIR__.'/var/cache/test');
26+
$container->setParameter('kernel.environment', 'test');
2527
(new WebServerExtension())->load([], $container);
2628

2729
$this->assertSame(
@@ -32,6 +34,24 @@ public function testLoad()
3234
__DIR__.'/test',
3335
$container->getDefinition('web_server.command.server_start')->getArgument(0)
3436
);
37+
38+
$this->assertSame(
39+
__DIR__.'/var/cache',
40+
$container->getDefinition('web_server.command.server_run')->getArgument(2)
41+
);
42+
$this->assertSame(
43+
__DIR__.'/var/cache',
44+
$container->getDefinition('web_server.command.server_start')->getArgument(2)
45+
);
46+
$this->assertSame(
47+
__DIR__.'/var/cache',
48+
$container->getDefinition('web_server.command.server_stop')->getArgument(0)
49+
);
50+
$this->assertSame(
51+
__DIR__.'/var/cache',
52+
$container->getDefinition('web_server.command.server_status')->getArgument(0)
53+
);
54+
3555
$this->assertTrue($container->hasDefinition('web_server.command.server_run'));
3656
$this->assertTrue($container->hasDefinition('web_server.command.server_start'));
3757
$this->assertTrue($container->hasDefinition('web_server.command.server_stop'));

src/Symfony/Bundle/WebServerBundle/WebServer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class WebServer
2525
const STARTED = 0;
2626
const STOPPED = 1;
2727

28+
private $pidFileDirectory;
29+
30+
public function __construct(string $pidFileDirectory = null)
31+
{
32+
$this->pidFileDirectory = $pidFileDirectory;
33+
}
34+
2835
public function run(WebServerConfig $config, $disableOutput = true, callable $callback = null)
2936
{
3037
if ($this->isRunning()) {
@@ -166,6 +173,6 @@ private function createServerProcess(WebServerConfig $config)
166173

167174
private function getDefaultPidFile()
168175
{
169-
return getcwd().'/.web-server-pid';
176+
return ($this->pidFileDirectory ?? getcwd()).'/.web-server-pid';
170177
}
171178
}

0 commit comments

Comments
 (0)