8000 [JsonEncoder] Add `Encodable` attribute · symfony/symfony@073610e · GitHub
[go: up one dir, main page]

Skip to content

Commit 073610e

Browse files
committed
[JsonEncoder] Add Encodable attribute
1 parent 3766937 commit 073610e

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
100100
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
101101
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
102+
use Symfony\Component\JsonEncoder\Attribute\Encodable;
102103
use Symfony\Component\JsonEncoder\Decode\Denormalizer\DenormalizerInterface as JsonEncoderDenormalizerInterface;
103104
use Symfony\Component\JsonEncoder\DecoderInterface as JsonEncoderDecoderInterface;
104105
use Symfony\Component\JsonEncoder\Encode\Normalizer\NormalizerInterface as JsonEncoderNormalizerInterface;
@@ -743,6 +744,10 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu
743744
}
744745
);
745746
}
747+
$container->registerAttributeForAutoconfiguration(Encodable::class, static function (ChildDefinition $definition): void {
748+
$definition->addTag('json_encoder.encodable');
749+
$definition->addTag('container.excluded');
750+
});
746751

747752
if (!$container->getParameter('kernel.debug')) {
748753
// remove tagged iterator argument for resource checkers

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/JsonEncoderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ public function testDecode()
4444

4545
$this->assertEquals($expected, $decoder->decode('{"@name": "DUMMY", "range": "0..1"}', Type::object(Dummy::class)));
4646
}
47+
48+
public function testWarmupEncodableClasses()
49+
{
50+
static::bootKernel(['test_case' => 'JsonEncoder']);
51+
52+
// clear already created encoders
53+
$encodersDir = \sprintf('%s/json_encoder/encoder/', static::getContainer()->getParameter('kernel.cache_dir'));
54+
if (is_dir($encodersDir)) {
55+
array_map('unlink', glob($encodersDir.'/*'));
56+
rmdir($encodersDir);
57+
}
58+
59+
static::getContainer()->get('json_encoder.cache_warmer.encoder_decoder.alias')->warmUp(static::getContainer()->getParameter('kernel.cache_dir'));
60+
61+
$this->assertFileExists($encodersDir);
62+
$this->assertCount(1, glob($encodersDir.'/*'));
63+
}
4764
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/JsonEncoder/Dto/Dummy.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\RangeNormalizer;
1515
use Symfony\Component\JsonEncoder\Attribute\Denormalizer;
16+
use Symfony\Component\JsonEncoder\Attribute\Encodable;
1617
use Symfony\Component\JsonEncoder\Attribute\EncodedName;
1718
use Symfony\Component\JsonEncoder\Attribute\Normalizer;
1819

1920
/**
2021
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
2122
*/
23+
#[Encodable]
2224
class Dummy
2325
{
2426
#[EncodedName('@name')]

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/JsonEncoder/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ services:
1818
alias: json_encoder.decoder
1919
public: true
2020

21+
json_encoder.cache_warmer.encoder_decoder.alias:
22+
alias: .json_encoder.cache_warmer.encoder_decoder
23+
public: true
24+
25+
Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\Dto\Dummy: ~
2126
Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\RangeNormalizer: ~
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonEncoder\Attribute;
13+
14+
/**
15+
* Mark a class as encodable.
16+
*
17+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
18+
*
19+
* @experimental
20+
*/
21+
#[\Attribute(\Attribute::TARGET_CLASS)]
22+
final class Encodable
23+
{
24+
}

src/Symfony/Component/JsonEncoder/DependencyInjection/EncodablePass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function process(ContainerBuilder $container): void
3030
$encodableClassNames = [];
3131

3232
// retrieve concrete services tagged with "json_encoder.encodable" tag
33-
foreach ($container->findTaggedServiceIds('json_encoder.encodable') as $id => $tags) {
33+
foreach ($container->getDefinitions() as $id => $definition) {
34+
if (!$definition->hasTag('json_encoder.encodable')) {
35+
continue;
36+
}
37+
3438
if (($className = $container->getDefinition($id)->getClass()) && !$container->getDefinition($id)->isAbstract()) {
3539
$encodableClassNames[] = $className;
3640
}

0 commit comments

Comments
 (0)
0