-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9a0a17
[FrameworkBundle] Added server:run command
michalpipa 519d431
[FrameworkBundle] Fixed built-in server router script
michalpipa 4a3f6d5
[FrameworkBundle] Removed global variable from router script
michalpipa e7d38c1
[FrameworkBundle] Changed PHP version detection (see: #3529)
michalpipa cfa2dff
[FrameworkBundle] Changed server:run command description
michalpipa c3bf479
[FrameworkBundle] Used Process component
michalpipa df11e62
[FrameworkBundle] Used $output->write() instead of echo
michalpipa File filter
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
commit d9a0a17e17c7555699b955d8347f2ddcd27012fd
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
27
src/Symfony/Bundle/FrameworkBundle/Resources/config/router.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.