-
-
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
Changes from 1 commit
c2d733a
8e89d34
0b61e4c
d258187
791a955
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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\AstGenerator; | ||
|
||
/** | ||
* Generator delegating the generation to a chain of generators. | ||
* | ||
* @author Joel Wurtz <joel.wurtz@gmail.com> | ||
*/ | ||
class AstGeneratorChain implements AstGeneratorInterface | ||
{ | ||
/** @var AstGeneratorInterface[] A list of generators */ | ||
protected $generators; | ||
|
||
/** @var bool Whether the generation must return as soon as possible or use all generators, default to false */ | ||
protected $returnOnFirst; | ||
|
||
public function __construct(array $generators = array(), $returnOnFirst = false) | ||
{ | ||
$this->generators = $generators; | ||
$this->returnOnFirst = $returnOnFirst; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate($object, array $context = array()) | ||
{ | ||
$nodes = array(); | ||
|
||
foreach ($this->generators as $generator) { | ||
if ($generator instanceof AstGeneratorInterface && $generator->supportsGeneration($object)) { | ||
$nodes = array_merge($nodes, $generator->generate($object, $context)); | ||
|
||
if ($this->returnOnFirst) { | ||
return $nodes; | ||
} | ||
} | ||
} | ||
|
||
return $nodes; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsGeneration($object) | ||
{ | ||
foreach ($this->generators as $generator) { | ||
if ($generator instanceof AstGeneratorInterface && $generator->supportsGeneration($object)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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 <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\AstGenerator; | ||
|
||
/** | ||
* An AstGeneratorInterface is a contract to transform an object into an AST. | ||
* | ||
* @author Joel Wurtz <joel.wurtz@gmail.com> | ||
*/ | ||
interface AstGeneratorInterface | ||
{ | ||
/** | ||
* Generate an object into an AST given a specific context. | ||
* | ||
* @param mixed $object Object to generate AST from | ||
* @param array $context Context for the generator | ||
* | ||
* @return \PhpParser\Node[] An array of statements (AST Node) | ||
*/ | ||
public function generate($object, array $context = array()); | ||
|
||
/** | ||
* Check whether the given object is supported for generation by this generator. | ||
* | ||
* @param mixed $object Object to generate AST from | ||
* | ||
* @return bool | ||
*/ | ||
public function supportsGeneration($object); | ||
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. This method makes me wonder: how it is possible that we don't exactly know if our generator is allowed to work with the given object? Does it even make sense to "try" generate AST tree of "random" object? Does it make sense to replace Once interface becomes vague, it turns into wrong abstraction, because
Also, it feels like "Tell, don't ask" violation. 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. This method is especially useful for chaining (@joelwurtz used it to chain generators transforming data depending on their type, really useful to allow the user to hook into the generation). Some generators could also be used in several components even if the routing component is not the best example for such use. 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.
Can't the chain be done with decoration only by throwing a 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. Would be good for me as well and as the generation is almost never optional it would even be logical, i will change that ! 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. I don't really understand use-case of the chain Generator, to be honest. Probably, another abstraction is missing like For me, /**
* Maps something on something another. Very useful abstraction.
*/
interface Mapper
{
/**
* @return mixed something
*/
public function map($something);
} It generates some AST node of some object. The purpose of this interface is very unclean. But some kind of 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. It is useful for normalizer generation you can take a look at the small app i wrote to illustrate how #17516 could be used. 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.
Still, I don't see why final class UrlGeneratorAstGenerator
{
public function generate(
RouteCollection $routeCollection,
// By the way, these parameters can be part of constructor
string $urlGeneratorClass,
string $baseUrlGeneratorClass
): NodeList
{
// ...
}
}
final class PhpMatcherAstGenerator
{
public function generate(RouteCollection $collection): NodeList
{
// ...
}
}
$nodeList =
$this->urlAstGenerator->generate(...)
->append(
$this->phpMatcherAstGenerator->generate(...)
); Generic interface adds false flexibility. Clarity of intent matters. Chain nodes, not generators. 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. This example looks much better indeed 👍 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\AstGenerator\Exception; | ||
|
||
class MissingContextException extends \RuntimeException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2016 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
AstGenerator Component | ||
====================== | ||
|
||
AstGenerator allows to generate PHP AST for several Component: | ||
|
||
* Transform class, properties and types extracted from the PropertyInfo Component into POPO objects ans Normalizers | ||
compatible with Serializer Component | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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\AstGenerator\Tests; | ||
|
||
use Symfony\Component\AstGenerator\AstGeneratorChain; | ||
use Symfony\Component\AstGenerator\AstGeneratorInterface; | ||
|
||
class AstGeneratorChainTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testEmpty() | ||
{ | ||
$generator = new AstGeneratorChain(); | ||
|
||
$this->assertFalse($generator->supportsGeneration('dummy')); | ||
$this->assertEmpty($generator->generate('dummy')); | ||
} | ||
|
||
public function testSupports() | ||
{ | ||
$generatorSub = $this->getGeneratorMock(true, array('ast')); | ||
|
||
$generator = new AstGeneratorChain(array($generatorSub)); | ||
$this->assertTrue($generator->supportsGeneration('dummy')); | ||
$this->assertEquals(array('ast'), $generator->generate('dummy')); | ||
} | ||
|
||
public function testMultiSupports() | ||
{ | ||
$generatorSub1 = $this->getGeneratorMock(true, array('ast1')); | ||
$generatorSub2 = $this->getGeneratorMock(true, array('ast2')); | ||
|
||
$generator = new AstGeneratorChain(array($generatorSub1, $generatorSub2)); | ||
$this->assertTrue($generator->supportsGeneration('dummy')); | ||
$this->assertEquals(array('ast1', 'ast2'), $generator->generate('dummy')); | ||
} | ||
|
||
public function testPartialSupports() | ||
{ | ||
$generatorSub1 = $this->getGeneratorMock(true, array('ast1')); | ||
$generatorSub2 = $this->getGeneratorMock(false); | ||
|
||
$generator = new AstGeneratorChain(array($generatorSub1, $generatorSub2)); | ||
$this->assertTrue($generator->supportsGeneration('dummy')); | ||
$this->assertEquals(array('ast1'), $generator->generate('dummy')); | ||
} | ||
|
||
public function testMultiSupportsWithFirstReturn() | ||
{ | ||
$generatorSub1 = $this->getGeneratorMock(true, array('ast1')); | ||
$generatorSub2 = $this->getGeneratorMock(true, array('ast2')); | ||
|
||
$generator = new AstGeneratorChain(array($generatorSub1, $generatorSub2), true); | ||
$this->assertTrue($generator->supportsGeneration('dummy')); | ||
$this->assertEquals(array('ast1'), $generator->generate('dummy')); | ||
} | ||
|
||
private function getGeneratorMock($support, $return = null) | ||
{ | ||
$generatorSub = $this->getMockBuilder(AstGeneratorInterface::class)->getMock(); | ||
$generatorSub | ||
->expects($this->any()) | ||
->method('supportsGeneration') | ||
->with('dummy') | ||
->willReturn($support); | ||
if (null === $return) { | ||
$generatorSub | ||
->expects($this->never()) | ||
->method('generate'); | ||
} else { | ||
$generatorSub | ||
->expects($this->any()) | ||
->method('generate') | ||
->with('dummy', array()) | ||
->willReturn($return); | ||
} | ||
|
||
return $generatorSub; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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\AstGenerator\Tests; | ||
|
||
use Symfony\Component\AstGenerator\UniqueVariableScope; | ||
|
||
class UniqueVariableScopeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testUniqueVariable() | ||
{ | ||
$uniqueVariableScope = new UniqueVariableScope(); | ||
|
||
$name = $uniqueVariableScope->getUniqueName('name'); | ||
$this->assertEquals('name', $name); | ||
|
||
$name = $uniqueVariableScope->getUniqueName('name'); | ||
$this->assertEquals('name_1', $name); | ||
|
||
$name = $uniqueVariableScope->getUniqueName('name'); | ||
$this->assertEquals('name_2', $name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\AstGenerator; | ||
|
||
/** | ||
* Allow to get a unique variable name for a scope (like a method). | ||
*/ | ||
class UniqueVariableScope | ||
{ | ||
private $registry = array(); | ||
|
||
/** | ||
* Return an unique name for a variable. | ||
* | ||
* @param string $name Name of the variable | ||
* | ||
* @return string if not found return the $name given, if not return the name suffixed with a number | ||
*/ | ||
public function getUniqueName($name) | ||
{ | ||
if (!isset($this->registry[$name])) { | ||
$this->registry[$name] = 0; | ||
|
||
return $name; | ||
} | ||
|
||
++$this->registry[$name]; | ||
|
||
return sprintf('%s_%s', $name, $this->registry[$name]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "symfony/ast-generator", | ||
"type": "library", | ||
"description": "Symfony component to generate AST from and to various others components", | ||
"keywords": ["symfony", "ast", "generator"], | ||
"homepage": "https://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Joel Wurtz", | ||
"email": "joel.wurtz@gmail.com" | ||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.9", | ||
"nikic/php-parser": "~2.0", | ||
"symfony/property-info": "~2.8|~3.0" | ||
}, | ||
"require-dev": { | ||
"symfony/serializer": "~2.8|~3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\AstGenerator\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev", | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "3.1-dev" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Symfony AstGenerator Component Test Suite"> | ||
<directory>./Tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./Tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Uh oh!
There was an error while loading. Please reload this page.
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.
Why this class is open for inheritance (not marked as
final
)?Same question about
UrlGeneratorGenerator
,PhpMatcherGenerator
,UniqueVariableScope
.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.
For this class and
UniqueVariableScope
i agree it would make sense even if there aren't that much final classes in symfony.But for the routing generators i would like to allow the user to easily hook into them and it could pass by inheritance.
Uh oh!
There was an error while loading. Please reload this page.
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.
How? Those classes have only
private
members and 2 public methods. And those 2 public methods can be easily changed with composition if developer really needs it.This is weird Symfony internal rule: to keep members
private
by default, but in the same time do not requirefinal
: there is no point to open them for inheritance then.If you find valid reason to extend the classes, you can provide sane extension point(s) instead of inheritance: strategies, events, hooks, etc.
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.
For now they are private but it may change. But you're right the class should be final as long as there are no extensions possible, i'll change ASAP.
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.
Sure, but instead of changing
private
properties toprotected
, you probably can introduce better extension point.Uh oh!
There was an error while loading. Please reload this page.
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.
Thx. I wasnt really opinionated on it, yet. https://ocramius.github.io/blog/when-to-declare-classes-final/ is also a nice read. Anyway, it looks good practice to me.. not sure if it's common enough (yet) for symfony though.
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.
@Ener-Getick then maybe it would be better to lock them (
final
and@internal
) first no?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.
Once you fix it, our comments will disappear :)
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.
@theofidry i'm using my phone tonight :P
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.
Good thing, you shouldn't work a saturday evening anyway ;)