Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 1 commit into from
Dec 10, 2024
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: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
/src/Symfony/Component/Translation/Bridge export-ignore
/src/Symfony/Component/Emoji/Resources/data/* linguist-generated=true
/src/Symfony/Component/Intl/Resources/data/*/* linguist-generated=true
/src/Symfony/Component/JsonEncoder/Tests/Fixtures/encoder/* linguist-generated=true
/src/Symfony/Component/JsonEncoder/Tests/Fixtures/decoder/* linguist-generated=true
/src/Symfony/**/.github/workflows/close-pull-request.yml linguist-generated=true
/src/Symfony/**/.github/PULL_REQUEST_TEMPLATE.md linguist-generated=true
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"symfony/http-foundation": "self.version",
"symfony/http-kernel": "self.version",
"symfony/intl": "self.version",
"symfony/json-encoder": "self.version",
"symfony/ldap": "self.version",
"symfony/lock": "self.version",
"symfony/mailer": "self.version",
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/JsonEncoder/.gitattributes
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/JsonEncoder/.gitignore
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 src/Symfony/Component/JsonEncoder/Attribute/Denormalizer.php
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 src/Symfony/Component/JsonEncoder/Attribute/EncodedName.php
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 src/Symfony/Component/JsonEncoder/Attribute/Normalizer.php
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;
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/JsonEncoder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.3
---

* Introduce the component as experimental
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]);
}
}
}
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),
));
}
}
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;
}
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;
}
}
Loading
Loading