-
-
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
Add hydration of object
- Loading branch information
commit 365bdb20bad19e85cc5c4083cf5df18bde5a2496
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
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/AstGenerator/Hydrate/ObjectHydrateFromArrayGenerator.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,26 @@ | ||
<?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\Expr; | ||
use PhpParser\Node\Scalar; | ||
|
||
class ObjectHydrateFromArrayGenerator extends ObjectHydrateGenerator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createInputExpr(Expr\Variable $inputVariable, $property) | ||
{ | ||
return new Expr\ArrayDimFetch($inputVariable, new Scalar\String_($property)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/AstGenerator/Hydrate/ObjectHydrateFromStdClassGenerator.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,25 @@ | ||
<?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\Expr; | ||
|
||
class ObjectHydrateFromStdClassGenerator extends ObjectHydrateGenerator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createInputExpr(Expr\Variable $inputVariable, $property) | ||
{ | ||
return new Expr\PropertyFetch($inputVariable, sprintf("{'%s'}", $property)); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
src/Symfony/Component/AstGenerator/Hydrate/ObjectHydrateGenerator.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,129 @@ | ||
<?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\Arg; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Expr; | ||
use Symfony\Component\AstGenerator\AstGeneratorInterface; | ||
use Symfony\Component\AstGenerator\Exception\MissingContextException; | ||
use Symfony\Component\AstGenerator\UniqueVariableScope; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; | ||
|
||
/** | ||
* Abstract class to generate hydration of object from data | ||
* | ||
* @author Joel Wurtz <joel.wurtz@gmail.com> | ||
*/ | ||
abstract class ObjectHydrateGenerator implements AstGeneratorInterface | ||
{ | ||
/** @var PropertyInfoExtractorInterface Extract list of properties from a class */ | ||
protected $propertyInfoExtractor; | ||
|
||
/** @var AstGeneratorInterface Generator for hydration 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']) || !($context['input'] instanceof Expr\Variable)) { | ||
throw new MissingContextException('Input variable not defined or not a Expr\Variable in generation context'); | ||
} | ||
|
||
if (!isset($context['output']) || !($context['output'] instanceof Expr\Variable)) { | ||
throw new MissingContextException('Output variable not defined or not a Expr\Variable in generation context'); | ||
} | ||
|
||
$uniqueVariableScope = isset($context['unique_variable_scope']) ? $context['unique_variable_scope'] : new UniqueVariableScope(); | ||
$statements = [ | ||
new Expr\Assign($context['output'], new Expr\New_(new Name("\\".$object))), | ||
]; | ||
|
||
foreach ($this->propertyInfoExtractor->getProperties($object, $context) as $property) { | ||
// Only hydrate writable property | ||
if (!$this->propertyInfoExtractor->isWritable($object, $property, $context)) { | ||
continue; | ||
} | ||
|
||
$output = new Expr\Variable($uniqueVariableScope->getUniqueName('output')); | ||
$input = $this->createInputExpr($context['input'], $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) { | ||
// @TODO Have property info extractor extract the way of writing a property (public or method with method name) | ||
$statements[] = new Expr\MethodCall($context['output'], 'set'.ucfirst($property), [ | ||
new Arg($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) { | ||
// @TODO Have property info extractor extract the way of writing a property (public or method with method name) | ||
$statements[] = new Expr\MethodCall($context['output'], 'set'.ucfirst($property), [ | ||
new Arg($input) | ||
]); | ||
} else { | ||
$statements[] = new Expr\MethodCall($context['output'], 'set'.ucfirst($property), [ | ||
new Arg($output) | ||
]); | ||
} | ||
} | ||
|
||
return $statements; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsGeneration($object) | ||
{ | ||
return is_string($object) && class_exists($object); | ||
} | ||
|
||
/** | ||
* Create the input expression for a specific property | ||
* | ||
* @param Expr\Variable $inputVariable Input variable of data | ||
* @param string $property Property to fetch | ||
* | ||
* @return Expr | ||
*/ | ||
abstract protected function createInputExpr(Expr\Variable $inputVariable, $property); | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/AstGenerator/Hydrate/Type/CollectionTypeGenerator.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,83 @@ | ||
<?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\Type; | ||
|
||
use PhpParser\Node\Expr; | ||
use Symfony\Component\AstGenerator\AstGeneratorInterface; | ||
use Symfony\Component\AstGenerator\Exception\MissingContextException; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* Abstract class to generate collection hydration | ||
* | ||
* @author Joel Wurtz <joel.wurtz@gmail.com> | ||
*/ | ||
class CollectionTypeGenerator implements AstGeneratorInterface | ||
{ | ||
const COLLECTION_WITH_STDCLASS = 0; | ||
const COLLECTION_WITH_ARRAY = 1; | ||
|
||
/** | ||
* CollectionTypeGenerator constructor. | ||
* | ||
* @param AstGeneratorInterface $subValueTypeGenerator Generator for the value of the collection | ||
* @param int|null $fromCollectionType From collection type to generate, array or stdClass, use null | ||
* to have a dynamic choice depending on the type of the collection key (where int will be array and string stdClass) | ||
* @param int|null $toCollectionType To collection type to generate, array or stdClass, use null | ||
* to have a dynamic choice depending on the type of the collection key (where int will be array and string stdClass) | ||
*/ | ||
public function __construct(AstGeneratorInterface $subValueTypeGenerator, $fromCollectionType = null, $toCollectionType = null) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param Type $object A type extracted with PropertyInfo component | ||
*/ | ||
public function generate($object, array $context = []) | ||
{ | ||
if (!isset($context['input']) || !($context['input'] instanceof Expr)) { | ||
throw new MissingContextException('Input variable not defined or not an Expr in generation context'); | ||
} | ||
|
||
if (!isset($context['output']) || !($context['output'] instanceof Expr)) { | ||
throw new MissingContextException('Output variable not defined or not an Expr in generation context'); | ||
} | ||
|
||
$statements = [ | ||
new Expr\Assign($context['output'], $this->createCollectionAssignStatement()), | ||
]; | ||
|
||
$loopValueVar = new Expr\Variable($context->getUniqueVariableName('value')); | ||
|
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsGeneration($object) | ||
{ | ||
return $object instanceof Type && $object->isCollection(); | ||
} | ||
|
||
/** | ||
* Create the collection assign statement | ||
* | ||
* @return Expr | ||
*/ | ||
protected function createCollectionAssignStatement() | ||
{ | ||
|
||
} | ||
} |
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
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.
properties