8000 [AstGenerator] [WIP] New Component, add normalizer generator by joelwurtz · Pull Request #17516 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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
Closed
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
GuilhemN authored and joelwurtz committed Aug 5, 2016
commit 1e07e3efac45ee34f16d04f570aec42ed74b20f5
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use Symfony\Component\AstGenerator\AstGeneratorInterface;
use Symfony\Component\AstGenerator\Exception\MissingContextException;
use Symfony\Component\PropertyInfo\Type;

/**
* Generate hydration of normalizable object type.
*
* @author Guilhem N. <egetick@gmail.com>
*/
class DenormalizableObjectTypeGenerator implements AstGeneratorInterface
{
/**
* {@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');
}

if (!isset($context['denormalizer']) || !($context['denormalizer'] instanceof Expr)) {
throw new MissingContextException('Denormalizer variable not defined or not an Expr in generation context');
}

$denormalizationArgs = array(
new Arg($context['input']),
new Arg(new Scalar\String_($object->getClassName())),
);
if (isset($context['format'])) {
$denormalizationArgs[] = new Arg($context['format']);
} else {
$denormalizationArgs[] = new Arg(new Expr\ConstFetch(new Name('null')));
}
if (isset($context['context'])) {
$denormalizationArgs[] = new Arg($context['context']);
}

$assign = [
new Expr\Assign($context['output'], new Expr\MethodCall(
$context['denormalizer'],
'denormalize',
$denormalizationArgs
))
];

if (isset($context['condition']) && $context['condition']) {
return [new Stmt\If_(
new Expr\BinaryOp\LogicalAnd(
new Expr\MethodCall(
$context['denormalizer'],
'supportsDenormalization',
$normalizationArgs
)
),
[
'stmts' => $assign
]
)];
}

return $assign;
}

/**
* {@inheritdoc}
*/
public function supportsGeneration($object)
{
return $object instanceof Type && Type::BUILTIN_TYPE_OBJECT === $object->getBuiltinType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,35 @@ public function generate($object, array $context = [])
}

if (!isset($context['normalizer']) || !($context['normalizer'] instanceof Expr)) {
throw new MissingContextException('Denormalizer variable not defined or not an Expr in generation context');
throw new MissingContextException('Normalizer variable not defined or not an Expr in generation context');
}

$normalizationArgs = array(new Arg($context['input']));
if (isset($context['format'])) {
$normalizationArgs[] = new Arg($context['format']);
} else {
$normalizationArgs[] = new Arg(new Expr\ConstFetch(new Name('null')));
}
if (isset($context['context'])) {
$normalizationArgs[] = new Arg($context['context']);
}

$assign = [
new Expr\Assign($context['output'], $context['input'])
new Expr\Assign($context['output'], new Expr\MethodCall(
$context['normalizer'],
'normalize',
$normalizationArgs
))
];

if (isset($context['condition']) && $context['condition']) {
return [new Stmt\If_(
new Expr\BinaryOp\LogicalAnd(
new Expr\FuncCall(
'is_object',
array(
new Arg($context['input']),
)
),
new Expr\Instanceof_(new Expr\Variable('data'), new Name($object->getClassName())),
new Expr\MethodCall(
$context['normalizer'],
'supportsNormalization'
call_user_func(function() use ($context) {
$args = array(new Arg($context['input']));
if (isset($context['format'])) {
$args[] = new Arg($context['input']);
} else {
$args[] = new Arg(new Expr\ConstFetch(new Name('null')));
}
if (isset($context['context'])) {
$args[] = new Arg($context['context']);
}

return $args;
})
'supportsNormalization',
$normalizationArgs
)
),
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\AstGenerator\Normalizer;

use PhpParser\Comment;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -58,6 +59,10 @@ public function generate($object, array $context = [])
new Name($context['name']),
[
'stmts' => [
new Stmt\Use_(array(
new Name('\Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait'),
new Name('\Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait'),
)),
$this->createSupportsNormalizationMethod($object),
$this->createSupportsDenormalizationMethod($object),
$this->createNormalizeMethod($object, array_merge($context, [
Expand All @@ -71,8 +76,11 @@ public function generate($object, array $context = [])
new Name('\Symfony\Component\Serializer\Normalizer\DenormalizerInterface'),
new Name('\Symfony\Component\Serializer\Normalizer\NormalizerInterface'),
new Name('\Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface'),
new Name('\Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface'),
],
'uses' => new Name('\Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait'),
],
[
'comments' => [new Comment("/**\n * This class is generated.\n * Please do not update it manually.\n */")],
]
)];
}
Expand Down Expand Up @@ -163,10 +171,12 @@ protected function createNormalizeMethod($class, array $context = [])
'stmts' => array_merge($this->normalizeStatementsGenerator->generate($class, array_merge($context, [
'input' => $input,
'output' => $output,
'normalizer' => new Expr\MethodCall(
new Expr\Variable('$this'),
'getNormalizer'
)
'normalizer' => new Expr\PropertyFetch(
new Expr\Variable('this'),
'normalizer'
),
'format' => new Expr\Variable(new Name('format')),
'context' => new Expr\Variable(new Name('context')),
])), [
new Stmt\Return_($output)
])
Expand Down Expand Up @@ -197,6 +207,12 @@ protected function createDenormalizeMethod($class, array $context = [])
'stmts' => array_merge($this->denormalizeStatementsGenerator->generate($class, array_merge($context, [
'input' => $input,
'output' => $output,
'denormalizer' => new Expr\PropertyFetch(
new Expr\Variable('this'),
'denormalizer'
),
'format' => new Expr\Variable(new Name('format')),
'context' => new Expr\Variable(new Name('context')),
])), [
new Stmt\Return_($output),
]),
Expand Down
0