feature #31280 [WebServerBundle] Change the default pidfile location to cache directory (jschaedl)
This PR was squashed before being merged into the 4.3-dev branch (closes #31280).
Discussion
----------
[WebServerBundle] Change the default pidfile location to cache directory
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | #29160 <!-- #-prefixed issue number(s), if any -->
| License | MIT
| Doc PR | tbd.
<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
- Bug fixes must be submitted against the lowest branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
-->
Commits
-------
2e14b6e891
[WebServerBundle] Change the default pidfile location to cache directory
This commit is contained in:
commit
707b1dfb5e
@ -31,13 +31,15 @@ class ServerRunCommand extends Command
|
||||
{
|
||||
private $documentRoot;
|
||||
private $environment;
|
||||
private $pidFileDirectory;
|
||||
|
||||
protected static $defaultName = 'server:run';
|
||||
|
||||
public function __construct(string $documentRoot = null, string $environment = null)
|
||||
public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
|
||||
{
|
||||
$this->documentRoot = $documentRoot;
|
||||
$this->environment = $environment;
|
||||
$this->pidFileDirectory = $pidFileDirectory;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
@ -129,7 +131,7 @@ EOF
|
||||
}
|
||||
|
||||
try {
|
||||
$server = new WebServer();
|
||||
$server = new WebServer($this->pidFileDirectory);
|
||||
$config = new WebServerConfig($documentRoot, $env, $input->getArgument('addressport'), $input->getOption('router'));
|
||||
|
||||
$message = sprintf('Server listening on http://%s', $config->getAddress());
|
||||
|
@ -31,13 +31,15 @@ class ServerStartCommand extends Command
|
||||
{
|
||||
private $documentRoot;
|
||||
private $environment;
|
||||
private $pidFileDirectory;
|
||||
|
||||
protected static $defaultName = 'server:start';
|
||||
|
||||
public function __construct(string $documentRoot = null, string $environment = null)
|
||||
public function __construct(string $documentRoot = null, string $environment = null, string $pidFileDirectory = null)
|
||||
{
|
||||
$this->documentRoot = $documentRoot;
|
||||
$this->environment = $environment;
|
||||
$this->pidFileDirectory = $pidFileDirectory;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
@ -133,7 +135,7 @@ EOF
|
||||
$this->getApplication()->setDispatcher(new EventDispatcher());
|
||||
|
||||
try {
|
||||
$server = new WebServer();
|
||||
$server = new WebServer($this->pidFileDirectory);
|
||||
if ($server->isRunning($input->getOption('pidfile'))) {
|
||||
$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'))));
|
||||
|
||||
|
@ -30,6 +30,15 @@ class ServerStatusCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'server:status';
|
||||
|
||||
private $pidFileDirectory;
|
||||
|
||||
public function __construct(string $pidFileDirectory = null)
|
||||
{
|
||||
$this->pidFileDirectory = $pidFileDirectory;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -64,7 +73,7 @@ EOF
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
|
||||
$server = new WebServer();
|
||||
$server = new WebServer($this->pidFileDirectory);
|
||||
if ($filter = $input->getOption('filter')) {
|
||||
if ($server->isRunning($input->getOption('pidfile'))) {
|
||||
list($host, $port) = explode(':', $address = $server->getAddress($input->getOption('pidfile')));
|
||||
|
@ -28,6 +28,15 @@ class ServerStopCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'server:stop';
|
||||
|
||||
private $pidFileDirectory;
|
||||
|
||||
public function __construct(string $pidFileDirectory = null)
|
||||
{
|
||||
$this->pidFileDirectory = $pidFileDirectory;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -55,7 +64,7 @@ EOF
|
||||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
|
||||
|
||||
try {
|
||||
$server = new WebServer();
|
||||
$server = new WebServer($this->pidFileDirectory);
|
||||
$server->stop($input->getOption('pidfile'));
|
||||
$io->success('Stopped the web server.');
|
||||
} catch (\Exception $e) {
|
||||
|
@ -31,6 +31,12 @@ class WebServerExtension extends Extension
|
||||
$container->getDefinition('web_server.command.server_run')->replaceArgument(0, $publicDirectory);
|
||||
$container->getDefinition('web_server.command.server_start')->replaceArgument(0, $publicDirectory);
|
||||
|
||||
$pidFileDirectory = $this->getPidFileDirectory($container);
|
||||
$container->getDefinition('web_server.command.server_run')->replaceArgument(2, $pidFileDirectory);
|
||||
$container->getDefinition('web_server.command.server_start')->replaceArgument(2, $pidFileDirectory);
|
||||
$container->getDefinition('web_server.command.server_stop')->replaceArgument(0, $pidFileDirectory);
|
||||
$container->getDefinition('web_server.command.server_status')->replaceArgument(0, $pidFileDirectory);
|
||||
|
||||
if (!class_exists(ConsoleFormatter::class)) {
|
||||
$container->removeDefinition('web_server.command.server_log');
|
||||
}
|
||||
@ -54,4 +60,16 @@ class WebServerExtension extends Extension
|
||||
|
||||
return $kernelProjectDir.'/'.$publicDir;
|
||||
}
|
||||
|
||||
private function getPidFileDirectory(ContainerBuilder $container): string
|
||||
{
|
||||
$kernelCacheDir = $container->getParameter('kernel.cache_dir');
|
||||
$environment = $container->getParameter('kernel.environment');
|
||||
|
||||
if (basename($kernelCacheDir) !== $environment) {
|
||||
return $container->getParameter('kernel.project_dir');
|
||||
}
|
||||
|
||||
return \dirname($container->getParameter('kernel.cache_dir'));
|
||||
}
|
||||
}
|
||||
|
@ -10,20 +10,24 @@
|
||||
<service id="web_server.command.server_run" class="Symfony\Bundle\WebServerBundle\Command\ServerRunCommand">
|
||||
<argument>%kernel.project_dir%/public</argument>
|
||||
<argument>%kernel.environment%</argument>
|
||||
<argument>%kernel.project_dir%/var/cache</argument>
|
||||
<tag name="console.command" command="server:run" />
|
||||
</service>
|
||||
|
||||
<service id="web_server.command.server_start" class="Symfony\Bundle\WebServerBundle\Command\ServerStartCommand">
|
||||
<argument>%kernel.project_dir%/public</argument>
|
||||
<argument>%kernel.environment%</argument>
|
||||
<argument>%kernel.project_dir%/var/cache</argument>
|
||||
<tag name="console.command" command="server:start" />
|
||||
</service>
|
||||
|
||||
<service id="web_server.command.server_stop" class="Symfony\Bundle\WebServerBundle\Command\ServerStopCommand">
|
||||
<argument>%kernel.project_dir%/var/cache</argument>
|
||||
<tag name="console.command" command="server:stop" />
|
||||
</service>
|
||||
|
||||
<service id="web_server.command.server_status" class="Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand">
|
||||
<argument>%kernel.project_dir%/var/cache</argument>
|
||||
<tag name="console.command" command="server:status" />
|
||||
</service>
|
||||
|
||||
|
@ -22,6 +22,8 @@ class WebServerExtensionTest extends TestCase
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->setParameter('kernel.project_dir', __DIR__);
|
||||
$container->setParameter('kernel.cache_dir', __DIR__.'/var/cache/test');
|
||||
$container->setParameter('kernel.environment', 'test');
|
||||
(new WebServerExtension())->load([], $container);
|
||||
|
||||
$this->assertSame(
|
||||
@ -32,6 +34,24 @@ class WebServerExtensionTest extends TestCase
|
||||
__DIR__.'/test',
|
||||
$container->getDefinition('web_server.command.server_start')->getArgument(0)
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
__DIR__.'/var/cache',
|
||||
$container->getDefinition('web_server.command.server_run')->getArgument(2)
|
||||
);
|
||||
$this->assertSame(
|
||||
__DIR__.'/var/cache',
|
||||
$container->getDefinition('web_server.command.server_start')->getArgument(2)
|
||||
);
|
||||
$this->assertSame(
|
||||
__DIR__.'/var/cache',
|
||||
$container->getDefinition('web_server.command.server_stop')->getArgument(0)
|
||||
);
|
||||
$this->assertSame(
|
||||
__DIR__.'/var/cache',
|
||||
$container->getDefinition('web_server.command.server_status')->getArgument(0)
|
||||
);
|
||||
|
||||
$this->assertTrue($container->hasDefinition('web_server.command.server_run'));
|
||||
$this->assertTrue($container->hasDefinition('web_server.command.server_start'));
|
||||
$this->assertTrue($container->hasDefinition('web_server.command.server_stop'));
|
||||
|
@ -25,6 +25,13 @@ class WebServer
|
||||
const STARTED = 0;
|
||||
const STOPPED = 1;
|
||||
|
||||
private $pidFileDirectory;
|
||||
|
||||
public function __construct(string $pidFileDirectory = null)
|
||||
{
|
||||
$this->pidFileDirectory = $pidFileDirectory;
|
||||
}
|
||||
|
||||
public function run(WebServerConfig $config, $disableOutput = true, callable $callback = null)
|
||||
{
|
||||
if ($this->isRunning()) {
|
||||
@ -166,6 +173,6 @@ class WebServer
|
||||
|
||||
private function getDefaultPidFile()
|
||||
{
|
||||
return getcwd().'/.web-server-pid';
|
||||
return ($this->pidFileDirectory ?? getcwd()).'/.web-server-pid';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user