-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[RFC][Ast][Routing] Use Ast to generate the UrlGenerator #19555
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2d733a
[AstGenerator] Init the Component
joelwurtz 8e89d34
[AstGenerator] Create an util class to instantiate common nodes
GuilhemN 0b61e4c
[Routing] Convert the PhpMatcherDumper to an AstGeneratorInterface
GuilhemN d258187
[Routing] Convert the PhpGeneratorDumper to an AstGeneratorInterface
GuilhemN 791a955
Focus on GeneratorAstGenerator
GuilhemN 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
[Routing] Convert the PhpMatcherDumper to an AstGeneratorInterface
- Loading branch information
commit 0b61e4c6766316b6e2f1628487196d9178ae2275
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
133 changes: 133 additions & 0 deletions
133
src/Symfony/Component/Routing/Matcher/Generator/PhpMatcherGenerator.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,133 @@ | ||
<?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\Component\Routing\Matcher\Generator; | ||
|
||
use PhpParser\BuilderFactory; | ||
use PhpParser\Comment; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Scalar; | ||
use Symfony\Component\AstGenerator\AstGeneratorInterface; | ||
use Symfony\Component\AstGenerator\Util\AstHelper; | ||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; | ||
use Symfony\Component\Routing\Exception\MethodNotAllowedException; | ||
use Symfony\Component\Routing\Exception\ResourceNotFoundException; | ||
use Symfony\Component\Routing\RequestContext; | ||
|
||
/** | ||
* @author Guilhem N. <egetick@gmail.com> | ||
*/ | ||
class PhpMatcherGenerator implements AstGeneratorInterface | ||
{ | ||
private $expressionLanguage; | ||
|
||
/** | ||
* @var ExpressionFunctionProviderInterface[] | ||
*/ | ||
private $expressionLanguageProviders = array(); | ||
private $factory; | ||
|
||
public function __construct() | ||
{ | ||
$this->factory = new BuilderFactory(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate($object, array $context = array()) | ||
{ | ||
$context = array_replace(array( | ||
'class' => 'ProjectUrlMatcher', | ||
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher', | ||
), $context); | ||
|
||
// trailing slash support is only enabled if we know how to redirect the user | ||
$interfaces = class_implements($context['base_class']); | ||
$supportsRedirections = isset($interfaces['Symfony\\Component\\Routing\\Matcher\\RedirectableUrlMatcherInterface']); | ||
|
||
return array($this->generateClass($object, $context)->getNode()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsGeneration($object) | ||
{ | ||
return is_string($object) && class_exists($object); | ||
} | ||
|
||
private function generateClass($object, array $context) | ||
{ | ||
$docComment = <<<COMMENT | ||
/** | ||
* {$context['class']}. | ||
* | ||
* This class has been auto-generated | ||
* by the Symfony Routing Component. | ||
*/ | ||
COMMENT; | ||
|
||
return $this->factory->class($context['class']) | ||
->setDocComment($docComment) | ||
->extend($context['base_class']) | ||
->addStmt($this->factory->property('context')->makePrivate()) | ||
->addStmt( | ||
$this->factory->method('__construct') | ||
->makePublic() | ||
->addParam($this->factory->param('context')->setTypeHint(RequestContext::class)) | ||
->addStmt( | ||
// $this->context = $context | ||
new Expr\Assign( | ||
new Expr\PropertyFetch(new Expr\Variable('this'), 'context'), | ||
new Expr\Variable('context') | ||
) | ||
) | ||
) | ||
->addStmt($this->generateMatchMethod($object, $context)); | ||
} | ||
|
||
private function generateMatchMethod($object, array $context) | ||
{ | ||
$method = $this->factory | ||
->method('match') | ||
->makePublic() | ||
->addParam($this->factory->param('pathinfo')) | ||
->addStmt(new Expr\Assign(new Expr\Variable('allow'), AstHelper::value(array()))) | ||
->addStmt(new Expr\Assign( | ||
new Expr\Variable('pathinfo'), | ||
new Expr\FuncCall(new Name('rawurldecode'), array( | ||
new Arg(new Expr\Variable('pathinfo')), | ||
)) | ||
)) | ||
->addStmt(new Expr\Assign(new Expr\Variable('context'), new Expr\PropertyFetch(new Expr\Variable('this'), 'context'))) | ||
->addStmt(new Expr\Assign(new Expr\Variable('request'), new Expr\PropertyFetch(new Expr\Variable('this'), 'request'))); | ||
|
||
$method->addStmt(new Stmt\Throw_(new Expr\Ternary( | ||
new Expr\BinaryOp\Smaller( | ||
new Scalar\LNumber(0), | ||
new Expr\FuncCall(new Name('count'), array(new Arg(new Expr\Variable('allow')))) | ||
), | ||
new Expr\New_(new Name(MethodNotAllowedException::class), array( | ||
new Arg(new Expr\FuncCall(new Name('array_unique'), array( | ||
new Arg(new Expr\Variable('allow')), | ||
))), | ||
)), | ||
new Expr\New_(new Name(ResourceNotFoundException::class)) | ||
))); | ||
|
||
return $method; | ||
} | ||
} |
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.
Both arguments are unused.