Skip to content

Add "executable" option to server:run console command #23721

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

Closed
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/WebServerBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* WebServer can now use '*' as a wildcard to bind to 0.0.0.0 (INADDR_ANY)
* Custom executable can be set using `SYMFONY_SERVER_EXECUTABLE` environment variable

3.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ protected function configure()
Specify your own router script via the <info>--router</info> option:

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

Custom executable to run the server can be set using SYMFONY_SERVER_EXECUTABLE environment variable.

See also: http://www.php.net/manual/en/features.commandline.webserver.php
EOF
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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\Tests\WebServerConfig;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\WebServerBundle\WebServerConfig;

class WebServerConfigTest extends TestCase
{
public function testConstructor()
{
$config = new WebServerConfig(
__DIR__.'/fixtures',
'dev',
'85.111.31.18:8080',
__DIR__.'/fixtures/router.php'
);

$this->assertSame(
$this->normalizePath(__DIR__.'/fixtures'),
$this->normalizePath($config->getDocumentRoot())
);
$this->assertSame('dev', $config->getEnv());
$this->assertSame('85.111.31.18:8080', $config->getAddress());
$this->assertEquals(8080, $config->getPort());
$this->assertSame(
$this->normalizePath(__DIR__.'/fixtures/router.php'),
$this->normalizePath($config->getRouter())
);
}

public function testWillSetCorrectAddressAndPortAutomatically()
{
$config = new WebServerConfig(__DIR__.'/fixtures', 'dev');

$this->assertEquals('127.0.0.1:8000', $config->getAddress());
$this->assertLessThanOrEqual(8100, $config->getPort());
$this->assertGreaterThanOrEqual(8000, $config->getPort());
}

public function testWillCorrectlyParseAsteriskAndPort()
{
$config = new WebServerConfig(__DIR__.'/fixtures', 'dev', '*:8080');

$this->assertSame('0.0.0.0:8080', $config->getAddress());
$this->assertEquals(8080, $config->getPort());
}

public function testWillParsePlainNumberAsPort()
{
$config = new WebServerConfig(__DIR__.'/fixtures', 'dev', '8080');

$this->assertSame('127.0.0.1:8080', $config->getAddress());
$this->assertEquals(8080, $config->getPort());
}

public function testWillFailForNonNumberPort()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Port "12a" is not valid.');

$config = new WebServerConfig(__DIR__.'/fixtures', 'dev', '127.0.0.1:12a');
}

public function testWillFailIfDocumentRootIsNotADirectory()
{
$this->expectException(\InvalidArgumentException::class);
// symplified to workaround directory separator issues
$this->expectExceptionMessage('The document root directory');

$config = new WebServerConfig(__DIR__.'/does-not-exist', 'dev');
}

public function testWillFailIfDocumentRootDoesNotContainFrontController()
{
$this->expectException(\InvalidArgumentException::class);
// symplified to workaround directory separator issues
$this->expectExceptionMessage('Unable to find the front controller under');

$config = new WebServerConfig(__DIR__.'/fixtures/not-containing-anything', 'dev');
}

public function testWillFailIfRouterDirectoryDoesNotContainRouter()
{
$this->expectException(\InvalidArgumentException::class);
// symplified to workaround directory separator issues
$this->expectExceptionMessage('Router script');

$config = new WebServerConfig(
__DIR__.'/fixtures',
'dev',
null,
__DIR__.'/fixtures/not-containing-anything/router.php'
);
}

public function testWillSetRouterToDeaultIfNotPresent()
{
$config = new WebServerConfig(__DIR__.'/fixtures', 'dev');

// router is relative to the WebServerConfig.php file (therefore two levels above)
$this->assertSame(
$this->normalizePath(dirname(dirname(__DIR__)).'/Resources/router.php'),
$this->normalizePath($config->getRouter())
);
}

/**
* Normalizes directory separators to what is native on the current platform.
*
* @param $path
*
* @return string
*/
private function normalizePath($path)
{
return strtr($path, '/', DIRECTORY_SEPARATOR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

// dummy front controller
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

// dummy router
22 changes: 14 additions & 8 deletions src/Symfony/Bundle/WebServerBundle/WebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Symfony\Bundle\WebServerBundle;

use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;

/**
* Manages a local HTTP web server.
Expand Down Expand Up @@ -145,12 +145,18 @@ public function isRunning($pidFile = null)
*/
private function createServerProcess(WebServerConfig $config)
{
$finder = new PhpExecutableFinder();
if (false === $binary = $finder->find()) {
throw new \RuntimeException('Unable to find the PHP binary.');
}

$process = new Process(array($binary, '-S', $config->getAddress(), $config->getRouter()));
$executable = $config->getExecutable();

// we need to separate the executable from the rest of the string as StringInput won't handle it
$firstPartOfCommand = explode(' ', $executable)[0];
Copy link
Member

Choose a reason for hiding this comment

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

What about a path with a space in it?

// remove the executable and keep remainder of parameters
$remainder = substr($executable, strlen($firstPartOfCommand) + 1); //the one is a space

$input = new StringInput($remainder);
// newly added method to retrieve input parts
$executableArray = $input->getCommandLineArray();
$processArray = array_merge(array($firstPartOfCommand), $executableArray, array('-S', $config->getAddress(), $config->getRouter()));
$process = new Process($processArray);
$process->setWorkingDirectory($config->getDocumentRoot());
$process->setTimeout(null);

Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Bundle/WebServerBundle/WebServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bundle\WebServerBundle;

use Symfony\Component\Process\PhpExecutableFinder;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand All @@ -21,6 +23,7 @@ class WebServerConfig
private $documentRoot;
private $env;
private $router;
private $executable;

public function __construct($documentRoot, $env, $address = null, $router = null)
{
Expand Down Expand Up @@ -69,6 +72,22 @@ public function __construct($documentRoot, $env, $address = null, $router = null
if (!ctype_digit($this->port)) {
throw new \InvalidArgumentException(sprintf('Port "%s" is not valid.', $this->port));
}

$executable = null;

$envExecutable = getenv('SYMFONY_SERVER_EXECUTABLE');
if ($envExecutable !== false) {
Copy link
Member

Choose a reason for hiding this comment

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

should be false !== $envExecutable

$executable = $envExecutable;
}

if ($executable === null) {
Copy link
Member

Choose a reason for hiding this comment

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

Yoda-style as well

$finder = new PhpExecutableFinder();
if (false === $executable = $finder->find()) {
throw new \RuntimeException('Unable to find the PHP executable.');
}
}

$this->executable = $executable;
}

public function getDocumentRoot()
Expand All @@ -86,6 +105,11 @@ public function getRouter()
return $this->router;
}

public function getExecutable()
{
return $this->executable;
}

public function getHostname()
{
return $this->hostname;
Expand Down
14 changes: 11 additions & 3 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,17 @@ public function getParameterOption($values, $default = false, $onlyParams = fals
*/
public function __toString()
{
$tokens = array_map(function ($token) {
$tokens = $this->getCommandLineArray();

return implode(' ', $tokens);
}

/**
* @return array
*/
public function getCommandLineArray()
{
return array_map(function ($token) {
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
return $match[1].$this->escapeToken($match[2]);
}
Expand All @@ -332,7 +342,5 @@ public function __toString()

return $token;
}, $this->tokens);

return implode(' ', $tokens);
}
}