1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\SerializerBundle\DependencyInjection; |
20
|
|
|
|
21
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
22
|
|
|
use Symfony\Component\Config\FileLocator; |
23
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
24
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
25
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
26
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
27
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
28
|
|
|
|
29
|
|
|
class JMSSerializerExtension extends ConfigurableExtension |
30
|
|
|
{ |
31
|
33 |
|
public function loadInternal(array $config, ContainerBuilder $container) |
32
|
|
|
{ |
33
|
33 |
|
$loader = new XmlFileLoader($container, new FileLocator(array( |
34
|
33 |
|
__DIR__.'/../Resources/config/'))); |
35
|
33 |
|
$loader->load('services.xml'); |
36
|
|
|
|
37
|
|
|
// Built-in handlers. |
38
|
33 |
|
$container->getDefinition('jms_serializer.datetime_handler') |
39
|
33 |
|
->addArgument($config['handlers']['datetime']['default_format']) |
40
|
33 |
|
->addArgument($config['handlers']['datetime']['default_timezone']) |
41
|
33 |
|
->addArgument($config['handlers']['datetime']['cdata']) |
42
|
|
|
; |
43
|
|
|
|
44
|
|
|
// property naming |
45
|
|
|
$container |
46
|
33 |
|
->getDefinition('jms_serializer.camel_case_naming_strategy') |
47
|
33 |
|
->addArgument($config['property_naming']['separator']) |
48
|
33 |
|
->addArgument($config['property_naming']['lower_case']) |
49
|
|
|
; |
50
|
33 |
|
if ($config['property_naming']['enable_cache']) { |
51
|
|
|
$container |
52
|
33 |
|
->getDefinition('jms_serializer.cache_naming_strategy') |
53
|
33 |
|
->addArgument(new Reference((string) $container->getAlias('jms_serializer.naming_strategy'))) |
54
|
|
|
; |
55
|
33 |
|
$container->setAlias('jms_serializer.naming_strategy', 'jms_serializer.cache_naming_strategy'); |
56
|
33 |
|
} |
57
|
|
|
|
58
|
33 |
|
$bundles = $container->getParameter('kernel.bundles'); |
59
|
|
|
|
60
|
33 |
|
if (!empty($config['expression_evaluator']['id'])) { |
61
|
|
|
$container |
62
|
32 |
|
->getDefinition('jms_serializer.serializer') |
63
|
32 |
|
->replaceArgument(7, new Reference($config['expression_evaluator']['id'])); |
64
|
32 |
|
} else { |
65
|
1 |
|
$container->removeDefinition('jms_serializer.expression_evaluator'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// metadata |
69
|
33 |
|
if ('none' === $config['metadata']['cache']) { |
70
|
|
|
$container->removeAlias('jms_serializer.metadata.cache'); |
71
|
33 |
|
} elseif ('file' === $config['metadata']['cache']) { |
72
|
|
|
$container |
73
|
33 |
|
->getDefinition('jms_serializer.metadata.cache.file_cache') |
74
|
33 |
|
->replaceArgument(0, $config['metadata']['file_cache']['dir']) |
75
|
|
|
; |
76
|
|
|
|
77
|
33 |
|
$dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']); |
78
|
33 |
|
if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) { |
79
|
|
|
throw new RuntimeException(sprintf('Could not create cache directory "%s".', $dir)); |
80
|
|
|
} |
81
|
33 |
|
} else { |
82
|
|
|
$container->setAlias('jms_serializer.metadata.cache', new Alias($config['metadata']['cache'], false)); |
83
|
|
|
} |
84
|
|
|
|
85
|
33 |
|
if ($config['metadata']['infer_types_from_doctrine_metadata'] === false) { |
86
|
1 |
|
$container->setParameter('jms_serializer.infer_types_from_doctrine_metadata', false); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
$container |
90
|
33 |
|
->getDefinition('jms_serializer.metadata_factory') |
91
|
33 |
|
->replaceArgument(2, $config['metadata']['debug']) |
92
|
|
|
; |
93
|
|
|
|
94
|
|
|
// directories |
95
|
33 |
|
$directories = array(); |
96
|
33 |
|
if ($config['metadata']['auto_detection']) { |
97
|
33 |
|
foreach ($bundles as $name => $class) { |
98
|
1 |
|
$ref = new \ReflectionClass($class); |
99
|
|
|
|
100
|
1 |
|
$directories[$ref->getNamespaceName()] = dirname($ref->getFileName()).'/Resources/config/serializer'; |
101
|
33 |
|
} |
102
|
33 |
|
} |
103
|
33 |
|
foreach ($config['metadata']['directories'] as $directory) { |
104
|
1 |
|
$directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/'); |
105
|
|
|
|
106
|
1 |
|
if ('@' === $directory['path'][0]) { |
107
|
1 |
|
$pathParts = explode('/', $directory['path']); |
108
|
1 |
|
$bundleName = substr($pathParts[0], 1); |
109
|
|
|
|
110
|
1 |
|
if (!isset($bundles[$bundleName])) { |
111
|
|
|
throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles)))); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
$ref = new \ReflectionClass($bundles[$bundleName]); |
115
|
1 |
|
$directory['path'] = dirname($ref->getFileName()).substr($directory['path'], strlen('@'.$bundleName)); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
1 |
|
$directories[rtrim($directory['namespace_prefix'], '\\')] = rtrim($directory['path'], '\\/'); |
119
|
33 |
|
} |
120
|
|
|
$container |
121
|
33 |
|
->getDefinition('jms_serializer.metadata.file_locator') |
122
|
33 |
|
->replaceArgument(0, $directories) |
123
|
|
|
; |
124
|
|
|
|
125
|
33 |
|
$container->setParameter('jms_serializer.xml_deserialization_visitor.doctype_whitelist', $config['visitors']['xml']['doctype_whitelist']); |
126
|
33 |
|
$container->setParameter('jms_serializer.xml_serialization_visitor.format_output', $config['visitors']['xml']['format_output']); |
127
|
33 |
|
$container->setParameter('jms_serializer.json_serialization_visitor.options', $config['visitors']['json']['options']); |
128
|
|
|
|
129
|
33 |
|
if ( ! $config['enable_short_alias']) { |
130
|
|
|
$container->removeAlias('serializer'); |
131
|
|
|
} |
132
|
|
|
|
133
|
33 |
|
if ( ! $container->getParameter('kernel.debug')) { |
134
|
|
|
$container->removeDefinition('jms_serializer.stopwatch_subscriber'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// context factories |
138
|
|
|
$services = [ |
139
|
33 |
|
'serialization' => 'jms_serializer.serialization_context_factory', |
140
|
33 |
|
'deserialization' => 'jms_serializer.deserialization_context_factory', |
141
|
33 |
|
]; |
142
|
33 |
|
foreach ($services as $configKey => $serviceId) { |
143
|
33 |
|
$container->getDefinition($serviceId) |
144
|
33 |
|
->replaceArgument(0, $config['default_context'][$configKey]['version']) |
145
|
33 |
|
->replaceArgument(1, $config['default_context'][$configKey]['serialize_null']) |
146
|
33 |
|
->replaceArgument(2, $config['default_context'][$configKey]['attributes']) |
147
|
33 |
|
->replaceArgument(3, $config['default_context'][$configKey]['groups']) |
148
|
|
|
; |
149
|
33 |
|
} |
150
|
33 |
|
} |
151
|
|
|
|
152
|
34 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
153
|
|
|
{ |
154
|
34 |
|
return new Configuration($container->getParameterBag()->resolveValue('%kernel.debug%')); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|