Skip to content

Commit 13308ad

Browse files
fix(symfony): register property_info fallback when not provided by Symfony (#7969)
Co-authored-by: soyuka <soyuka@users.noreply.github.com> Signed-off-by: Guillaume Delré <delre.guillaume@gmail.com> Fixes #7876 Fix #7876 Closes api-platform/docs#2262
1 parent 2f82499 commit 13308ad

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

Bundle/Resources/config/api.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
use ApiPlatform\Symfony\Routing\Router;
4747
use ApiPlatform\Symfony\Routing\SkolemIriConverter;
4848
use Negotiation\Negotiator;
49+
use phpDocumentor\Reflection\DocBlockFactory;
50+
use phpDocumentor\Reflection\Types\ContextFactory;
51+
use PHPStan\PhpDocParser\Parser\PhpDocParser;
52+
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
53+
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
54+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
55+
use Symfony\Component\PropertyInfo\PropertyInfoCacheExtractor;
56+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
4957
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
5058
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
5159

@@ -68,7 +76,38 @@
6876

6977
$services->alias('api_platform.property_accessor', 'property_accessor');
7078

71-
$services->alias('api_platform.property_info', 'property_info');
79+
$services->set('api_platform.property_info.reflection_extractor', ReflectionExtractor::class)
80+
->tag('property_info.list_extractor', ['priority' => -1000])
81+
->tag('property_info.type_extractor', ['priority' => -1002])
82+
->tag('property_info.access_extractor', ['priority' => -1000])
83+
->tag('property_info.initializable_extractor', ['priority' => -1000]);
84+
85+
if (class_exists(DocBlockFactory::class)) {
86+
$services->set('api_platform.property_info.php_doc_extractor', PhpDocExtractor::class)
87+
->tag('property_info.description_extractor', ['priority' => -1000])
88+
->tag('property_info.type_extractor', ['priority' => -1001]);
89+
}
90+
91+
if (class_exists(PhpDocParser::class) && class_exists(ContextFactory::class)) {
92+
$services->set('api_platform.property_info.phpstan_extractor', PhpStanExtractor::class)
93+
->tag('property_info.type_extractor', ['priority' => -1000]);
94+
}
95+
96+
$services->set('api_platform.property_info', PropertyInfoExtractor::class)
97+
->args([
98+
tagged_iterator('property_info.list_extractor'),
99+
tagged_iterator('property_info.type_extractor'),
100+
tagged_iterator('property_info.description_extractor'),
101+
tagged_iterator('property_info.access_extractor'),
102+
tagged_iterator('property_info.initializable_extractor'),
103+
]);
104+
105+
$services->set('api_platform.property_info.cache', PropertyInfoCacheExtractor::class)
106+
->decorate('api_platform.property_info')
107+
->args([
108+
service('.inner'),
109+
service('cache.property_info'),
110+
]);
72111

73112
$services->set('api_platform.negotiator', Negotiator::class);
74113

Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public function testCommonConfiguration(): void
190190
'api_platform.path_segment_name_generator.dash',
191191
'api_platform.path_segment_name_generator.underscore',
192192
'api_platform.metadata.inflector',
193+
'api_platform.property_info',
193194
'api_platform.resource_class_resolver',
194195
'api_platform.route_loader',
195196
'api_platform.router',
@@ -231,7 +232,6 @@ public function testCommonConfiguration(): void
231232
'api_platform.iri_converter',
232233
'api_platform.path_segment_name_generator',
233234
'api_platform.property_accessor',
234-
'api_platform.property_info',
235235
'api_platform.serializer',
236236
'api_platform.inflector',
237237
];

0 commit comments

Comments
 (0)