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 Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
22
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
23
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
24
|
|
|
use Symfony\Component\Config\FileLocator; |
25
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
26
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
28
|
|
|
|
29
|
|
|
class JMSSerializerExtension extends ConfigurableExtension |
30
|
|
|
{ |
31
|
15 |
|
public function loadInternal(array $config, ContainerBuilder $container) |
32
|
|
|
{ |
33
|
15 |
|
$loader = new XmlFileLoader($container, new FileLocator(array( |
34
|
15 |
|
__DIR__.'/../Resources/config/'))); |
35
|
15 |
|
$loader->load('services.xml'); |
36
|
|
|
|
37
|
|
|
// Built-in handlers. |
38
|
15 |
|
$container->getDefinition('jms_serializer.datetime_handler') |
39
|
15 |
|
->addArgument($config['handlers']['datetime']['default_format']) |
40
|
15 |
|
->addArgument($config['handlers']['datetime']['default_timezone']) |
41
|
15 |
|
->addArgument($config['handlers']['datetime']['cdata']) |
42
|
|
|
; |
43
|
|
|
|
44
|
|
|
// property naming |
45
|
|
|
$container |
46
|
15 |
|
->getDefinition('jms_serializer.camel_case_naming_strategy') |
47
|
15 |
|
->addArgument($config['property_naming']['separator']) |
48
|
15 |
|
->addArgument($config['property_naming']['lower_case']) |
49
|
|
|
; |
50
|
15 |
|
if ($config['property_naming']['enable_cache']) { |
51
|
|
|
$container |
52
|
15 |
|
->getDefinition('jms_serializer.cache_naming_strategy') |
53
|
15 |
|
->addArgument(new Reference((string) $container->getAlias('jms_serializer.naming_strategy'))) |
54
|
|
|
; |
55
|
15 |
|
$container->setAlias('jms_serializer.naming_strategy', 'jms_serializer.cache_naming_strategy'); |
56
|
15 |
|
} |
57
|
|
|
|
58
|
15 |
|
$bundles = $container->getParameter('kernel.bundles'); |
59
|
|
|
|
60
|
15 |
|
if (!empty($config['expression_evaluator']['id'])) { |
61
|
|
|
$container |
62
|
13 |
|
->getDefinition('jms_serializer.serializer') |
63
|
13 |
|
->replaceArgument(7, new Reference($config['expression_evaluator']['id'])); |
64
|
|
|
|
65
|
|
|
$container |
66
|
13 |
|
->setAlias('jms_serializer.accessor_strategy', 'jms_serializer.accessor_strategy.expression'); |
67
|
|
|
|
68
|
13 |
|
} else { |
69
|
2 |
|
$container->removeDefinition('jms_serializer.expression_evaluator'); |
70
|
2 |
|
$container->removeDefinition('jms_serializer.accessor_strategy.expression'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// metadata |
74
|
15 |
|
if ('none' === $config['metadata']['cache']) { |
75
|
|
|
$container->removeAlias('jms_serializer.metadata.cache'); |
76
|
15 |
|
} elseif ('file' === $config['metadata']['cache']) { |
77
|
|
|
$container |
78
|
15 |
|
->getDefinition('jms_serializer.metadata.cache.file_cache') |
79
|
15 |
|
->replaceArgument(0, $config['metadata']['file_cache']['dir']) |
80
|
|
|
; |
81
|
|
|
|
82
|
15 |
|
$dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']); |
83
|
15 |
|
if (!file_exists($dir)) { |
84
|
1 |
|
if (!$rs = @mkdir($dir, 0777, true)) { |
85
|
|
|
throw new RuntimeException(sprintf('Could not create cache directory "%s".', $dir)); |
86
|
|
|
} |
87
|
1 |
|
} |
88
|
15 |
|
} else { |
89
|
|
|
$container->setAlias('jms_serializer.metadata.cache', new Alias($config['metadata']['cache'], false)); |
90
|
|
|
} |
91
|
|
|
|
92
|
15 |
|
if ($config['metadata']['infer_types_from_doctrine_metadata'] === false) { |
93
|
|
|
$container->setParameter('jms_serializer.infer_types_from_doctrine_metadata', false); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$container |
97
|
15 |
|
->getDefinition('jms_serializer.metadata_factory') |
98
|
15 |
|
->replaceArgument(2, $config['metadata']['debug']) |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
// directories |
102
|
15 |
|
$directories = array(); |
103
|
15 |
|
if ($config['metadata']['auto_detection']) { |
104
|
15 |
|
foreach ($bundles as $name => $class) { |
105
|
|
|
$ref = new \ReflectionClass($class); |
106
|
|
|
|
107
|
|
|
$directories[$ref->getNamespaceName()] = dirname($ref->getFileName()).'/Resources/config/serializer'; |
108
|
15 |
|
} |
109
|
15 |
|
} |
110
|
15 |
|
foreach ($config['metadata']['directories'] as $directory) { |
111
|
|
|
$directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/'); |
112
|
|
|
|
113
|
|
|
if ('@' === $directory['path'][0]) { |
114
|
|
|
$bundleName = substr($directory['path'], 1, strpos($directory['path'], '/') - 1); |
115
|
|
|
|
116
|
|
|
if (!isset($bundles[$bundleName])) { |
117
|
|
|
throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles)))); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$ref = new \ReflectionClass($bundles[$bundleName]); |
121
|
|
|
$directory['path'] = dirname($ref->getFileName()).substr($directory['path'], strlen('@'.$bundleName)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$directories[rtrim($directory['namespace_prefix'], '\\')] = rtrim($directory['path'], '\\/'); |
125
|
15 |
|
} |
126
|
|
|
$container |
127
|
15 |
|
->getDefinition('jms_serializer.metadata.file_locator') |
128
|
15 |
|
->replaceArgument(0, $directories) |
129
|
|
|
; |
130
|
|
|
|
131
|
15 |
|
$container->setParameter('jms_serializer.xml_deserialization_visitor.doctype_whitelist', $config['visitors']['xml']['doctype_whitelist']); |
132
|
15 |
|
$container->setParameter('jms_serializer.xml_serialization_visitor.format_output', $config['visitors']['xml']['format_output']); |
133
|
15 |
|
$container->setParameter('jms_serializer.json_serialization_visitor.options', $config['visitors']['json']['options']); |
134
|
|
|
|
135
|
15 |
|
if ( ! $config['enable_short_alias']) { |
136
|
|
|
$container->removeAlias('serializer'); |
137
|
|
|
} |
138
|
|
|
|
139
|
15 |
|
if ( ! $container->getParameter('kernel.debug')) { |
140
|
|
|
$container->removeDefinition('jms_serializer.stopwatch_subscriber'); |
141
|
|
|
} |
142
|
15 |
|
} |
143
|
|
|
|
144
|
16 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
145
|
|
|
{ |
146
|
16 |
|
return new Configuration($container->getParameterBag()->resolveValue('%kernel.debug%')); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|