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 all commits
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
"egulias/email-validator": "~1.2,>=1.2.8|~2.0",
"symfony/polyfill-apcu": "~1.1",
"symfony/security-acl": "~2.8|~3.0",
"phpdocumentor/reflection-docblock": "^3.0"
"phpdocumentor/reflection-docblock": "^3.0",
"nikic/PHP-Parser": "~2.0"
},
"conflict": {
"phpdocumentor/reflection-docblock": "<3.0",
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Ast/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
34 changes: 34 additions & 0 deletions src/Symfony/Component/Ast/AstDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Ast;

use PhpParser\PrettyPrinterAbstract;
use PhpParser\PrettyPrinter\Standard;

final class AstDumper
{
private $printer;

public function __construct(PrettyPrinterAbstract $printer = null)
{
if (null === $printer) {
$printer = new Standard();
}

$this->printer = $printer;
}

public function dump(NodeList $nodeList)
{
return $this->printer->prettyPrintFile($nodeList->getNodes());
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Ast/LICENSE
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.
49 changes: 49 additions & 0 deletions src/Symfony/Component/Ast/NodeList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Ast;

use PhpParser\Node;

final class NodeList
{
private $nodes = array();

/**
* @param Node[] $nodes
*/
public function __construct(array $nodes)
{
foreach ($nodes as $node) {
$this->addNode($node);
}
}

/**
* @return Node[]
*/
public function getNodes()
{
return $this->nodes;
}

public function addNode(Node $node)
{
$this->nodes[] = $node;
}

public function append(NodeList $nodeList)
{
foreach ($nodeList->getNodes() as $node) {
$this->addNode($node);
}
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Ast/README.md
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

108 changes: 108 additions & 0 deletions src/Symfony/Component/Ast/Util/AstHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\Ast\Util;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;

/**
* @internal
*/
final class AstHelper
{
private static $parser;

/**
* @param Node[] $stmts
*
* @return string the php code
*/
public static function dump(array $stmts)
{
$printer = new Standard();

return $printer->prettyPrintFile($stmts);
}

/**
* Transforms php code into an ast node.
*
* @param string $code the php code
*
* @return Node[]
*/
public static function raw($code)
{
$code = "<?php \n".$code;
if (null === self::$parser) {
$parserFactory = new ParserFactory();
self::$parser = $parserFactory->create(ParserFactory::ONLY_PHP5);
}

return self::$parser->parse($code);
}

/**
* Transforms a php value into an AST node.
*
* @param null|bool|int|float|string|array $value
*
* @return Expr
*/
public static function value($value)
{
if (is_null($value)) {
return new Expr\ConstFetch(
new Name('null')
);
} elseif (is_bool($value)) {
return new Expr\ConstFetch(
new Name($value ? 'true' : 'false')
);
} elseif (is_int($value)) {
return new Scalar\LNumber($value);
} elseif (is_float($value)) {
return new Scalar\DNumber($value);
} elseif (is_string($value)) {
return new Scalar\String_($value);
} elseif (is_array($value)) {
$items = array();
$lastKey = -1;
foreach ($value as $itemKey => $itemValue) {
// for consecutive, numeric keys don't generate keys
if (null !== $lastKey && ++$lastKey === $itemKey) {
$items[] = new Expr\ArrayItem(
self::value($itemValue)
);
} else {
$lastKey = null;
$items[] = new Expr\ArrayItem(
self::value($itemValue),
self::value($itemKey)
);
}
}

return new Expr\Array_($items);
} else {
throw new \LogicException('Invalid value');
}
}

private function __construct()
{
}
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/Ast/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "symfony/ast",
"type": "library",
"description": "Symfony AST Component",
"keywords": ["symfony", "ast"],
"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"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Ast\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
}
}
}
28 changes: 28 additions & 0 deletions src/Symfony/Component/Ast/phpunit.xml.dist
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>
Loading