-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
65dcd7a
Add tests to WebServerConfig
tomasfejfar 9d4f729
Add "executable" option to server:run console command
tomasfejfar 2feb064
Move executable finding fallback to WebServerConfig
tomasfejfar 6c8aa40
Tests for "executable" option
tomasfejfar c051b18
Proof of concept - executable with params
tomasfejfar 426b30a
Pull executable from ENV
tomasfejfar b5ff2ba
Revert CLI param and set using ENV
tomasfejfar 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
There are no files selected for viewing
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
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
128 changes: 128 additions & 0 deletions
128
src/Symfony/Bundle/WebServerBundle/Tests/WebServerConfig/WebServerConfigTest.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,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); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Bundle/WebServerBundle/Tests/WebServerConfig/fixtures/index.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,3 @@ | ||
<?php | ||
|
||
// dummy front controller |
Empty file.
3 changes: 3 additions & 0 deletions
3
src/Symfony/Bundle/WebServerBundle/Tests/WebServerConfig/fixtures/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,3 @@ | ||
<?php | ||
|
||
// dummy router |
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
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 |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\Bundle\WebServerBundle; | ||
|
||
use Symfony\Component\Process\PhpExecutableFinder; | ||
|
||
/** | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
|
@@ -21,6 +23,7 @@ class WebServerConfig | |
private $documentRoot; | ||
private $env; | ||
private $router; | ||
private $executable; | ||
|
||
public function __construct($documentRoot, $env, $address = null, $router = null) | ||
{ | ||
|
@@ -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) { | ||
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. should be |
||
$executable = $envExecutable; | ||
} | ||
|
||
if ($executable === null) { | ||
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. 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() | ||
|
@@ -86,6 +105,11 @@ public function getRouter() | |
return $this->router; | ||
} | ||
|
||
public function getExecutable() | ||
{ | ||
return $this->executable; | ||
} | ||
|
||
public function getHostname() | ||
{ | ||
return $this->hostname; | ||
|
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
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.
What about a path with a space in it?