Skip to content

Commit a12ad34

Browse files
committed
[JsonEncoder] Add JsonEncodable attribute
1 parent 7b33872 commit a12ad34

File tree

7 files changed

+62
-5
lines changed

7 files changed

+62
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class UnusedTagsPass implements CompilerPassInterface
5454
'html_sanitizer',
5555
'http_client.client',
5656
'json_encoder.denormalizer',
57-
'json_encoder.encodable',
5857
'json_encoder.normalizer',
5958
'kernel.cache_clearer',
6059
'kernel.cache_warmer',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
101101
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
102102
use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
103+
use Symfony\Component\JsonEncoder\Attribute\JsonEncodable;
103104
use Symfony\Component\JsonEncoder\Decode\Denormalizer\DenormalizerInterface as JsonEncoderDenormalizerInterface;
104105
use Symfony\Component\JsonEncoder\DecoderInterface as JsonEncoderDecoderInterface;
105106
use Symfony\Component\JsonEncoder\Encode\Normalizer\NormalizerInterface as JsonEncoderNormalizerInterface;
@@ -745,6 +746,10 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu
745746
}
746747
);
747748
}
749+
$container->registerAttributeForAutoconfiguration(JsonEncodable::class, static function (ChildDefinition $definition): void {
750+
$definition->addTag('json_encoder.encodable');
751+
$definition->addTag('container.excluded');
752+
});
748753

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

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

Lines changed: 23 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,13 @@
2122
*/
2223
class JsonEncoderTest extends AbstractWebTestCase
2324
{
24-
public function testEncode()
25+
protected function setUp(): void
2526
{
2627
static::bootKernel(['test_case' => 'JsonEncoder']);
28+
}
2729

30+
public function testEncode()
31+
{
2832
/** @var EncoderInterface $encoder */
2933
$encoder = static::getContainer()->get('json_encoder.encoder.alias');
3034

@@ -33,8 +37,6 @@ public function testEncode()
3337

3438
public function testDecode()
3539
{
36-
static::bootKernel(['test_case' => 'JsonEncoder']);
37-
3840
/** @var DecoderInterface $decoder */
3941
$decoder = static::getContainer()->get('json_encoder.decoder.alias');
4042

@@ -44,4 +46,22 @@ public function testDecode()
4446

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

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
@@ -14,11 +14,13 @@
1414
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\RangeNormalizer;
1515
use Symfony\Component\JsonEncoder\Attribute\Denormalizer;
1616
use Symfony\Component\JsonEncoder\Attribute\EncodedName;
17+
use Symfony\Component\JsonEncoder\Attribute\JsonEncodable;
1718
use Symfony\Component\JsonEncoder\Attribute\Normalizer;
1819

1920
/**
2021
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
2122
*/
23+
#[JsonEncodable]
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
16+
*
17+
* @experimental
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS)]
20+
final class JsonEncodable
21+
{
22+
}

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