Skip to content

Commit b70b7ea

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

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

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
@@ -12,13 +12,15 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\Dto;
1313

1414
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\RangeNormalizer;
15+
use Symfony\Component\JsonEncoder\Attribute\Encodable;
1516
use Symfony\Component\JsonEncoder\Attribute\Denormalizer;
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
15+
16+
/**
17+
* Autoconfigure "json_encoder.encodable" tag.
18+
*
19+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
20+
*
21+
* @experimental
22+
*/
23+
#[\Attribute(\Attribute::TARGET_CLASS)]
24+
final class Encodable extends AutoconfigureTag
25+
{
26+
public function __construct()
27+
{
28+
parent::__construct('json_encoder.encodable');
29+
}
30+
}

0 commit comments

Comments
 (0)