8000 [FrameworkBundle] Added server:run command (PHP 5.4 built-in web server) by michalpipa · Pull Request #3465 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Added server:run command (PHP 5.4 built-in web server) #3465

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 7 commits into from
Mar 23, 2012
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
101 changes: 101 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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\FrameworkBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;

/**
* Runs Symfony2 application using PHP built-in web server
*
* @author Michał Pipa <michal.pipa.xsolve@gmail.com>
*/
class ServerRunCommand extends ContainerAwareCommand
{
/**
* {@inheritDoc}
*/
public function isEnabled()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
return false;
}

return parent::isEnabled();
}

/**
* @see Command
*/
protected function configure()
{
$this
->setDefinition(array(
new InputArgument('address', InputArgument::OPTIONAL, 'Address:port', 'localhost:8000'),
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root', 'web/'),
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
))
->setName('server:run')
->setDescription('Runs PHP built-in web server')
->setHelp(<<<EOF
The <info>%command.name%</info> runs PHP built-in web server:

<info>%command.full_name%</info>

To change default bind address and port use the <info>address</info> argument:

<info>%command.full_name% 127.0.0.1:8080</info>

To change default docroot directory use the <info>--docroot</info> option:

<info>%command.full_name% --docroot=htdocs/</info>

If you have custom docroot directory layout, you can specify your own
router script using <info>--router</info> option:

<info>%command.full_name% --router=app/config/router.php</info>

See also: http://www.php.net/manual/en/features.commandline.webserver.php
EOF
)
;
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$router = $input->getOption('router') ?: $this
->getContainer()
->get('kernel')
->locateResource('@FrameworkBundle/Resources/config/router.php')
;

$builder = new ProcessBuilder(array(
PHP_BINARY,
'-S',
$input->getArgument('address'),
$router
));

$builder->setWorkingDirectory($input->getOption('docroot'));
$builder->setTimeout(null);

$builder->getProcess()->run(function ($type, $buffer) use ($output) {
$output->write($buffer);
});
}
}
32 changes: 32 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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.
*/

/*
* This file implements rewrite rules for PHP built-in web server.
*
* See: http://www.php.net/manual/en/features.commandline.webserver.php
*
* If you have custom directory layout, then you have to write your own router
* and pass it as a value to 'router' option of server:run command.
*
* @author: Michał Pipa <michal.pipa.xsolve@gmail.com>
*/

if (isset($_SERVER['SCRIPT_FILENAME'])) {
return false;
} else {
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT']
. DIRECTORY_SEPARATOR
. 'app.php'
;

require 'app.php';
}
0