-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[AstGenerator] [WIP] New Component, add normalizer generator #17516
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
joelwurtz
wants to merge
14
commits into
symfony:master
from
joelwurtz:feature/AstGeneratorComponent
Closed
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
36b89e2
Init AstGenerator Component
joelwurtz f20a0a1
Add chain generator
joelwurtz d41518f
Add normalizer generator
joelwurtz 8349681
Begin hydrate generator
joelwurtz a112c2f
Declare output in a upper level for better abstraction
joelwurtz 80992e0
Fix Cs
joelwurtz 365bdb2
Add hydration of object
joelwurtz de38c7b
Add collection type generator
joelwurtz 83718fa
Require PHP-Parser
GuilhemN 189c995
Replace Prophecy by Phpunit mocks
GuilhemN 7cbeab0
Create NormalizableObjectTypeGenerator
GuilhemN d9b4630
Include the normalizer in the context
GuilhemN 1e07e3e
Fixes
GuilhemN 8e636a0
Use long array syntax
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
Begin hydrate generator
- Loading branch information
commit 8349681ba436ca76886a27aac31d98e3c144e392
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/AstGenerator/Exception/MissingContextException.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,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\AstGenerator\Exception; | ||
|
||
class MissingContextException extends \RuntimeException | ||
{ | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/AstGenerator/Hydrate/ArrayHydrateGenerator.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,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\Hydrate; | ||
|
||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Scalar; | ||
|
||
/** | ||
* Create AST Statement to normalize a Class into a stdClassObject | ||
* | ||
* @author Joel Wurtz <joel.wurtz@gmail.com> | ||
*/ | ||
class ArrayHydrateGenerator extends HydrateFromObjectGenerator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAssignStatement($dataVariable) | ||
{ | ||
return new Expr\Assign($dataVariable, new Expr\Array_()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getSubAssignVariableStatement($dataVariable, $property) | ||
{ | ||
return new Expr\ArrayDimFetch($dataVariable, new Scalar\String_($property)); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
src/Symfony/Component/AstGenerator/Hydrate/HydrateFromObjectGenerator.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,121 @@ | ||
<?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\Hydrate; | ||
|
||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Expr; | ||
use Symfony\Component\AstGenerator\AstGeneratorInterface; | ||
use Symfony\Component\AstGenerator\Exception\MissingContextException; | ||
use Symfony\Component\PropertyAccess\PropertyAccessor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; | ||
|
||
abstract class HydrateFromObjectGenerator implements AstGeneratorInterface | ||
{ | ||
/** @var PropertyInfoExtractorInterface Extract list of properties from a class */ | ||
protected $propertyInfoExtractor; | ||
|
||
/** @var AstGeneratorInterface Generator for normalization of types */ | ||
protected $typeHydrateAstGenerator; | ||
|
||
public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, AstGeneratorInterface $typeHydrateAstGenerator) | ||
{ | ||
$this->propertyInfoExtractor = $propertyInfoExtractor; | ||
$this->typeHydrateAstGenerator = $typeHydrateAstGenerator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate($object, array $context = []) | ||
{ | ||
if (!isset($context['input'])) { | ||
throw new MissingContextException('Input variable not defined in context'); | ||
} | ||
|
||
$dataVariable = new Expr\Variable('data'); | ||
$statements = [$this->getAssignStatement($dataVariable)]; | ||
|
||
foreach ($this->propertyInfoExtractor->getProperties($object, $context) as $property) { | ||
// Only normalize readable property | ||
if (!$this->propertyInfoExtractor->isReadable($object, $property, $context)) { | ||
continue; | ||
} | ||
|
||
// @TODO Have property info extractor extract the way of reading a property (public or method with method name) | ||
$input = new Expr\MethodCall($context['input'], 'get'.ucfirst($property)); | ||
$output = $this->getSubAssignVariableStatement($dataVariable, $property); | ||
$types = $this->propertyInfoExtractor->getTypes($object, $property, $context); | ||
|
||
// If no type can be extracted, directly assign output to input | ||
if (null === $types || count($types) == 0) { | ||
$statements[] = new Expr\Assign($output, $input); | ||
|
||
continue; | ||
} | ||
|
||
// If there is multiple types, we need to know which one we must normalize | ||
$conditionNeeded = (boolean)(count($types) > 1); | ||
$noAssignment = true; | ||
|
||
foreach ($types as $type) { | ||
if (!$this->typeHydrateAstGenerator->supportsGeneration($type)) { | ||
continue; | ||
} | ||
|
||
$noAssignment = false; | ||
$statements = array_merge($statements, $this->typeHydrateAstGenerator->generate($type, array_merge($context, [ | ||
'input' => $input, | ||
'output' => $output, | ||
'condition' => $conditionNeeded | ||
]))); | ||
} | ||
|
||
// If nothing has been assigned, we directly put input into output | ||
if ($noAssignment) { | ||
$statements[] = new Expr\Assign($output, $input); | ||
} | ||
} | ||
|
||
$statements[] = new Stmt\Return_($dataVariable); | ||
|
||
return $statements; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsGeneration($object) | ||
{ | ||
return (is_string($object) && class_exists($object)); | ||
} | ||
|
||
/** | ||
* Create the assign statement | ||
* | ||
* @param Expr\Variable $dataVariable Variable to use | ||
* | ||
* @return Expr\Assign An assignment for the variable | ||
*/ | ||
abstract protected function getAssignStatement($dataVariable); | ||
|
||
|
||
/** | ||
* Create the sub assign variable statement | ||
* | ||
* @param Expr\Variable $dataVariable Variable to use | ||
* @param string $property Property name for object or array dimension | ||
* | ||
* @return Expr\ArrayDimFetch|Expr\PropertyFetch | ||
*/ | ||
abstract protected function getSubAssignVariableStatement($dataVariable, $property); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/AstGenerator/Hydrate/StdClassHydrateGenerator.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 <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\Hydrate; | ||
|
||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Expr; | ||
|
||
/** | ||
* Create AST Statement to normalize a Class into a stdClassObject | ||
* | ||
* @author Joel Wurtz <joel.wurtz@gmail.com> | ||
*/ | ||
class StdClassHydrateGenerator extends HydrateFromObjectGenerator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAssignStatement($dataVariable) | ||
{ | ||
return new Expr\Assign($dataVariable, new Expr\New_(new Name("\\stdClass"))); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getSubAssignVariableStatement($dataVariable, $property) | ||
{ | ||
return new Expr\PropertyFetch($dataVariable, sprintf("{'%s'}", $property)); | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
src/Symfony/Component/AstGenerator/Tests/Hydrate/ArrayHydrateGeneratorTest.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,110 @@ | ||
<?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\Hydrate; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use Prophecy\Argument; | ||
use Symfony\Component\AstGenerator\AstGeneratorInterface; | ||
use Symfony\Component\AstGenerator\Hydrate\ArrayHydrateGenerator; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class ArrayHydrateGeneratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var Standard */ | ||
protected $printer; | ||
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 could be private. |
||
|
||
public function setUp() | ||
{ | ||
$this->printer = new Standard(); | ||
} | ||
|
||
public function testHydrateGenerator() | ||
{ | ||
$propertyInfoExtractor = $this->prophesize(PropertyInfoExtractorInterface::class); | ||
$propertyInfoExtractor->getProperties(Dummy::class, Argument::type('array'))->willReturn(['foo', 'bar']); | ||
$propertyInfoExtractor->isReadable(Dummy::class, 'foo', Argument::type('array'))->willReturn(true); | ||
$propertyInfoExtractor->isReadable(Dummy::class, 'bar', Argument::type('array'))->willReturn(false); | ||
$propertyInfoExtractor->getTypes(Dummy::class, 'foo', Argument::type('array'))->willReturn([ | ||
new Type('string') | ||
]); | ||
$hydrateGenerator = new ArrayHydrateGenerator($propertyInfoExtractor->reveal(), new DummyTypeGenerator()); | ||
|
||
$this->assertTrue($hydrateGenerator->supportsGeneration(Dummy::class)); | ||
|
||
$dummyObject = new Dummy(); | ||
$dummyObject->foo = "test"; | ||
|
||
$dummyArray = eval($this->printer->prettyPrint($hydrateGenerator->generate(Dummy::class, [ | ||
'input' => new Expr\Variable('dummyObject') | ||
]))); | ||
|
||
$this->assertInternalType('array', $dummyArray); | ||
$this->assertArrayHasKey('foo', $dummyArray); | ||
$this->assertEquals('test', $dummyArray['foo']); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\AstGenerator\Exception\MissingContextException | ||
*/ | ||
public function testNoInput() | ||
{ | ||
$propertyInfoExtractor = $this->prophesize(PropertyInfoExtractorInterface::class); | ||
$hydrateGenerator = new ArrayHydrateGenerator($propertyInfoExtractor->reveal(), new DummyTypeGenerator()); | ||
$hydrateGenerator->generate(Dummy::class); | ||
} | ||
} | ||
|
||
class Dummy | ||
{ | ||
public $foo; | ||
|
||
public $bar; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getFoo() | ||
{ | ||
return $this->foo; | ||
} | ||
|
||
/** | ||
* @param mixed $bar | ||
*/ | ||
public function setBar($bar) | ||
{ | ||
$this->bar = $bar; | ||
} | ||
} | ||
|
||
class DummyTypeGenerator implements AstGeneratorInterface | ||
{ | ||
public function generate($object, array $context = []) | ||
{ | ||
if (!isset($context['input'])) { | ||
throw new \Exception('no input'); | ||
} | ||
|
||
if (!isset($context['output'])) { | ||
throw new \Exception('no output'); | ||
} | ||
|
||
return [new Expr\Assign($context['output'], $context['input'])]; | ||
} | ||
|
||
public function supportsGeneration($object) | ||
{ | ||
return true; | ||
} | ||
} |
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.
0 === count($type)