From d9a0a17e17c7555699b955d8347f2ddcd27012fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Wed, 22 Feb 2012 21:04:54 +0100 Subject: [PATCH 1/7] [FrameworkBundle] Added server:run command --- .../Command/ServerRunCommand.php | 116 ++++++++++++++++++ .../Resources/config/router.php | 27 ++++ 2 files changed, 143 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php new file mode 100644 index 0000000000..483da057a3 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php @@ -0,0 +1,116 @@ + + * + * 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 + */ +class ServerRunCommand extends ContainerAwareCommand +{ + /** + * {@inheritDoc} + */ + public function isEnabled() + { + if (PHP_VERSION_ID < 50400) { + 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(<<%command.name% runs Symfony2 application using PHP built-in web server: + + %command.full_name% + +To change default bind address and port use the address argument: + + %command.full_name% 127.0.0.1:8080 + +To change default docroot directory use the --docroot option: + + %command.full_name% --docroot=htdocs/ + +If you have custom docroot directory layout, you can specify your own +router script using --router option: + + %command.full_name% --router=app/config/router.php + +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)) { + 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 + ); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php new file mode 100644 index 0000000000..c8ce1e7c29 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php @@ -0,0 +1,27 @@ + + * + * 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 + */ + +if (isset($_SERVER['SCRIPT_FILENAME'])) { + return false; +} else { + require 'app.php'; +} From 519d43158feeb588aef711a80c3cb9f812197852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Wed, 7 Mar 2012 08:14:42 +0100 Subject: [PATCH 2/7] [FrameworkBundle] Fixed built-in server router script --- .../Bundle/FrameworkBundle/Resources/config/router.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php index c8ce1e7c29..4d7bf6ecd1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php @@ -23,5 +23,12 @@ if (isset($_SERVER['SCRIPT_FILENAME'])) { return false; } else { - require 'app.php'; + $controller = 'app.php'; + + $_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] + . DIRECTORY_SEPARATOR + . $controller + ; + + require $controller; } From 4a3f6d57685f57690469298e23dc0a680983054a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Wed, 7 Mar 2012 08:31:12 +0100 Subject: [PATCH 3/7] [FrameworkBundle] Removed global variable from router script --- .../Bundle/FrameworkBundle/Resources/config/router.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php index 4d7bf6ecd1..e7174612f3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php @@ -23,12 +23,10 @@ if (isset($_SERVER['SCRIPT_FILENAME'])) { return false; } else { - $controller = 'app.php'; - $_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR - . $controller + . 'app.php' ; - require $controller; + require 'app.php'; } From e7d38c138116fa490cac87894e940a93286a3b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Tue, 13 Mar 2012 08:38:03 +0100 Subject: [PATCH 4/7] [FrameworkBundle] Changed PHP version detection (see: #3529) --- src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php index 483da057a3..fb92f3e768 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php @@ -28,7 +28,7 @@ class ServerRunCommand extends ContainerAwareCommand */ public function isEnabled() { - if (PHP_VERSION_ID < 50400) { + if (version_compare(phpversion(), '5.4.0', '<')) { return false; } From cfa2dff5ce33856a2e8db198b5c49cc67ffc3cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Tue, 13 Mar 2012 08:47:19 +0100 Subject: [PATCH 5/7] [FrameworkBundle] Changed server:run command description --- .../Bundle/FrameworkBundle/Command/ServerRunCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php index fb92f3e768..5518aad26f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php @@ -47,9 +47,9 @@ class ServerRunCommand extends ContainerAwareCommand 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') + ->setDescription('Runs PHP built-in web server') ->setHelp(<<%command.name% runs Symfony2 application using PHP built-in web server: +The %command.name% runs PHP built-in web server: %command.full_name% From c3bf479b1475aaa23888b2f2a99eb8d8cbdb3fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Tue, 13 Mar 2012 08:48:29 +0100 Subject: [PATCH 6/7] [FrameworkBundle] Used Process component --- .../Command/ServerRunCommand.php | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php index 5518aad26f..be64532a87 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php @@ -15,6 +15,7 @@ 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 @@ -74,43 +75,27 @@ 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)) { - 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 - ) - ); + $builder = new ProcessBuilder(array( + PHP_BINARY, + '-S', + $input->getArgument('address'), + $router + )); - proc_open( - $command, - array( - 0 => STDIN, - 1 => STDOUT, - 2 => STDERR - ), - $pipes - ); + $builder->setWorkingDirectory($input->getOption('docroot')); + $builder->setTimeout(null); + + $process = $builder->getProcess()->run(function ($type, $buffer) { + echo $buffer; + }); } } From df11e6287acfb98cd944c7a4d5f37122aa619873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pipa?= Date: Tue, 13 Mar 2012 18:49:25 +0100 Subject: [PATCH 7/7] [FrameworkBundle] Used $output->write() instead of echo --- .../Bundle/FrameworkBundle/Command/ServerRunCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php index be64532a87..2daf5e5f99 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php @@ -94,8 +94,8 @@ EOF $builder->setWorkingDirectory($input->getOption('docroot')); $builder->setTimeout(null); - $process = $builder->getProcess()->run(function ($type, $buffer) { - echo $buffer; + $builder->getProcess()->run(function ($type, $buffer) use ($output) { + $output->write($buffer); }); } }