Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalizers should be cacheable with CacheableSupportsMethodInterface #93

Merged
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

## 4.1.0 - 2019-01-24

* [Jane][OpenAPI] Add support for cacheable normalizer
* [Jane] Added `use-cacheable-supports-method` option to add CacheableSupportsMethodInterface to your Normalizers.

## 4.0.4 - 2018-10-19

Expand Down
3 changes: 3 additions & 0 deletions documentation/JsonSchema/generate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Other options are available to customize the generated code:

* ``reference``: A boolean which indicate to add the support for `JSON Reference`_ into the generated code.
* ``date-format``: A date format to specify how the generated code should encode and decode ``\DateTime`` object to string
* ``use-fixer``: A boolean which indicate if we make a first cs-fix after code generation
* ``fixer-config-file``: A string to specify where to find the custom configuration for the cs-fixer after code generation
* ``use-cacheable-supports-method``: A boolean which indicate if we use ``CacheableSupportsMethodInterface`` interface to improve caching performances when used with Symfony Serializer.

Multi schemas
-------------
Expand Down
3 changes: 3 additions & 0 deletions documentation/OpenAPI/generate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Other options are available to customize the generated code:
not respecting some standards (nullable field as an example)
* ``async``: A boolean (false by default) which allows to generate a full asynchronous client using `Amp`_ with `Artax`_
client, see :doc:`/OpenAPI/async` fore more information.
* ``use-fixer``: A boolean which indicate if we make a first cs-fix after code generation
* ``fixer-config-file``: A string to specify where to find the custom configuration for the cs-fixer after code generation
* ``use-cacheable-supports-method``: A boolean which indicate if we use ``CacheableSupportsMethodInterface`` interface to improve caching performances when used with Symfony Serializer.

.. _`JSON Reference`: https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html
.. _Amp: https://amphp.org/
Expand Down
2 changes: 2 additions & 0 deletions src/JsonSchema/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected function resolveConfiguration(array $options = [])
'date-format' => \DateTime::RFC3339,
'use-fixer' => true,
'fixer-config-file' => null,
'use-cacheable-supports-method' => null,
]);

if (\array_key_exists('json-schema-file', $options)) {
Expand Down Expand Up @@ -108,6 +109,7 @@ protected function resolveSchema($schema, array $options = [])
'strict',
'use-fixer',
'fixer-config-file',
'use-cacheable-supports-method',
]);

$optionsResolver->setRequired([
Expand Down
36 changes: 29 additions & 7 deletions src/JsonSchema/Generator/Normalizer/NormalizerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,29 @@ trait NormalizerGenerator
*/
abstract protected function getNaming();

protected function createNormalizerClass($name, $methods)
protected function createNormalizerClass($name, $methods, $useCacheableSupportsMethod = false)
{
$traits = [
new Stmt\TraitUse([new Name('DenormalizerAwareTrait')]),
new Stmt\TraitUse([new Name('NormalizerAwareTrait')]),
];

$implements = [
new Name('DenormalizerInterface'),
new Name('NormalizerInterface'),
new Name('DenormalizerAwareInterface'),
new Name('NormalizerAwareInterface'),
];

if ($useCacheableSupportsMethod) {
$implements[] = new Name('CacheableSupportsMethodInterface');
}

return new Stmt\Class_(
new Name($this->getNaming()->getClassName($name)),
[
'stmts' => array_merge($traits, $methods),
'implements' => [
new Name('DenormalizerInterface'),
new Name('NormalizerInterface'),
new Name('DenormalizerAwareInterface'),
new Name('NormalizerAwareInterface'),
],
'implements' => $implements,
]
);
}
Expand Down Expand Up @@ -139,6 +145,22 @@ protected function createNormalizeMethod($modelFqdn, Context $context, ClassGues
]);
}

/**
* Create method to say that hasCacheableSupportsMethod is supported.
*
* @return Stmt\ClassMethod
*/
protected function createHasCacheableSupportsMethod()
{
return new Stmt\ClassMethod('hasCacheableSupportsMethod', [
'type' => Stmt\Class_::MODIFIER_PUBLIC,
'returnType' => 'bool',
'stmts' => [
new Stmt\Return_(new Expr\ConstFetch(new Name('true'))),
],
]);
}

protected function normalizeMethodStatements(Expr\Variable $dataVariable, ClassGuess $classGuess, Context $context): array
{
return [
Expand Down
36 changes: 31 additions & 5 deletions src/JsonSchema/Generator/NormalizerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@ class NormalizerGenerator implements GeneratorInterface
*/
protected $useReference;

/**
* @var bool|null Whether to use the CacheableSupportsMethodInterface interface, for >sf 4.1
*/
protected $useCacheableSupportsMethod;

/**
* @param Naming $naming Naming Service
* @param bool $useReference Whether to generate the JSON Reference system
* @param bool $useCache Whether to use the CacheableSupportsMethodInterface interface, for >sf 4.1
*/
public function __construct(Naming $naming, $useReference = true)
public function __construct(Naming $naming, $useReference = true, $useCacheableSupportsMethod = null)
{
$this->naming = $naming;
$this->useReference = $useReference;
$this->useCacheableSupportsMethod = $this->canUseCacheableSupportsMethod($useCacheableSupportsMethod);
}

/**
Expand Down Expand Up @@ -62,13 +69,18 @@ public function generate(Schema $schema, string $className, Context $context)
$methods[] = $this->createDenormalizeMethod($modelFqdn, $context, $class);
$methods[] = $this->createNormalizeMethod($modelFqdn, $context, $class);

if ($this->useCacheableSupportsMethod) {
$methods[] = $this->createHasCacheableSupportsMethod();
}

$normalizerClass = $this->createNormalizerClass(
$class->getName() . 'Normalizer',
$methods
$methods,
$this->useCacheableSupportsMethod
);
$classes[] = $normalizerClass->name;

$namespace = new Stmt\Namespace_(new Name($schema->getNamespace() . '\\Normalizer'), [
$useStmts = [
new Stmt\Use_([new Stmt\UseUse(new Name('Jane\JsonSchemaRuntime\Reference'))]),
new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\Component\Serializer\Exception\InvalidArgumentException'))]),
new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface'))]),
Expand All @@ -77,8 +89,15 @@ public function generate(Schema $schema, string $className, Context $context)
new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface'))]),
new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait'))]),
new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\Component\Serializer\Normalizer\NormalizerInterface'))]),
$normalizerClass,
]);
];

if ($this->useCacheableSupportsMethod) {
$useStmts[] = new Stmt\Use_([new Stmt\UseUse(new Name('Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface'))]);
}

$useStmts[] = $normalizerClass;

$namespace = new Stmt\Namespace_(new Name($schema->getNamespace() . '\\Normalizer'), $useStmts);

$schema->addFile(new File($schema->getDirectory() . '/Normalizer/' . $normalizerClass->name . '.php', $namespace, self::FILE_TYPE_NORMALIZER));
}
Expand All @@ -92,6 +111,13 @@ public function generate(Schema $schema, string $className, Context $context)
));
}

protected function canUseCacheableSupportsMethod(?bool $useCacheableSupportsMethod): bool
{
return
true === $useCacheableSupportsMethod ||
(null === $useCacheableSupportsMethod && class_exists('Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface'));
}

protected function createNormalizerFactoryClass($classes)
{
$statements = [
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Jane.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static function build($options = [])
$chainGuesser = JsonSchemaGuesserFactory::create($serializer, $options);
$naming = new Naming();
$modelGenerator = new ModelGenerator($naming);
$normGenerator = new NormalizerGenerator($naming, $options['reference']);
$normGenerator = new NormalizerGenerator($naming, $options['reference'], $options['use-cacheable-supports-method'] ?? false);

$self = new self($serializer, $chainGuesser, $naming, $options['strict']);
$self->addGenerator($modelGenerator);
Expand Down
2 changes: 2 additions & 0 deletions src/OpenApi/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected function resolveConfiguration(array $options = [])
'strict' => true,
'use-fixer' => true,
'fixer-config-file' => null,
'use-cacheable-supports-method' => null,
]);

if (\array_key_exists('openapi-file', $options)) {
Expand Down Expand Up @@ -112,6 +113,7 @@ protected function resolveSchema($schema, array $options = [])
'strict',
'use-fixer',
'fixer-config-file',
'use-cacheable-supports-method',
]);

$optionsResolver->setDefault('version', 2);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/JaneOpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static function build(array $options = [])
$generators = GeneratorFactory::build($serializer, $options);
$naming = new Naming();
$modelGenerator = new ModelGenerator($naming);
$normGenerator = new NormalizerGenerator($naming, $options['reference'] ?? false);
$normGenerator = new NormalizerGenerator($naming, $options['reference'] ?? false, $options['use-cacheable-supports-method'] ?? false);

$self = new self(
$schemaParser,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'openapi-file' => __DIR__ . '/swagger.json',
'namespace' => 'Jane\OpenApi\Tests\Expected',
'directory' => __DIR__ . '/generated',
'use-cacheable-supports-method' => true,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file has been auto generated by Jane,
*
* Do no edit it directly.
*/

namespace Jane\OpenApi\Tests\Expected;

class Client extends \Jane\OpenApiRuntime\Client\Psr7HttplugClient
{
public static function create($httpClient = null)
{
if (null === $httpClient) {
$httpClient = \Http\Discovery\HttpClientDiscovery::find();
}
$messageFactory = \Http\Discovery\MessageFactoryDiscovery::find();
$streamFactory = \Http\Discovery\StreamFactoryDiscovery::find();
$serializer = new \Symfony\Component\Serializer\Serializer(\Jane\OpenApi\Tests\Expected\Normalizer\NormalizerFactory::create(), [new \Symfony\Component\Serializer\Encoder\JsonEncoder(new \Symfony\Component\Serializer\Encoder\JsonEncode(), new \Symfony\Component\Serializer\Encoder\JsonDecode())]);

return new static($httpClient, $messageFactory, $serializer, $streamFactory);
}
}
Loading