-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] dump static arrays instead of classes for both matcher and generator #25909
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
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
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
37 changes: 37 additions & 0 deletions
37
src/Symfony/Bundle/FrameworkBundle/Routing/RedirectableStaticUrlMatcher.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,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Routing; | ||
|
||
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface; | ||
use Symfony\Component\Routing\Matcher\StaticUrlMatcher; | ||
|
||
/** | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class RedirectableStaticUrlMatcher extends StaticUrlMatcher implements RedirectableUrlMatcherInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function redirect($path, $route, $scheme = null) | ||
{ | ||
return array( | ||
'_controller' => 'Symfony\\Bundle\\FrameworkBundle\\Controller\\RedirectController::urlRedirectAction', | ||
'path' => $path, | ||
'permanent' => true, | ||
'scheme' => $scheme, | ||
'httpPort' => $this->context->getHttpPort(), | ||
'httpsPort' => $this->context->getHttpsPort(), | ||
'_route' => $route, | ||
); | ||
} | ||
} |
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,10 +11,14 @@ | |
|
||
namespace Symfony\Bundle\FrameworkBundle\Routing; | ||
|
||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0. Use RedirectableStaticUrlMatcher instead.', RedirectableUrlMatcher::class), E_USER_DEPRECATED); | ||
|
||
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher as BaseMatcher; | ||
|
||
/** | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @deprecated since Symfony 4.1, to be removed in 5.0. Use RedirectableStaticUrlMatcher instead. | ||
*/ | ||
class RedirectableUrlMatcher extends BaseMatcher | ||
{ | ||
|
77 changes: 77 additions & 0 deletions
77
src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableStaticUrlMatcherTest.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,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Routing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Routing\Matcher\Dumper\StaticUrlMatcherDumper; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableStaticUrlMatcher; | ||
use Symfony\Component\Routing\RequestContext; | ||
|
||
/** | ||
* @requires function \Symfony\Component\Routing\Matcher\StaticUrlMatcher::match | ||
*/ | ||
class RedirectableStaticUrlMatcherTest extends TestCase | ||
{ | ||
public function testRedirectWhenNoSlash() | ||
{ | ||
$routes = new RouteCollection(); | ||
$routes->add('foo', new Route('/foo/')); | ||
|
||
$matcher = $this->getMatcher($routes, $context = new RequestContext()); | ||
|
||
$this->assertEquals(array( | ||
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', | ||
'path' => '/foo/', | ||
'permanent' => true, | ||
'scheme' => null, | ||
'httpPort' => $context->getHttpPort(), | ||
'httpsPort' => $context->getHttpsPort(), | ||
'_route' => 'foo', | ||
), | ||
$matcher->match('/foo') | ||
); | ||
} | ||
|
||
public function testSchemeRedirect() | ||
{ | ||
$routes = new RouteCollection(); | ||
$routes->add('foo', new Route('/foo', array(), array(), array(), '', array('https'))); | ||
|
||
$matcher = $this->getMatcher($routes, $context = new RequestContext()); | ||
|
||
$this->assertEquals(array( | ||
'_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', | ||
'path' => '/foo', | ||
'permanent' => true, | ||
'scheme' => 'https', | ||
'httpPort' => $context->getHttpPort(), | ||
'httpsPort' => $context->getHttpsPort(), | ||
'_route' => 'foo', | ||
), | ||
$matcher->match('/foo') | ||
); | ||
} | ||
|
||
private function getMatcher(RouteCollection $routes, RequestContext $context) | ||
{ | ||
$dumper = new StaticUrlMatcherDumper($routes); | ||
$path = sys_get_temp_dir().'/php_matcher.'.uniqid('StaticUrlMatcher').'.php'; | ||
|
||
file_put_contents($path, $dumper->dump()); | ||
$matcher = new RedirectableStaticUrlMatcher(require $path, $context); | ||
unlink($path); | ||
|
||
return $matcher; | ||
} | ||
} |
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,11 +11,15 @@ | |
|
||
namespace Symfony\Component\Routing\Generator\Dumper; | ||
|
||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0. Use StaticUrlGeneratorDumper instead.', PhpGeneratorDumper::class), E_USER_DEPRECATED); | ||
|
||
/** | ||
* PhpGeneratorDumper creates a PHP class able to generate URLs for a given set of routes. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Tobias Schultze <http://tobion.de> | ||
* | ||
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlGeneratorDumper instead. | ||
*/ | ||
class PhpGeneratorDumper extends GeneratorDumper | ||
{ | ||
|
64 changes: 64 additions & 0 deletions
64
src/Symfony/Component/Routing/Generator/Dumper/StaticUrlGeneratorDumper.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,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Generator\Dumper; | ||
|
||
use Symfony\Component\Routing\Matcher\Dumper\StaticUrlMatcherDumper; | ||
|
||
/** | ||
* StaticUrlGeneratorDumper creates a PHP array to be used with StaticUrlGenerator. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Tobias Schultze <http://tobion.de> | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
class StaticUrlGeneratorDumper extends GeneratorDumper | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function dump(array $options = array()) | ||
{ | ||
return <<<EOF | ||
<?php | ||
|
||
// This file has been auto-generated by the Symfony Routing Component. | ||
|
||
return array({$this->generateDeclaredRoutes()} | ||
); | ||
|
||
EOF; | ||
} | ||
|
||
/** | ||
* Generates PHP code representing an array of defined routes | ||
* together with the routes properties (e.g. requirements). | ||
*/ | ||
private function generateDeclaredRoutes(): string | ||
{ | ||
$routes = ''; | ||
foreach ($this->getRoutes()->all() as $name => $route) { | ||
$compiledRoute = $route->compile(); | ||
|
||
$properties = array(); | ||
$properties[] = $compiledRoute->getVariables(); | ||
$properties[] = $route->getDefaults(); | ||
$properties[] = $route->getRequirements(); | ||
$properties[] = $compiledRoute->getTokens(); | ||
$properties[] = $compiledRoute->getHostTokens(); | ||
$properties[] = $route->getSchemes(); | ||
|
||
$routes .= sprintf("\n '%s' => %s,", $name, StaticUrlMatcherDumper::export($properties)); | ||
} | ||
|
||
return $routes; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Routing/Generator/StaticUrlGenerator.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,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Generator; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
use Symfony\Component\Routing\RequestContext; | ||
|
||
class StaticUrlGenerator extends UrlGenerator | ||
{ | ||
private $dumpedRoutes = array(); | ||
|
||
public function __construct(array $dumpedRoutes, RequestContext $context, LoggerInterface $logger = null) | ||
{ | ||
$this->dumpedRoutes = $dumpedRoutes; | ||
$this->context = $context; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) | ||
{ | ||
if (!isset($this->dumpedRoutes[$name])) { | ||
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name)); | ||
} | ||
|
||
list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = $this->dumpedRoutes[$name]; | ||
|
||
return $this->doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes); | ||
} | ||
} |
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\Component\Routing\Matcher\Dumper; | ||
|
||
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0. Use StaticUrlMatcherDumper instead.', PhpMatcherDumper::class), E_USER_DEPRECATED); | ||
|
||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
@@ -22,6 +24,8 @@ | |
* @author Fabien Potencier <[email protected]> | ||
* @author Tobias Schultze <http://tobion.de> | ||
* @author Arnaud Le Blanc <[email protected]> | ||
* | ||
* @deprecated since Symfony 4.1, to be removed in 5.0. Use StaticUrlMatcherDumper instead. | ||
*/ | ||
class PhpMatcherDumper extends MatcherDumper | ||
{ | ||
|
Oops, something went wrong.
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.
dunno why this is ignored by fabbot, which raises a false positive for now
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.
Does fabbot use a separate config file maybe?