Skip to content

Commit ad8e5a0

Browse files
committed
[JsonEncoder] Add Encodable attribute
1 parent 4dcb217 commit ad8e5a0

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
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;
@@ -741,6 +742,10 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu
741742
}
742743
);
743744
}
745+
$container->registerAttributeForAutoconfiguration(Encodable::class, static function (ChildDefinition $definition): void {
746+
$definition->addTag('json_encoder.encodable');
747+
$definition->addTag('container.excluded');
748+
});
744749

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

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\Dto\Dummy;
15+
use Symfony\Component\Filesystem\Filesystem;
1516
use Symfony\Component\JsonEncoder\DecoderInterface;
1617
use Symfony\Component\JsonEncoder\EncoderInterface;
1718
use Symfony\Component\TypeInfo\Type;
@@ -21,10 +22,15 @@
2122
*/
2223
class JsonEncoderTest extends AbstractWebTestCase
2324
{
24-
public function testEncode()
25+
protected function setUp(): void
2526
{
27+
parent::setUp();
28+
2629
static::bootKernel(['test_case' => 'JsonEncoder']);
30+
}
2731

32+
public function testEncode()
33+
{
2834
/** @var EncoderInterface $encoder */
2935
$encoder = static::getContainer()->get('json_encoder.encoder.alias');
3036

@@ -33,8 +39,6 @@ public function testEncode()
3339

3440
public function testDecode()
3541
{
36-
static::bootKernel(['test_case' => 'JsonEncoder']);
37-
3842
/** @var DecoderInterface $decoder */
3943
$decoder = static::getContainer()->get('json_encoder.decoder.alias');
4044

@@ -44,4 +48,22 @@ public function testDecode()
4448

4549
$this->assertEquals($expected, $decoder->decode('{"@name": "DUMMY", "range": "0..1"}', Type::object(Dummy::class)));
4650
}
51+
52+
public function testWarmupEncodableClasses()
53+
{
54+
/** @var Filesystem $fs */
55+
$fs = static::getContainer()->get('filesystem');
56+
57+
$encodersDir = \sprintf('%s/json_encoder/encoder/', static::getContainer()->getParameter('kernel.cache_dir'));
58+
59+
// clear already created encoders
60+
if ($fs->exists($encodersDir)) {
61+
$fs->remove($encodersDir);
62+
}
63+
64+
static::getContainer()->get('json_encoder.cache_warmer.encoder_decoder.alias')->warmUp(static::getContainer()->getParameter('kernel.cache_dir'));
65+
66+
$this->assertFileExists($encodersDir);
67+
$this->assertCount(1, glob($encodersDir.'/*'));
68+
}
4769
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy