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 1 commit
Commits
File filter

Filter by extension

8000
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[FrameworkBundle] Added server:run command
  • Loading branch information
michalpipa committed Feb 27, 2012
commit d9a0a17e17c7555699b955d8347f2ddcd27012fd
116 changes: 116 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?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;

/**
* 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 (PHP_VERSION_ID < 50400) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you use this syntax?

<?php

if (version_compare(phpversion(), '5.4.0', '<')) {

} else {

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that both versions are OK, but mine is simpler.

PHP_VERSION_ID is available since PHP 5.2.7, but you need at least PHP 5.3 to use Symfony2 anyway.

Copy link
Contributor 8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't mind, either are fine, but I thought Symfony used the latter. Just checked and the former is not used at all and the latter is only used in 4 tests, so it doesn't really matter.

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 Symfony2 application using PHP built-in web server')
->setHelp(<<<EOF
The <info>%command.name%</info> runs Symfony2 application using 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
*
* @throws \InvalidArgumentException When the docroot directory does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$docroot = $input->getOption('docroot');

if (@!chdir($docroot)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be if (!@chdir($docRoot))

throw new \InvalidArgumentException(sprintf(
'Unable to change directory to %s',
$docroot
));
}

$router = $input->getOption('router') ?: $this
->getContainer()
->get('kernel')
->locateResource('@FrameworkBundle/Resources/config/router.php')
;

$command = escapeshellcmd(
sprintf(
'%s -S %s %s',
PHP_BINARY,
$input->getArgument('address'),
$router
)
);

proc_open(
$command,
array(
0 => STDIN,
1 => STDOUT,
2 => STDERR
),
$pipes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use the Process component instead which includes workaround for Windows bugs for instance

);
}
}
27 changes: 27 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 {
require 'app.php';
}
0