8000 Metadata aware PropertyAccessor support for FrameworkBundle · symfony/symfony@e948620 · GitHub
[go: up one dir, main page]

Skip to content

Commit e948620

Browse files
committed
Metadata aware PropertyAccessor support for FrameworkBundle
1 parent 3c262ba commit e948620

File tree

10 files changed

+77
-16
lines changed

10 files changed

+77
-16
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ private function addPropertyAccessSection(ArrayNodeDefinition $rootNode)
783783
->children()
784784
->booleanNode('magic_call')->defaultFalse()->end()
785785
->booleanNode('throw_exception_on_invalid_index')->defaultFalse()->end()
786+
->booleanNode('enable_annotations')->defaultFalse()->end()
786787
->end()
787788
->end()
788789
->end()

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

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function load(array $configs, ContainerBuilder $container)
288288
$this->registerDebugConfiguration($config['php_errors'], $container, $loader);
289289
$this->registerRouterConfiguration($config['router'], $container, $loader);
290290
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
291-
$this->registerPropertyAccessConfiguration($config['property_access'], $container);
291+
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
292292

293293
if ($this->isConfigEnabled($container, $config['serializer'])) {
294294
$this->registerSerializerConfiguration($config['serializer'], $container, $loader);
@@ -1365,39 +1365,44 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
13651365
}
13661366
}
13671367

1368-
private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)
1368+
private function registerComponentMapping(ContainerBuilder $container, array $config, $component, $fileRecorder)
13691369
{
1370-
$fileRecorder = function ($extension, $path) use (&$files) {
1371-
$files['yaml' === $extension ? 'yml' : $extension][] = $path;
1372-
};
1373-
1374-
if (interface_exists('Symfony\Component\Form\FormInterface')) {
1375-
$reflClass = new \ReflectionClass('Symfony\Component\Form\FormInterface');
1376-
$fileRecorder('xml', dirname($reflClass->getFileName()).'/Resources/config/validation.xml');
1377-
}
1378-
13791370
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
13801371
$dirname = $bundle['path'];
13811372

13821373
if (
13831374
$container->fileExists($file = $dirname.'/Resources/config/validation.yaml', false) ||
1384-
$container->fileExists($file = $dirname.'/Resources/config/validation.yml', false)
1375+
$container->fileExists($file = $dirname.'/Resources/config/'.$component.'.yml', false)
13851376
) {
13861377
$fileRecorder('yml', $file);
13871378
}
13881379

1389-
if ($container->fileExists($file = $dirname.'/Resources/config/validation.xml', false)) {
1380+
if ($container->fileExists($file = $dirname.'/Resources/config/'.$component.'.xml', false)) {
13901381
$fileRecorder('xml', $file);
13911382
}
13921383

1393-
if ($container->fileExists($dir = $dirname.'/Resources/config/validation', '/^$/')) {
1384+
if ($container->fileExists($dir = $dirname.'/Resources/config/'.$component, '/^$/')) {
13941385
$this->registerMappingFilesFromDir($dir, $fileRecorder);
13951386
}
13961387
}
13971388

13981389
$this->registerMappingFilesFromConfig($container, $config, $fileRecorder);
13991390
}
14001391

1392+
private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)
1393+
{
1394+
$fileRecorder = function ($extension, $path) use (&$files) {
1395+
$files['yaml' === $extension ? 'yml' : $extension][] = $path;
1396+
};
1397+
1398+
if (interface_exists('Symfony\Component\Form\FormInterface')) {
1399+
$reflClass = new \ReflectionClass('Symfony\Component\Form\FormInterface');
1400+
$fileRecorder('xml', dirname($reflClass->getFileName()).'/Resources/config/validation.xml');
1401+
}
1402+
1403+
$this->registerComponentMapping($container, $config, 'validator', $fileRecorder);
1404+
}
1405+
14011406
private function registerMappingFilesFromDir($dir, callable $fileRecorder)
14021407
{
14031408
foreach (Finder::create()->followLinks()->files()->in($dir)->name('/\.(xml|ya?ml)$/') as $file) {
@@ -1483,12 +1488,42 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
14831488
}
14841489
}
14851490

1486-
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container)
1491+
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
14871492
{
1493+
$loader->load('property_access.xml');
1494+
1495+
$chainLoader = $container->getDefinition('property_access.mapping.chain_loader');
1496+
1497+
$serializerLoaders = array();
1498+
if (isset($config['enable_annotations']) && $config['enable_annotations']) {
1499+
if (!class_exists(Doctrine\Common\Annotations\Annotation::class)) {
1500+
throw new LogicException('Annotations cannot be enabled as the Doctrine Annotation library is not installed.');
1501+
}
1502+
1503+
$annotationLoader = new Definition(
1504+
Symfony\Component\PropertyAccess\Mapping\Loader\AnnotationLoader::class,
1505+
array(new Reference('annotation_reader'))
1506+
);
1507+
$annotationLoader->setPublic(false);
1508+
1509+
$serializerLoaders[] = $annotationLoader;
1510+
}
1511+
1512+
$fileRecorder = function ($extension, $path) use (&$serializerLoaders) {
1513+
$definition = new Definition(in_array($extension, array('yaml', 'yml')) ? 'Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader' : 'Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($path));
1514+
$definition->setPublic(false);
1515+
$serializerLoaders[] = $definition;
1516+
};
1517+
1518+
$this->registerComponentMapping($container, $config, 'property_access', $fileRecorder);
1519+
1520+
$chainLoader->replaceArgument(0, $serializerLoaders);
1521+
14881522
$container
14891523
->getDefinition('property_accessor')
14901524
->replaceArgument(0, $config['magic_call'])
14911525
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
1526+
->replaceArgument(2, $chainLoader)
14921527
;
14931528
}
14941529

src/Symfony/Bundle/FrameworkBundle/Resources/config/property_access.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@
77
<services>
88
<defaults public="false" />
99

10+
<!-- Loader -->
11+
<service id="property_access.mapping.chain_loader" class="Symfony\Component\PropertyAccess\Mapping\Loader\LoaderChain" public="false">
12+
<argument type="collection" />
13+
</service>
14+
15+
<!-- Class Metadata Factory -->
16+
<service id="property_access.mapping.class_metadata_factory" class="Symfony\Component\PropertyAccess\Mapping\Factory\LazyLoadingMetadataFactory" public="false">
17+
<argument type="service" id="property_access.mapping.chain_loader" />
18+
<argument>null</argument>
19+
</service>
20+
1021
<service id="property_accessor" class="Symfony\Component\PropertyAccess\PropertyAccessor">
1122
<argument /> <!-- magicCall, set by the extension -->
1223
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
1324
<argument type="service" id="cache.property_access" on-invalid="ignore" />
25+
<argument type="service" id="property_access.mapping.class_metadata_factory" on-invalid="ignore" />
1426
</service>
1527
<service id="Symfony\Component\PropertyAccess\PropertyAccessorInterface" alias="property_accessor" />
1628
</services>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@
215215
<xsd:complexType name="property_access">
216216
<xsd:attribute name="magic-call" type="xsd:boolean" />
217217
<xsd:attribute name="throw-exception-on-invalid-index" type="xsd:boolean" />
218+
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
218219
</xsd:complexType>
219220

220221
<xsd:complexType name="serializer">

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ protected static function getBundleDefaultConfig()
282282
'property_access' => array(
283283
'magic_call' => false,
284284
'throw_exception_on_invalid_index' => false,
285+
'enable_annotations' => false,
285286
),
286287
'property_info' => array(
287288
'enabled' => false,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
'debug' => true,
6565
'file_cache_dir' => '%kernel.cache_dir%/annotations',
6666
),
67+
'property_access' => array(
68+
'magic_call' => false,
69+
'throw_exception_on_invalid_index' => false,
70+
'enable_annotations' => true,
71+
),
6772
'serializer' => array(
6873
'enabled' => true,
6974
'enable_annotations' => true,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/property_accessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
'property_access' => array(
55
'magic_call' => true,
66
'throw_exception_on_invalid_index' => true,
7+
'enable_annotations' => true,
78
),
89
));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/property_accessor.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config>
10-
<framework:property-access magic-call="true" throw-exception-on-invalid-index="true" />
10+
<framework:property-access magic-call="true" throw-exception-on-invalid-index="true" enable-annotations="false" />
1111
</framework:config>
1212
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ framework:
5353
enabled: true
5454
enable_annotations: true
5555
name_converter: serializer.name_converter.camel_case_to_snake_case
56+
property_access:
57+
enable_annotations: true
58+
magic_call: false
59+
throw_exception_on_invalid_index: false
5660
property_info: ~
5761
ide: file%%link%%format
5862
request:

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/property_accessor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ framework:
22
property_access:
33
magic_call: true
44
throw_exception_on_invalid_index: true
5+
enable_annotations: false

0 commit comments

Comments
 (0)
0