Skip to content

Commit 74bc733

Browse files
committed
[WebServerBundle] Code review changes
- fixed possible BC breaks in command constructors - changed pidFile dir to cache dir
1 parent 3aae37d commit 74bc733

File tree

8 files changed

+64
-30
lines changed

8 files changed

+64
-30
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
*/
3030
class ServerRunCommand extends Command
3131
{
32-
private $projectDir;
3332
private $documentRoot;
3433
private $environment;
34+
private $pidFileDirectory;
3535

3636
protected static $defaultName = 'server:run';
3737

38-
public function __construct(string $projectDir = null, string $documentRoot = null, string $environment = null)
38+
public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
3939
{
40-
$this->projectDir = $projectDir;
4140
$this->documentRoot = $documentRoot;
4241
$this->environment = $environment;
42+
$this->pidFileDirectory = $pidFileDirectory;
4343

4444
parent::__construct();
4545
}
@@ -131,7 +131,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
131131
}
132132

133133
try {
134-
$server = new WebServer($this->projectDir);
134+
$server = new WebServer($this->pidFileDirectory);
135135
$config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));
136136

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

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
*/
3030
class ServerStartCommand extends Command
3131
{
32-
private $projectDir;
3332
private $documentRoot;
3433
private $environment;
34+
private $pidFileDirectory;
3535

3636
protected static $defaultName = 'server:start';
3737

38-
public function __construct(string $projectDir = null, string $documentRoot = null, string $environment = null)
38+
public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
3939
{
40-
$this->projectDir = $projectDir;
4140
$this->documentRoot = $documentRoot;
4241
$this->environment = $environment;
42+
$this->pidFileDirectory = $pidFileDirectory;
4343

4444
parent::__construct();
4545
}
@@ -135,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
135135
$this->getApplication()->setDispatcher(new EventDispatcher());
136136

137137
try {
138-
$server = new WebServer($this->projectDir);
138+
$server = new WebServer($this->pidFileDirectory);
139139
if ($server->isRunning($input->getOption('pidfile'))) {
140140
$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'))));
141141

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class ServerStatusCommand extends Command
3030
{
3131
protected static $defaultName = 'server:status';
3232

33-
private $projectDir;
33+
private $pidFileDirectory;
3434

35-
public function __construct(string $projectDir = null)
35+
public function __construct(string $pidFileDirectory = null)
3636
{
37-
$this->projectDir = $projectDir;
37+
$this->pidFileDirectory = $pidFileDirectory;
3838

3939
parent::__construct();
4040
}
@@ -73,7 +73,7 @@ protected function configure()
7373
protected function execute(InputInterface $input, OutputInterface $output)
7474
{
7575
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
76-
$server = new WebServer($this->projectDir);
76+
$server = new WebServer($this->pidFileDirectory);
7777
if ($filter = $input->getOption('filter')) {
7878
if ($server->isRunning($input->getOption('pidfile'))) {
7979
list($host, $port) = explode(':', $address = $server->getAddress($input->getOption('pidfile')));

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class ServerStopCommand extends Command
2828
{
2929
protected static $defaultName = 'server:stop';
3030

31-
private $projectDir;
31+
private $pidFileDirectory;
3232

33-
public function __construct(string $projectDir = null)
33+
public function __construct(string $pidFileDirectory = null)
3434
{
35-
$this->projectDir = $projectDir;
35+
$this->pidFileDirectory = $pidFileDirectory;
3636

3737
parent::__construct();
3838
}
@@ -64,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6464
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
6565

6666
try {
67-
$server = new WebServer($this->projectDir);
67+
$server = new WebServer($this->pidFileDirectory);
6868
$server->stop($input->getOption('pidfile'));
6969
$io->success('Stopped the web server.');
7070
} catch (\Exception $e) {

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ public function load(array $configs, ContainerBuilder $container)
2828
$loader->load('webserver.xml');
2929

3030
$publicDirectory = $this->getPublicDirectory($container);
31-
$container->getDefinition('web_server.command.server_run')->replaceArgument(1, $publicDirectory);
32-
$container->getDefinition('web_server.command.server_start')->replaceArgument(1, $publicDirectory);
31+
$container->getDefinition('web_server.command.server_run')->replaceArgument(0, $publicDirectory);
32+
$container->getDefinition('web_server.command.server_start')->replaceArgument(0, $publicDirectory);
33+
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);
3339

3440
if (!class_exists(ConsoleFormatter::class)) {
3541
$container->removeDefinition('web_server.command.server_log');
@@ -54,4 +60,16 @@ private function getPublicDirectory(ContainerBuilder $container)
5460

5561
return $kernelProjectDir.'/'.$publicDir;
5662
}
63+
64+
private function getPidFileDirectory(ContainerBuilder $container)
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'), 1);
74+
}
5775
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
<defaults public="false" />
99

1010
<service id="web_server.command.server_run" class="Symfony\Bundle\WebServerBundle\Command\ServerRunCommand">
11-
<argument>%kernel.project_dir%</argument>
1211
<argument>%kernel.project_dir%/public</argument>
1312
<argument>%kernel.environment%</argument>
13+
<argument>%kernel.project_dir%/var/cache</argument>
1414
<tag name="console.command" command="server:run" />
1515
</service>
1616

1717
<service id="web_server.command.server_start" class="Symfony\Bundle\WebServerBundle\Command\ServerStartCommand">
18-
<argument>%kernel.project_dir%</argument>
1918
<argument>%kernel.project_dir%/public</argument>
2019
<argument>%kernel.environment%</argument>
20+
<argument>%kernel.project_dir%/var/cache</argument>
2121
<tag name="console.command" command="server:start" />
2222
</service>
2323

2424
<service id="web_server.command.server_stop" class="Symfony\Bundle\WebServerBundle\Command\ServerStopCommand">
25-
<argument>%kernel.project_dir%</argument>
25+
<argument>%kernel.project_dir%/var/cache</argument>
2626
<tag name="console.command" command="server:stop" />
2727
</service>
2828

2929
<service id="web_server.command.server_status" class="Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand">
30-
<argument>%kernel.project_dir%</argument>
30+
<argument>%kernel.project_dir%/var/cache</argument>
3131
<tag name="console.command" command="server:status" />
3232
</service>
3333

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,30 @@ public function testLoad()
2626

2727
$this->assertSame(
2828
__DIR__.'/test',
29-
$container->getDefinition('web_server.command.server_run')->getArgument(1)
29+
$container->getDefinition('web_server.command.server_run')->getArgument(0)
3030
);
3131
$this->assertSame(
3232
__DIR__.'/test',
33-
$container->getDefinition('web_server.command.server_start')->getArgument(1)
33+
$container->getDefinition('web_server.command.server_start')->getArgument(0)
3434
);
35+
36+
$this->assertSame(
37+
__DIR__.'/test/var/cache',
38+
$container->getDefinition('web_server.command.server_run')->getArgument(2)
39+
);
40+
$this->assertSame(
41+
__DIR__.'/test/var/cache',
42+
$container->getDefinition('web_server.command.server_start')->getArgument(2)
43+
);
44+
$this->assertSame(
45+
__DIR__.'/test/var/cache',
46+
$container->getDefinition('web_server.command.server_stop')->getArgument(0)
47+
);
48+
$this->assertSame(
49+
__DIR__.'/test/var/cache',
50+
$container->getDefinition('web_server.command.server_status')->getArgument(0)
51+
);
52+
3553
$this->assertTrue($container->hasDefinition('web_server.command.server_run'));
3654
$this->assertTrue($container->hasDefinition('web_server.command.server_start'));
3755
$this->assertTrue($container->hasDefinition('web_server.command.server_stop'));

src/Symfony/Bundle/WebServerBundle/WebServer.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ class WebServer
2525
const STARTED = 0;
2626
const STOPPED = 1;
2727

28-
private $projectDir;
28+
private $pidFileDirectory;
2929

30-
public function __construct(string $projectDir = null)
30+
public function __construct(string $pidFileDirectory = null)
3131
{
32-
$this->projectDir = $projectDir;
32+
$this->pidFileDirectory = $pidFileDirectory;
3333
}
3434

3535
public function run(WebServerConfig $config, $disableOutput = true, callable $callback = null)
@@ -173,8 +173,6 @@ private function createServerProcess(WebServerConfig $config)
173173

174174
private function getDefaultPidFile()
175175
{
176-
return $this->projectDir
177-
? $this->projectDir.'/.web-server-pid'
178-
: getcwd().'/.web-server-pid';
176+
return ($this->pidFileDirectory ?? getcwd()).'/.web-server-pid';
179177
}
180178
}

0 commit comments

Comments
 (0)