8000 [WebServerBundle] Decouple server:* commands from the container · symfony/symfony@2e63025 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e63025

Browse files
committed
[WebServerBundle] Decouple server:* commands from the container
1 parent e4c95ed commit 2e63025

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Bundle\WebServerBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use Symfony\Component\Console\Command\Command;
1515

1616
/**
1717
* Base methods for commands related to a local web server.
1818
*
1919
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
2020
*/
21-
abstract class ServerCommand extends ContainerAwareCommand
21+
abstract class ServerCommand extends Command
2222
{
2323
/**
2424
* {@inheritdoc}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
*/
2828
class ServerRunCommand extends ServerCommand
2929
{
30+
private $documentRoot;
31+
private $environment;
32+
33+
public function __construct($documentRoot = null, $environment = null)
34+
{
35+
$this->documentRoot = $documentRoot;
36+
$this->environment = $environment;
37+
38+
parent::__construct();
39+
}
40+
3041
/**
3142
* {@inheritdoc}
3243
*/
@@ -71,7 +82,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
7182
$io = new SymfonyStyle($input, $output);
7283

7384
if (null === $documentRoot = $input->getOption('docroot')) {
74-
$documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
85+
if (!$this->documentRoot) {
86+
$io->error('The document root directory must be either passed as first argument of the constructor or through the "--docroot" input option.');
87+
88+
return 1;
89+
}
90+
$documentRoot = $this->documentRoot;
7591
}
7692

7793
if (!is_dir($documentRoot)) {
@@ -80,7 +96,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
8096
return 1;
8197
}
8298

83-
$env = $this->getContainer()->getParameter('kernel.environment');
99+
if (!$env = $this->environment) {
100+
if ($input->hasOption('env') && !$env = $input->getOption('env')) {
101+
$io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.');
102+
103+
return 1;
104+
} else {
105+
$io->error('The environment must be passed as second argument of the constructor.');
106+
107+
return 1;
108+
}
109+
}
110+
84111
if ('prod' === $env) {
85112
$io->error('Running this server in production environment is NOT recommended!');
86113
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
*/
2727
class ServerStartCommand extends ServerCommand
2828
{
29+
private $documentRoot;
30+
private $environment;
31+
32+
public function __construct($documentRoot = null, $environment = null)
33+
{
34+
$this->documentRoot = $documentRoot;
35+
$this->environment = $environment;
36+
37+
parent::__construct();
38+
}
39+
2940
/**
3041
* {@inheritdoc}
3142
*/
@@ -84,7 +95,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
8495
}
8596

8697
if (null === $documentRoot = $input->getOption('docroot')) {
87-
$documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
98+
if (!$this->documentRoot) {
99+
$io->error('The document root directory must be either passed as first argument of the constructor or through the "docroot" input option.');
100+
101+
return 1;
102+
}
103+
$documentRoot = $this->documentRoot;
88104
}
89105

90106
if (!is_dir($documentRoot)) {
@@ -93,7 +109,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
93109
return 1;
94110
}
95111

96-
$env = $this->getContainer()->getParameter('kernel.environment');
112+
if (!$env = $this->environment) {
113+
if ($input->hasOption('env') && !$env = $input->getOption('env')) {
114+
$io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.');
115+
116+
return 1;
117+
} else {
118+
$io->error('The environment must be passed as second argument of the constructor.');
119+
120+
return 1;
121+
}
122+
}
123+
97124
if ('prod' === $env) {
98125
$io->error('Running this server in production environment is NOT recommended!');
99126
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\WebServerBundle\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Extension\Extension;
15+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\Config\FileLocator;
18+
19+
/**
20+
* @author Robin Chalas <robin.chalas@gmail.com>
21+
*/
22+
class WebServerExtension extends Extension
23+
{
24+
public function load(array $configs, ContainerBuilder $container)
25+
{
26+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27+
$loader->load('webserver.xml');
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service id="web_server.command.server_run" class="Symfony\Bundle\WebServerBundle\Command\ServerRunCommand">
9+
<argument>%kernel.root_dir%/../web</argument>
10+
<argument>%kernel.environment%</argument>
11+
<tag name="console.command" />
12+
</service>
13+
14+
<service id="web_server.command.server_start" class="Symfony\Bundle\WebServerBundle\Command\ServerStartCommand">
15+
<argument>%kernel.root_dir%/../web</argument>
16+
<argument>%kernel.environment%</argument>
17+
<tag name="console.command" />
18+
</service>
19+
20+
<service id="web_server.command.server_stop" class="Symfony\Bundle\WebServerBundle\Command\ServerStopCommand">
21+
<tag name="console.command" />
22+
</service>
23+
24+
<service id="web_server.command.server_status" class="Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand">
25+
<tag name="console.command" />
26+
</service>
27+
</services>
28+
29+
</container>

0 commit comments

Comments
 (0)
0