8000 bug #59287 [JsonEncoder] Fix retrieving encodable classes (mtarld) · symfony/symfony@f3fb3a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f3fb3a5

Browse files
bug #59287 [JsonEncoder] Fix retrieving encodable classes (mtarld)
This PR was merged into the 7.3 branch. Discussion ---------- [JsonEncoder] Fix retrieving encodable classes | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Fix encodable classes retrievable. They now can be retrieved if they are services tagged with `json_encoder.encodable`. This as well lay the foundations for a next PR that will allow defining specific types to warm up in configuration. Commits ------- c94098f [JsonEncoder] Fix retrieving encodable classes
2 parents 4c071c2 + c94098f commit f3fb3a5

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
@@ -2595,16 +2595,6 @@ private function addJsonEncoderSection(ArrayNodeDefinition $rootNode, callable $
25952595
->arrayNode('json_encoder')
25962596
->info('JSON encoder configuration')
25972597
->{$enableIfStandalone('symfony/json-encoder', EncoderInterface::class)}()
2598-
->fixXmlConfig('path')
2599-
->children()
2600-
->arrayNode('paths')
2601-
->info('Namespaces and paths of encodable/decodable classes.')
2602-
->normalizeKeys(false)
2603-
->useAttributeAsKey('namespace')
2604-
->scalarPrototype()->end()
2605-
->defaultValue([])
2606-
->end()
2607-
->end()
26082598
->end()
26092599
->end()
26102600
;

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

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

2032-
$encodableDefinition = (new Definition())
2033-
->setAbstract(true)
2034-
->addTag('container.excluded')
2035-
->addTag('json_encoder.encodable');
2036-
2037-
foreach ($config['paths'] as $namespace => $path) {
2038-
$loader->registerClasses($encodableDefinition, $namespace, $path);
2039-
}
2040-
20412032
if (\PHP_VERSION_ID >= 80400) {
20422033
$container->removeDefinition('.json_encoder.cache_warmer.lazy_ghost');
20432034
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
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\PropertyInfoConstructorPass;
@@ -188,6 +189,7 @@ public function build(ContainerBuilder $container): void
188189
$container->addCompilerPass(new ErrorLoggerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
189190
$container->addCompilerPass(new VirtualRequestStackPass());
190191
$container->addCompilerPass(new TranslationUpdateCommandPass(), PassConfig::TYPE_BEFORE_REMOVING);
192+
$container->addCompilerPass(new EncodablePass());
191193

192194
if ($container->getParameter('kernel.debug')) {
193195
$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
@@ -1007,11 +1007,7 @@
10071007
</xsd:complexType>
10081008

10091009
<xsd:complexType name="json-encoder">
1010-
<xsd:choice minOccurs="0" maxOccurs="unbounded">
1011-
<xsd:sequence>
1012-
<xsd:element name="default-context" type="metadata" minOccurs="0" maxOccurs="1" />
1013-
</xsd:sequence>
1014-
</xsd:choice>
10151010
<xsd:attribute name="enabled" type="xsd:boolean" />
10161011
</xsd:complexType>
1012+
10171013
</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