8000 [JsonEncoder] Fix retrieving encodable classes · symfony/symfony@c94098f · GitHub
[go: up one dir, main page]

Skip to content

Commit c94098f

Browse files
committed
[JsonEncoder] Fix retrieving encodable classes
1 parent 78f4d9a commit c94098f

File tree

10 files changed

+99
-31
lines changed

10 files changed

+99
-31
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,16 +2580,6 @@ private function addJsonEncoderSection(ArrayNodeDefinition $rootNode, callable $
25802580
->arrayNode('json_encoder')
25812581
->info('JSON encoder configuration')
25822582
->{$enableIfStandalone('symfony/json-encoder', EncoderInterface::class)}()
2583-
->fixXmlConfig('path')
2584-
->children()
2585-
->arrayNode('paths')
2586-
->info('Namespaces and paths of encodable/decodable classes.')
2587-
->normalizeKeys(false)
2588-
->useAttributeAsKey('namespace')
2589-
->scalarPrototype()->end()
2590-
->defaultValue([])
2591-
->end()
2592-
->end()
25932583
->end()
25942584
->end()
25952585
;

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,15 +2026,6 @@ private function registerJsonEncoderConfiguration(array $config, ContainerBuilde
20262026
$container->setParameter('.json_encoder.decoders_dir', '%kernel.cache_dir%/json_encoder/decoder');
20272027
$container->setParameter('.json_encoder.lazy_ghosts_dir', '%kernel.cache_dir%/json_encoder/lazy_ghost');
20282028

2029-
$encodableDefinition = (new Definition())
2030-
->setAbstract(true)
2031-
->addTag('container.excluded')
2032-
->addTag('json_encoder.encodable');
2033-
2034-
foreach ($config['paths'] as $namespace => $path) {
2035-
$loader->registerClasses($encodableDefinition, $namespace, $path);
2036-
}
2037-
20382029
if (\PHP_VERSION_ID >= 80400) {
20392030
$container->removeDefinition('.json_encoder.cache_warmer.lazy_ghost');
20402031
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
57AE 5454
use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass;
5555
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
5656
use Symfony\Component\HttpKernel\KernelEvents;
57+
use Symfony\Component\JsonEncoder\DependencyInjection\EncodablePass;
5758
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
5859
use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
5960
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
@@ -186,6 +187,7 @@ public function build(ContainerBuilder $container): void
186187
$container->addCompilerPass(new ErrorLoggerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
187188
$container->addCompilerPass(new VirtualRequestStackPass());
188189
$container->addCompilerPass(new TranslationUpdateCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
190+
$container->addCompilerPass(new EncodablePass());
189191

190192
if ($container->getParameter('kernel.debug')) {
191193
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);

src/Symfony/Bundle/FrameworkBundle/Resources/config/json_encoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
// cache
108108
->set('.json_encoder.cache_warmer.encoder_decoder', EncoderDecoderCacheWarmer::class)
109109
->args([
110-
tagged_iterator('json_encoder.encodable'),
110+
abstract_arg('encodable class names'),
111111
service('json_encoder.encode.property_metadata_loader'),
112112
service('json_encoder.decode.property_metadata_loader'),
113113
param('.json_encoder.encoders_dir'),
@@ -118,7 +118,7 @@
118118

119119
->set('.json_encoder.cache_warmer.lazy_ghost', LazyGhostCacheWarmer::class)
120120
->args([
121-
tagged_iterator('json_encoder.encodable'),
121+
abstract_arg('encodable class names'),
122122
param('.json_encoder.lazy_ghosts_dir'),
123123
])
124124
->tag('kernel.cache_warmer')

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,7 @@
10061006
</xsd:complexType>
10071007

10081008
<xsd:complexType name="json-encoder">
1009-
<xsd:choice minOccurs="0" maxOccurs="unbounded">
1010-
<xsd:sequence>
1011-
<xsd:element name="default-context" type="metadata" minOccurs="0" maxOccurs="1" />
1012-
</xsd:sequence>
1013-
</xsd:choice>
10141009
<xsd:attribute name="enabled" type="xsd:boolean" />
10151010
</xsd:complexType>
1011+
10161012
</xsd:schema>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,6 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
973973
],
974974
'json_encoder' => [
975975
'enabled' => !class_exists(FullStack::class) && class_exists(JsonEncoder::class),
976-
'paths' => [],
977976
],
978977
];
979978
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ imports:
44
framework:
55
http_method_override: false
66
type_info: ~
7-
json_encoder:
8-
enabled: true
9-
paths:
10-
Symfony\Bundle\FrameworkBundle\Tests\Functional\app\JsonEncoder\Dto\: '../../Tests/Functional/app/JsonEncoder/Dto/*'
7+
json_encoder: ~
118

129
services:
1310
_defaults:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* Sets the encodable classes to the services that need them.
19+
*
20+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
21+
*/
22+
class EncodablePass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container): void
25+
{
26+
if (!$container->hasDefinition('json_encoder.encoder')) {
27+
return;
28+
}
29+
30+
$encodableClassNames = [];
31+
32+
// retrieve concrete services tagged with "json_encoder.encodable" tag
33+
foreach ($container->findTaggedServiceIds('json_encoder.encodable') as $id => $tags) {
34+
if (($className = $container->getDefinition($id)->getClass()) && !$container->getDefinition($id)->isAbstract()) {
35+
$encodableClassNames[] = $className;
36+
}
37+
38+
$container->removeDefinition($id);
39+
}
40+
41+
$container->getDefinition('.json_encoder.cache_warmer.encoder_decoder')
42+
->replaceArgument(0, $encodableClassNames);
43+
44+
if ($container->hasDefinition('.json_encoder.cache_warmer.lazy_ghost')) {
45+
$container->getDefinition('.json_encoder.cache_warmer.lazy_ghost')
46+
->replaceArgument(0, $encodableClassNames);
47+
}
48+
}
49+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\JsonEncoder\DependencyInjection\EncodablePass;
17+
18+
class EncodablePassTest extends TestCase
19+
{
20+
public function testSetEncodableClassNames()
21+
{
22+
$container = new ContainerBuilder();
23+
24+
$container->register('json_encoder.encoder');
25+
$container->register('.json_encoder.cache_warmer.encoder_decoder')->setArguments([null]);
26+
$container->register('.json_encoder.cache_warmer.lazy_ghost')->setArguments([null]);
27+
28+
$container->register('encodable')->setClass('Foo')->addTag('json_encoder.encodable');
29+
$container->register('abstractEncodable')->setClass('Bar')->addTag('json_encoder.encodable')->setAbstract(true);
30+
$container->register('notEncodable')->setClass('Baz');
31+
32+
$pass = new EncodablePass();
33+
$pass->process($container);
34+
35+
$encoderDecoderCacheWarmer = $container->getDefinition('.json_encoder.cache_warmer.encoder_decoder');
36+
$lazyGhostCacheWarmer = $container->getDefinition('.json_encoder.cache_warmer.lazy_ghost');
37+
38+
$expectedEncodableClassNames = ['Foo'];
39+
40+
$this->assertSame($expectedEncodableClassNames, $encoderDecoderCacheWarmer->getArgument(0));
41+
$this->assertSame($expectedEncodableClassNames, $lazyGhostCacheWarmer->getArgument(0));
42+
}
43+
}

src/Symfony/Component/JsonEncoder/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"require-dev": {
2828
"phpstan/phpdoc-parser": "^1.0",
29+
"symfony/dependency-injection": "^7.2",
2930
"symfony/http-kernel": "^7.2"
3031
},
3132
"autoload": {

0 commit comments

Comments
 (0)
0