Skip to content

[WebServerBundle] Decouple server commands from the container #21190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/WebServerBundle/Command/ServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
31 changes: 29 additions & 2 deletions src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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)) {
Expand All @@ -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!');
}
Expand Down
31 changes: 29 additions & 2 deletions src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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)) {
Expand All @@ -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!');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
}
}
29 changes: 29 additions & 0 deletions src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml
Original file line number Diff line number Diff line change
@@ -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>