8000 Added support for YAML and XML mapping · symfony/symfony@18ad328 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18ad328

Browse files
committed
Added support for YAML and XML mapping
1 parent 5bd8b55 commit 18ad328

38 files changed

+1991
-28
lines changed

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

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function load(array $configs, ContainerBuilder $container)
128128
}
129129

130130
$this->registerAnnotationsConfiguration($config['annotations'], $container, $loader);
131-
$this->registerPropertyAccessConfiguration($config['property_access'], $container);
131+
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
132132

133133
if (isset($config['serializer'])) {
134134
$this->registerSerializerConfiguration($config['serializer'], $container, $loader);
@@ -858,13 +858,90 @@ private function registerAnnotationsConfiguration(array $config, ContainerBuilde
858858
}
859859
}
860860

861-
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container)
861+
/**
862+
* Loads the PropertyAccess configuration.
863+
*
864+
* @param array $config A serializer configuration array
865+
* @param ContainerBuilder $container A ContainerBuilder instance
866+
* @param XmlFileLoader $loader An XmlFileLoader instance
867+
*/
868+
private function registerPropertyAccessConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
862869
{
863870
$container
864871
->getDefinition('property_accessor')
865872
->replaceArgument(0, $config['magic_call'])
866873
->replaceArgument(1, $config['throw_exception_on_invalid_index'])
867874
;
875+
876+
if (!$this->isConfigEnabled($container, $config)) {
877+
return;
878+
}
879+
880+
$loader->load('property_access.xml');
881+
$chainLoader = $container->getDefinition('property_access.mapping.chain_loader');
882+
883+
$serializerLoaders = array();
884+
if (isset($config['enable_annotations']) && $config['enable_annotations']) {
885+
$annotationLoader = new Definition(
886+
'Symfony\Component\PropertyAccess\Mapping\Loader\AnnotationLoader',
887+
array(new Reference('annotation_reader'))
888+
);
889+
$annotationLoader->setPublic(false);
890+
891+
$serializerLoaders[] = $annotationLoader;
892+
}
893+
894+
$bundles = $container->getParameter('kernel.bundles');
895+
foreach ($bundles as $bundle) {
896+
$reflection = new \ReflectionClass($bundle);
897+
$dirname = dirname($reflection->getFileName());
898+
899+
if (is_file($file = $dirname.'/Resources/config/property_access.xml')) {
900+
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\XmlFileLoader', array(realpath($file)));
901+
$definition->setPublic(false);
902+
903+
$serializerLoaders[] = $definition;
904+
$container->addResource(new FileResource($file));
905+
}
906+
907+
if (is_file($file = $dirname.'/Resources/config/property_access.yml')) {
908+
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\YamlFileLoader', array(realpath($file)));
909+
$definition->setPublic(false);
910+
911+
$serializerLoaders[] = $definition;
912+
$container->addResource(new FileResource($file));
913+
}
914+
915+
if (is_dir($dir = $dirname.'/Resources/config/property_access')) {
916+
foreach (Finder::create()->files()->in($dir)->name('*.xml') as $file) {
917+
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\XmlFileLoader', array($file->getRealpath()));
918+
$definition->setPublic(false);
919+
920+
$serializerLoaders[] = $definition;
921+
}
922+
foreach (Finder::create()->files()->in($dir)->name('*.yml') as $file) {
923+
$definition = new Definition('Symfony\Component\PropertyAccess\Mapping\Loader\YamlFileLoader', array($file->getRealpath()));
924+
$definition->setPublic(false);
925+
926+
$serializerLoaders[] = $definition;
927+
}
928+
929+
$container->addResource(new DirectoryResource($dir));
930+
}
931+
}
932+
933+
$chainLoader->replaceArgument(0, $serializerLoaders);
934+
935+
if (isset($config['cache']) && $config['cache']) {
936+
$container->setParameter(
937+
'property_access.mapping.cache.prefix',
938+
'property_access_'.$this->getKernelRootHash($container)
939+
);
940+
941+
$container->getDefinition('property_access.mapping.class_metadata_factory')->replaceArgument(
942+
1, new Reference($config['cache'])
943+
);
944+
}
868945
}
869946

870947
/**

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8+
9+
<!-- Loader -->
10+
<service id="property_access.mapping.chain_loader" class="Symfony\Component\PropertyAccess\Mapping\Loader\LoaderChain" public="false">
11+
<argument type="collection" />
12+
</service>
13+
14+
<!-- Class Metadata Factory -->
15+
<service id="property_access.mapping.class_metadata_factory" class="Symfony\Component\PropertyAccess\Mapping\Factory\ClassMetadataFactory" public="false">
16+
<argument type="service" id="property_access.mapping.chain_loader" />
17+
<argument>null</argument>
18+
</service>
19+
20+
<!-- Cache -->
21+
<service id="property_access.mapping.cache.apc" class="Doctrine\Common\Cache\ApcCache" public="false">
22+
<call method="setNamespace">
23+
<argument>%property_access.mapping.cache.prefix%</argument>
24+
</call>
25+
</service>
26+
827
<service id="property_accessor" class="Symfony\Component\PropertyAccess\PropertyAccessor" >
928
<argument /> <!-- magicCall, set by the extension -->
1029
<argument /> <!-- throwExceptionOnInvalidIndex, set by the extension -->
11-
<argument type="service" id="annotation_reader" />
30+
<argument type="service" id="property_access.mapping.class_metadata_factory" />
1231
</service>
1332
</services>
1433
</container>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ protected static function getBundleDefaultConfig()
224224
'property_access' => array(
225225
'magic_call' => false,
226226
'throw_exception_on_invalid_index' => false,
227+
'enable_annotations' => false,
227228
),
228229
'property_info' => array(
229230
'enabled' => false,

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' => false,
78
),
89
));

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: true

src/Symfony/Component/PropertyAccess/Annotation/PropertyAccessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Property accessor configuration annotation.
1616
*
1717
* @Annotation
18+
* @Target({"PROPERTY"})
1819
*
1920
* @author Luis Ramón López <lrlopez@gmail.com>
2021
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\PropertyAccess\Exception;
13+
14+
/**
15+
* MappingException.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class MappingException extends RuntimeException
20+
{
21+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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\PropertyAccess\Mapping;
13+
14+
/**
15+
* {@inheritdoc}
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class AttributeMetadata implements AttributeMetadataInterface
20+
{
21+
/**
22+
* @var string
23+
*
24+
* @internal This property is public in order to reduce the size of the
25+
* class' serialized representation. Do not access it. Use
26+
* {@link getName()} instead.
27+
*/
28+
public $name;
29+
30+
/**
31+
* @var string
32+
*
33+
* @internal This property is public in order to reduce the size of the
34+
* class' serialized representation. Do not access it. Use
35+
* {@link getGetter()} instead.
36+
*/
37+
public $getter;
38+
39+
/**
40+
* @var string
41+
*
42+
* @internal This property is public in order to reduce the size of the
43+
* class' serialized representation. Do not access it. Use
44+
* {@link getSetter()} instead.
45+
*/
46+
public $setter;
47+
48+
/**
49+
* @var string
50+
*
51+
* @internal This property is public in order to reduce the size of the
52+
* class' serialized representation. Do not access it. Use
53+
* {@link getAdder()} instead.
54+
*/
55+
public $adder;
56+
57+
/**
58+
* @var string
59+
*
60+
* @internal This property is public in order to reduce the size of the
61+
* class' serialized representation. Do not access it. Use
62+
* {@link getRemover()} instead.
63+
*/
64+
public $remover;
65+
66+
/**
67+
* Constructs a metadata for the given attribute.
68+
*
69+
* @param string $name
70+
*/
71+
public function __construct($name)
72+
{
73+
$this->name = $name;
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function getName()
80+
{
81+
return $this->name;
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function getSetter()
88+
{
89+
return $this->setter;
90+
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
public function setSetter($setter)
96+
{
97+
$this->setter = $setter;
98+
}
99+
100+
/**
101+
* {@inheritdoc}
102+
*/
103+
public function getGetter()
104+
{
105+
return $this->getter;
106+
}
107+
108+
/**
109+
* {@inheritdoc}
110+
*/
111+
public function setGetter($getter)
112+
{
113+
$this->getter = $getter;
114+
}
115+
116+
/**
117+
* {@inheritdoc}
118+
*/
119+
public function getAdder()
120+
{
121+
return $this->adder;
122+
}
123+
124+
/**
125+
* {@inheritdoc}
126+
*/
127+
public function setAdder($adder)
128+
{
129+
$this->adder = $adder;
130+
}
131+
132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function getRemover()
136+
{
137+
return $this->remover;
138+
}
139+
140+
/**
141+
* {@inheritdoc}
142+
*/
143+
public function setRemover($remover)
144+
{
145+
$this->remover = $remover;
146+
}
147+
148+
/**
149+
* {@inheritdoc}
150+
*/
151+
public function merge(AttributeMetadataInterface $attributeMetadata)
152+
{
153+
// Overwrite only if not defined
154+
if (null === $this->getter) {
155+
$this->getter = $attributeMetadata->getGetter();
156+
}
157+
if (null === $this->setter) {
158+
$this->setter = $attributeMetadata->getSetter();
159+
}
160+
if (null === $this->adder) {
161+
$this->adder = $attributeMetadata->getAdder();
162+
}
163+
if (null === $this->remover) {
164+
$this->remover = $attributeMetadata->getRemover();
165+
}
166+
}
167+
168+
/**
169+
* Returns the names of the properties that should be serialized.
170+
*
171+
* @return string[]
172+
*/
173+
public function __sleep()
174+
{
175+
return array('name', 'getter', 'setter', 'adder', 'remover');
176+
}
177+
}

0 commit comments

Comments
 (0)
0