10000 [RFC][Ast][Routing] Use Ast to generate the UrlGenerator by GuilhemN · Pull Request #19555 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Routing] Convert the PhpMatcherDumper to an AstGeneratorInterface
  • Loading branch information
GuilhemN committed Aug 6, 2016
commit 0b61e4c6766316b6e2f1628487196d9178ae2275
40 changes: 5 additions & 35 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Component\Routing\Matcher\Dumper;

use Symfony\Component\AstGenerator\Util\AstHelper;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Matcher\Generator\PhpMatcherGenerator;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;

Expand All @@ -31,6 +33,7 @@ class PhpMatcherDumper extends MatcherDumper
* @var ExpressionFunctionProviderInterface[]
*/
private $expressionLanguageProviders = array();
private $generator;

/**
* Dumps a set of routes to a PHP class.
Expand All @@ -46,42 +49,9 @@ class PhpMatcherDumper extends MatcherDumper
*/
public function dump(array $options = array())
{
$options = array_replace(array(
'class' => 'ProjectUrlMatcher',
'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
), $options);
$generator = new PhpMatcherGenerator();

// trailing slash support is only enabled if we know how to redirect the user
$interfaces = class_implements($options['base_class']);
$supportsRedirections = isset($interfaces['Symfony\\Component\\Routing\\Matcher\\RedirectableUrlMatcherInterface']);

return <<<EOF
<?php

use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;

/**
* {$options['class']}.
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class {$options['class']} extends {$options['base_class']}
{
/**
* Constructor.
*/
public function __construct(RequestContext \$context)
{
\$this->context = \$context;
}

{$this->generateMatchMethod($supportsRedirections)}
}

EOF;
return AstHelper::dump($generator->generate($this->getRoutes(), $options));
}

public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
Expand Down
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Both arguments are unused.

{
$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;
}
}
0