Skip to content

[JsonEncoder] Allow to warm up object and list #59536

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
Jan 25, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,11 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu
}
);
}
$container->registerAttributeForAutoconfiguration(JsonEncodable::class, static function (ChildDefinition $definition): void {
$definition->addTag('json_encoder.encodable');
$container->registerAttributeForAutoconfiguration(JsonEncodable::class, static function (ChildDefinition $definition, JsonEncodable $attribute): void {
$definition->addTag('json_encoder.encodable', [
'object' => $attribute->asObject,
'list' => $attribute->asList,
]);
$definition->addTag('container.excluded');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public function testWarmupEncodableClasses()
static::getContainer()->get('json_encoder.cache_warmer.encoder_decoder.alias')->warmUp(static::getContainer()->getParameter('kernel.cache_dir'));

$this->assertFileExists($encodersDir);
$this->assertCount(1, glob($encodersDir.'/*'));
$this->assertCount(2, glob($encodersDir.'/*'));
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/JsonEncoder/Attribute/JsonEncodable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
#[\Attribute(\Attribute::TARGET_CLASS)]
final class JsonEncodable
{
public function __construct(
public bool $asObject = true,
public bool $asList = true,
) {
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/JsonEncoder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ CHANGELOG

* Introduce the component as experimental
* Add native PHP lazy ghost support
* Allow to select the warmup of object and list in `JsonEncodable` and `json_encoder.encodable`
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ final class EncoderDecoderCacheWarmer implements CacheWarmerInterface
private DecoderGenerator $decoderGenerator;

/**
* @param iterable<class-string> $encodableClassNames
* @param iterable<class-string, array{object: bool, list: bool}> $encodable
*/
public function __construct(
private iterable $encodableClassNames,
private iterable $encodable,
PropertyMetadataLoaderInterface $encodePropertyMetadataLoader,
PropertyMetadataLoaderInterface $decodePropertyMetadataLoader,
string $encodersDir,
Expand All @@ -49,11 +49,20 @@ public function __construct(

public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
foreach ($this->encodableClassNames as $className) {
$type = Type::object($className);
foreach ($this->encodable as $className => $encodable) {
if ($encodable['object']) {
$type = Type::object($className);

$this->warmUpEncoder($type);
$this->warmUpDecoders($type);
$this->warmUpEncoder($type);
$this->warmUpDecoders($type);
}

if ($encodable['list']) {
$type = Type::list(Type::object($className));

$this->warmUpEncoder($type);
$this->warmUpDecoders($type);
}
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Sets the encodable classes to the services that need them.
* Sets the encodable metadata to the services that need them.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
Expand All @@ -27,27 +27,30 @@ public function process(ContainerBuilder $container): void
return;
}

$encodableClassNames = [];
$encodable = [];

// retrieve concrete services tagged with "json_encoder.encodable" tag
foreach ($container->getDefinitions() as $id => $definition) {
if (!$definition->hasTag('json_encoder.encodable')) {
if (!$tag = ($definition->getTag('json_encoder.encodable')[0] ?? null)) {
continue;
}

if (($className = $container->getDefinition($id)->getClass()) && !$container->getDefinition($id)->isAbstract()) {
$encodableClassNames[] = $className;
$encodable[$className] = [
'object' => $tag['object'],
'list' => $tag['list'],
];
}

$container->removeDefinition($id);
}

$container->getDefinition('.json_encoder.cache_warmer.encoder_decoder')
->replaceArgument(0, $encodableClassNames);
->replaceArgument(0, $encodable);

if ($container->hasDefinition('.json_encoder.cache_warmer.lazy_ghost')) {
$container->getDefinition('.json_encoder.cache_warmer.lazy_ghost')
->replaceArgument(0, $encodableClassNames);
->replaceArgument(0, array_keys($encodable));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\JsonEncoder\CacheWarmer\EncoderDecoderCacheWarmer;
use Symfony\Component\JsonEncoder\Mapping\PropertyMetadataLoader;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Model\ClassicDummy;
use Symfony\Component\JsonEncoder\Tests\Fixtures\Model\DummyWithNameAttributes;
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;

class EncoderDecoderCacheWarmerTest extends TestCase
Expand Down Expand Up @@ -42,24 +43,36 @@ protected function setUp(): void

public function testWarmUp()
{
$this->cacheWarmer()->warmUp('useless');
$this->cacheWarmer([
ClassicDummy::class => ['object' => true, 'list' => true],
DummyWithNameAttributes::class => ['object' => true, 'list' => false],
])->warmUp('useless');

$this->assertSame([
\sprintf('%s/5acb3571777e02a2712fb9a9126a338f.json.php', $this->encodersDir),
\sprintf('%s/d147026bb5d25e5012afcdc1543cf097.json.php', $this->encodersDir),
\sprintf('%s/de878efdd0bf652bdd72d1dc95f6d80d.json.php', $this->encodersDir),
], glob($this->encodersDir.'/*'));

$this->assertSame([
\sprintf('%s/5acb3571777e02a2712fb9a9126a338f.json.php', $this->decodersDir),
\sprintf('%s/5acb3571777e02a2712fb9a9126a338f.json.stream.php', $this->decodersDir),
\sprintf('%s/d147026bb5d25e5012afcdc1543cf097.json.php', $this->decodersDir),
\sprintf('%s/d147026bb5d25e5012afcdc1543cf097.json.stream.php', $this->decodersDir),
\sprintf('%s/de878efdd0bf652bdd72d1dc95f6d80d.json.php', $this->decodersDir),
\sprintf('%s/de878efdd0bf652bdd72d1dc95f6d80d.json.stream.php', $this->decodersDir),
], glob($this->decodersDir.'/*'));
}

private function cacheWarmer(): EncoderDecoderCacheWarmer
/**
* @param array<class-string, array{object: bool, list: bool}> $encodableClasses
*/
private function cacheWarmer(array $encodableClasses = []): EncoderDecoderCacheWarmer
{
$typeResolver = TypeResolver::create();

return new EncoderDecoderCacheWarmer(
[ClassicDummy::class],
$encodableClasses,
new PropertyMetadataLoader($typeResolver),
new PropertyMetadataLoader($typeResolver),
$this->encodersDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function testSetEncodableClassNames()
$container->register('.json_encoder.cache_warmer.encoder_decoder')->setArguments([null]);
$container->register('.json_encoder.cache_warmer.lazy_ghost')->setArguments([null]);

$container->register('encodable')->setClass('Foo')->addTag('json_encoder.encodable');
$container->register('abstractEncodable')->setClass('Bar')->addTag('json_encoder.encodable')->setAbstract(true);
$container->register('encodable')->setClass('Foo')->addTag('json_encoder.encodable', ['object' => true, 'list' => true]);
$container->register('abstractEncodable')->setClass('Bar')->addTag('json_encoder.encodable', ['object' => true, 'list' => true])->setAbstract(true);
$container->register('notEncodable')->setClass('Baz');

$pass = new EncodablePass();
Expand All @@ -35,9 +35,7 @@ public function testSetEncodableClassNames()
$encoderDecoderCacheWarmer = $container->getDefinition('.json_encoder.cache_warmer.encoder_decoder');
$lazyGhostCacheWarmer = $container->getDefinition('.json_encoder.cache_warmer.lazy_ghost');

$expectedEncodableClassNames = ['Foo'];

$this->assertSame($expectedEncodableClassNames, $encoderDecoderCacheWarmer->getArgument(0));
$this->assertSame($expectedEncodableClassNames, $lazyGhostCacheWarmer->getArgument(0));
$this->assertSame(['Foo' => ['object' => true, 'list' => true]], $encoderDecoderCacheWarmer->getArgument(0));
$this->assertSame(['Foo'], $lazyGhostCacheWarmer->getArgument(0));
}
}
Loading