-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[JsonEncoder][Serializer] Introducing the component #51718
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
Merged
Merged
Changes from all commits
Commits
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
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
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
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/JsonEncoder/Attribute/Denormalizer.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,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\Attribute; | ||
|
||
/** | ||
* Defines a callable or a {@see \Symfony\Component\JsonEncoder\Decode\Denormalizer\DenormalizerInterface} service id | ||
* that will be used to denormalize the property data during decoding. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @experimental | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class Denormalizer | ||
{ | ||
private string|\Closure $denormalizer; | ||
|
||
/** | ||
* @param string|(callable(mixed, array<string, mixed>?): mixed)|(callable(mixed): mixed) $denormalizer | ||
*/ | ||
public function __construct(mixed $denormalizer) | ||
{ | ||
if (\is_callable($denormalizer)) { | ||
$denormalizer = \Closure::fromCallable($denormalizer); | ||
} | ||
|
||
$this->denormalizer = $denormalizer; | ||
} | ||
|
||
public function getDenormalizer(): string|\Closure | ||
{ | ||
return $this->denormalizer; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/JsonEncoder/Attribute/EncodedName.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,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\Attribute; | ||
|
||
/** | ||
* Defines the encoded property name. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @experimental | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
final class EncodedName | ||
{ | ||
public function __construct( | ||
private string $name, | ||
) { | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/JsonEncoder/Attribute/Normalizer.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,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\Attribute; | ||
|
||
/** | ||
* Defines a callable or a {@see \Symfony\Component\JsonEncoder\Encode\Normalizer\NormalizerInterface} service id | ||
* that will be used to normalize the property data during encoding. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @experimental | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class Normalizer | ||
{ | ||
private string|\Closure $normalizer; | ||
|
||
/** | ||
* @param string|(callable(mixed, array<string, mixed>?): mixed)|(callable(mixed): mixed) $normalizer | ||
*/ | ||
public function __construct(mixed $normalizer) | ||
{ | ||
if (\is_callable($normalizer)) { | ||
$normalizer = \Closure::fromCallable($normalizer); | ||
} | ||
|
||
$this->normalizer = $normalizer; | ||
} | ||
|
||
public function getNormalizer(): string|\Closure | ||
{ | ||
return $this->normalizer; | ||
} | ||
} |
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 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.3 | ||
--- | ||
|
||
* Introduce the component as experimental |
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/JsonEncoder/CacheWarmer/EncoderDecoderCacheWarmer.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,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\CacheWarmer; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\JsonEncoder\Decode\DecoderGenerator; | ||
use Symfony\Component\JsonEncoder\Encode\EncoderGenerator; | ||
use Symfony\Component\JsonEncoder\Exception\ExceptionInterface; | ||
use Symfony\Component\JsonEncoder\Mapping\PropertyMetadataLoaderInterface; | ||
use Symfony\Component\TypeInfo\Type; | ||
|
||
/** | ||
* Generates encoders and decoders PHP files. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class EncoderDecoderCacheWarmer implements CacheWarmerInterface | ||
{ | ||
private EncoderGenerator $encoderGenerator; | ||
private DecoderGenerator $decoderGenerator; | ||
|
||
/** | ||
* @param iterable<class-string> $encodableClassNames | ||
*/ | ||
public function __construct( | ||
private iterable $encodableClassNames, | ||
PropertyMetadataLoaderInterface $encodePropertyMetadataLoader, | ||
PropertyMetadataLoaderInterface $decodePropertyMetadataLoader, | ||
string $encodersDir, | ||
string $decodersDir, | ||
bool $forceEncodeChunks = false, | ||
private LoggerInterface $logger = new NullLogger(), | ||
) { | ||
$this->encoderGenerator = new EncoderGenerator($encodePropertyMetadataLoader, $encodersDir, $forceEncodeChunks); | ||
$this->decoderGenerator = new DecoderGenerator($decodePropertyMetadataLoader, $decodersDir); | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
foreach ($this->encodableClassNames as $className) { | ||
$type = Type::object($className); | ||
|
||
$this->warmUpEncoder($type); | ||
$this->warmUpDecoders($type); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
private function warmUpEncoder(Type $type): void | ||
{ | ||
try { | ||
$this->encoderGenerator->generate($type); | ||
} catch (ExceptionInterface $e) { | ||
$this->logger->debug('Cannot generate "json" encoder for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); | ||
} | ||
} | ||
|
||
private function warmUpDecoders(Type $type): void | ||
{ | ||
try { | ||
$this->decoderGenerator->generate($type, decodeFromStream: false); | ||
} catch (ExceptionInterface $e) { | ||
$this->logger->debug('Cannot generate "json" decoder for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); | ||
} | ||
|
||
try { | ||
$this->decoderGenerator->generate($type, decodeFromStream: true); | ||
} catch (ExceptionInterface $e) { | ||
$this->logger->debug('Cannot generate "json" streaming decoder for "{type}": {exception}', ['type' => (string) $type, 'exception' => $e]); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/Symfony/Component/JsonEncoder/CacheWarmer/LazyGhostCacheWarmer.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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\CacheWarmer; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer; | ||
use Symfony\Component\JsonEncoder\Exception\RuntimeException; | ||
use Symfony\Component\VarExporter\ProxyHelper; | ||
|
||
/** | ||
* Generates lazy ghost {@see \Symfony\Component\VarExporter\LazyGhostTrait} | ||
* PHP files for $encodable types. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class LazyGhostCacheWarmer extends CacheWarmer | ||
{ | ||
private Filesystem $fs; | ||
|
||
/** | ||
* @param iterable<class-string> $encodableClassNames | ||
*/ | ||
public function __construct( | ||
private iterable $encodableClassNames, | ||
private string $lazyGhostsDir, | ||
) { | ||
$this->fs = new Filesystem(); | ||
} | ||
|
||
public function warmUp(string $cacheDir, ?string $buildDir = null): array | ||
{ | ||
if (!$this->fs->exists($this->lazyGhostsDir)) { | ||
$this->fs->mkdir($this->lazyGhostsDir); | ||
} | ||
|
||
foreach ($this->encodableClassNames as $className) { | ||
$this->warmClassLazyGhost($className); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param class-string $className | ||
*/ | ||
private function warmClassLazyGhost(string $className): void | ||
{ | ||
$path = \sprintf('%s%s%s.php', $this->lazyGhostsDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className)); | ||
|
||
try { | ||
$classReflection = new \ReflectionClass($className); | ||
} catch (\ReflectionException $e) { | ||
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
|
||
$this->writeCacheFile($path, \sprintf( | ||
'class %s%s', | ||
\sprintf('%sGhost', preg_replace('/\\\\/', '', $className)), | ||
ProxyHelper::generateLazyGhost($classReflection), | ||
)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/JsonEncoder/DataModel/DataAccessorInterface.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,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\DataModel; | ||
|
||
use PhpParser\Node\Expr; | ||
|
||
/** | ||
* Represents a way to access data on PHP. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
interface DataAccessorInterface | ||
{ | ||
/** | ||
* Converts to "nikic/php-parser" PHP expression. | ||
*/ | ||
public function toPhpExpr(): Expr; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/JsonEncoder/DataModel/Decode/BackedEnumNode.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,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\JsonEncoder\DataModel\Decode; | ||
|
||
use Symfony\Component\TypeInfo\Type\BackedEnumType; | ||
|
||
/** | ||
* Represents a backed enum in the data model graph representation. | ||
* | ||
* Backed enums are leaves in the data model tree. | ||
* | ||
* @author Mathias Arlaud <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class BackedEnumNode implements DataModelNodeInterface | ||
{ | ||
public function __construct( | ||
public BackedEnumType $type, | ||
) { | ||
} | ||
|
||
public function getIdentifier(): string | ||
{ | ||
return (string) $this->type; | ||
} | ||
|
||
public function getType(): BackedEnumType | ||
{ | ||
return $this->type; | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.