[WebServerBundle] Decouple server:* commands from the container

This commit is contained in:
Robin Chalas 2017-01-07 11:45:42 +01:00
parent e4c95ed400
commit 2e63025e18
No known key found for this signature in database
GPG Key ID: 89672113756EE03B
5 changed files with 118 additions and 6 deletions

View File

@ -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 <christian.flothmann@xabbuh.de>
*/
abstract class ServerCommand extends ContainerAwareCommand
abstract class ServerCommand extends Command
{
/**
* {@inheritdoc}

View File

@ -27,6 +27,17 @@ use Symfony\Component\Console\Style\SymfonyStyle;
*/
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 @@ EOF
$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 @@ EOF
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!');
}

View File

@ -26,6 +26,17 @@ use Symfony\Component\Console\Style\SymfonyStyle;
*/
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 @@ EOF
}
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 @@ EOF
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!');
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <robin.chalas@gmail.com>
*/
class WebServerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('webserver.xml');
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="web_server.command.server_run" class="Symfony\Bundle\WebServerBundle\Command\ServerRunCommand">
<argument>%kernel.root_dir%/../web</argument>
<argument>%kernel.environment%</argument>
<tag name="console.command" />
</service>
<service id="web_server.command.server_start" class="Symfony\Bundle\WebServerBundle\Command\ServerStartCommand">
<argument>%kernel.root_dir%/../web</argument>
<argument>%kernel.environment%</argument>
<tag name="console.command" />
</service>
<service id="web_server.command.server_stop" class="Symfony\Bundle\WebServerBundle\Command\ServerStopCommand">
<tag name="console.command" />
</service>
<service id="web_server.command.server_status" class="Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand">
<tag name="console.command" />
</service>
</services>
</container>