From 2e63025e18ac0ecf7dee50637aa264667158b02b Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Sat, 7 Jan 2017 11:45:42 +0100 Subject: [PATCH] [WebServerBundle] Decouple server:* commands from the container --- .../WebServerBundle/Command/ServerCommand.php | 4 +-- .../Command/ServerRunCommand.php | 31 +++++++++++++++++-- .../Command/ServerStartCommand.php | 31 +++++++++++++++++-- .../WebServerExtension.php | 29 +++++++++++++++++ .../Resources/config/webserver.xml | 29 +++++++++++++++++ 5 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Bundle/WebServerBundle/DependencyInjection/WebServerExtension.php create mode 100644 src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml diff --git a/src/Symfony/Bundle/WebServerBundle/Command/ServerCommand.php b/src/Symfony/Bundle/WebServerBundle/Command/ServerCommand.php index ac661ac56bd2..40a0da6e08bc 100644 --- a/src/Symfony/Bundle/WebServerBundle/Command/ServerCommand.php +++ b/src/Symfony/Bundle/WebServerBundle/Command/ServerCommand.php @@ -11,14 +11,14 @@ namespace Symfony\Bundle\WebServerBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; /** * Base methods for commands related to a local web server. * * @author Christian Flothmann */ -abstract class ServerCommand extends ContainerAwareCommand +abstract class ServerCommand extends Command { /** * {@inheritdoc} diff --git a/src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php index ca91f259539a..4259e0071314 100644 --- a/src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php +++ b/src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php @@ -27,6 +27,17 @@ */ class ServerRunCommand extends ServerCommand { + private $documentRoot; + private $environment; + + public function __construct($documentRoot = null, $environment = null) + { + $this->documentRoot = $documentRoot; + $this->environment = $environment; + + parent::__construct(); + } + /** * {@inheritdoc} */ @@ -71,7 +82,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $io = new SymfonyStyle($input, $output); if (null === $documentRoot = $input->getOption('docroot')) { - $documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web'; + if (!$this->documentRoot) { + $io->error('The document root directory must be either passed as first argument of the constructor or through the "--docroot" input option.'); + + return 1; + } + $documentRoot = $this->documentRoot; } if (!is_dir($documentRoot)) { @@ -80,7 +96,18 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $env = $this->getContainer()->getParameter('kernel.environment'); + if (!$env = $this->environment) { + if ($input->hasOption('env') && !$env = $input->getOption('env')) { + $io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.'); + + return 1; + } else { + $io->error('The environment must be passed as second argument of the constructor.'); + + return 1; + } + } + if ('prod' === $env) { $io->error('Running this server in production environment is NOT recommended!'); } diff --git a/src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php b/src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php index ba5f29d452ef..a490802472f9 100644 --- a/src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php +++ b/src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php @@ -26,6 +26,17 @@ */ class ServerStartCommand extends ServerCommand { + private $documentRoot; + private $environment; + + public function __construct($documentRoot = null, $environment = null) + { + $this->documentRoot = $documentRoot; + $this->environment = $environment; + + parent::__construct(); + } + /** * {@inheritdoc} */ @@ -84,7 +95,12 @@ protected function execute(InputInterface $input, OutputInterface $output) } if (null === $documentRoot = $input->getOption('docroot')) { - $documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web'; + if (!$this->documentRoot) { + $io->error('The document root directory must be either passed as first argument of the constructor or through the "docroot" input option.'); + + return 1; + } + $documentRoot = $this->documentRoot; } if (!is_dir($documentRoot)) { @@ -93,7 +109,18 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $env = $this->getContainer()->getParameter('kernel.environment'); + if (!$env = $this->environment) { + if ($input->hasOption('env') && !$env = $input->getOption('env')) { + $io->error('The environment must be either passed as second argument of the constructor or through the "--env" input option.'); + + return 1; + } else { + $io->error('The environment must be passed as second argument of the constructor.'); + + return 1; + } + } + if ('prod' === $env) { $io->error('Running this server in production environment is NOT recommended!'); } diff --git a/src/Symfony/Bundle/WebServerBundle/DependencyInjection/WebServerExtension.php b/src/Symfony/Bundle/WebServerBundle/DependencyInjection/WebServerExtension.php new file mode 100644 index 000000000000..b26236bcccef --- /dev/null +++ b/src/Symfony/Bundle/WebServerBundle/DependencyInjection/WebServerExtension.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\WebServerBundle\DependencyInjection; + +use Symfony\Component\DependencyInjection\Extension\Extension; +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Config\FileLocator; + +/** + * @author Robin Chalas + */ +class WebServerExtension extends Extension +{ + public function load(array $configs, ContainerBuilder $container) + { + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('webserver.xml'); + } +} diff --git a/src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml b/src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml new file mode 100644 index 000000000000..aab41fe51a84 --- /dev/null +++ b/src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml @@ -0,0 +1,29 @@ + + + + + + + %kernel.root_dir%/../web + %kernel.environment% + + + + + %kernel.root_dir%/../web + %kernel.environment% + + + + + + + + + + + + +