|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file was originally received from JMSSerializerBundle; since then, |
|
5
|
|
|
* it has been modified, and does only in parts resemble the original source code. |
|
6
|
|
|
* |
|
7
|
|
|
* To the original source code, the following license applies: |
|
8
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
11
|
|
|
* you may not use this file except in compliance with the License. |
|
12
|
|
|
* You may obtain a copy of the License at |
|
13
|
|
|
* |
|
14
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
15
|
|
|
* |
|
16
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
17
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
18
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
19
|
|
|
* See the License for the specific language governing permissions and |
|
20
|
|
|
* limitations under the License. |
|
21
|
|
|
* |
|
22
|
|
|
* To all other portions of code, the following license applies: |
|
23
|
|
|
* |
|
24
|
|
|
* This file is part of the SimpleSerializerBundle package. |
|
25
|
|
|
* |
|
26
|
|
|
* Copyright (c) 2012 Farheap Solutions (http://www.farheap.com) |
|
27
|
|
|
* |
|
28
|
|
|
* For the full copyright and license information, please view the |
|
29
|
|
|
* LICENSE file that was distributed with this source code. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
namespace Opensoft\Bundle\SimpleSerializerBundle\DependencyInjection; |
|
33
|
|
|
|
|
34
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
35
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
36
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
37
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
38
|
|
|
use Symfony\Component\Config\FileLocator; |
|
39
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
40
|
|
|
use Opensoft\Bundle\SimpleSerializerBundle\Exception\RuntimeException; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @author Dmitry Petrov <[email protected]> |
|
44
|
|
|
*/ |
|
45
|
|
|
class OpensoftSimpleSerializerExtension extends Extension |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var KernelInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $kernel; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param KernelInterface $kernel |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(KernelInterface $kernel) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->kernel = $kernel; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $configs |
|
64
|
|
|
* @param ContainerBuilder $container |
|
65
|
|
|
*/ |
|
66
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
67
|
|
|
{ |
|
68
|
|
|
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
71
|
|
|
$loader->load('services.xml'); |
|
72
|
|
|
|
|
73
|
|
|
// metadata |
|
74
|
|
|
if ('none' === $config['metadata']['cache']) { |
|
75
|
|
|
$container->removeAlias('opensoft_simple_serializer.metadata.cache'); |
|
76
|
|
|
} else if ('file' === $config['metadata']['cache']) { |
|
77
|
|
|
$container |
|
78
|
|
|
->getDefinition('opensoft_simple_serializer.metadata.cache.file') |
|
79
|
|
|
->replaceArgument(0, $config['metadata']['file_cache']['dir']) |
|
80
|
|
|
; |
|
81
|
|
|
|
|
82
|
|
|
$dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']); |
|
83
|
|
|
if (!file_exists($dir)) { |
|
84
|
|
|
if (!$rs = @mkdir($dir, 0777, true)) { |
|
85
|
|
|
throw new RuntimeException(sprintf('Could not create cache directory "%s".', $dir)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} else { |
|
89
|
|
|
$container->setAlias('opensoft_simple_serializer.metadata.cache.file', new Alias($config['metadata']['cache'], false)); |
|
90
|
|
|
} |
|
91
|
|
|
$container |
|
92
|
|
|
->getDefinition('opensoft_simple_serializer.metadata.factory') |
|
93
|
|
|
->replaceArgument(2, $config['metadata']['debug']) |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
// directories |
|
98
|
|
|
$directories = array(); |
|
99
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
|
100
|
|
|
if ($config['metadata']['auto_detection']) { |
|
101
|
|
|
foreach ($bundles as $class) { |
|
102
|
|
|
$ref = new \ReflectionClass($class); |
|
103
|
|
|
|
|
104
|
|
|
$directories[$ref->getNamespaceName()] = dirname($ref->getFileName()) . '/Resources/config/simple-serializer'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
foreach ($config['metadata']['directories'] as $directory) { |
|
108
|
|
|
$directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/'); |
|
109
|
|
|
|
|
110
|
|
|
if ($directory['path'][0] === '@') { |
|
111
|
|
|
$bundleName = substr($directory['path'], 1, strpos($directory['path'], '/') - 1); |
|
112
|
|
|
|
|
113
|
|
|
if (!isset($bundles[$bundleName])) { |
|
114
|
|
|
throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. |
|
115
|
|
|
Available bundles: %s', $bundleName, implode(', ', array_keys($bundles)))); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$ref = new \ReflectionClass($bundles[$bundleName]); |
|
119
|
|
|
$directory['path'] = dirname($ref->getFileName()) . substr($directory['path'], strlen('@'.$bundleName)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$directories[rtrim($directory['namespace_prefix'], '\\')] = rtrim($directory['path'], '\\/'); |
|
123
|
|
|
} |
|
124
|
|
|
$container |
|
125
|
|
|
->getDefinition('opensoft_simple_serializer.metadata.driver.file_locator') |
|
126
|
|
|
->replaceArgument(0, $directories) |
|
127
|
|
|
; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritDoc} |
|
132
|
|
|
* |
|
133
|
|
|
* @param array $config |
|
134
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
135
|
|
|
* @return null|Configuration |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
138
|
|
|
{ |
|
139
|
|
|
return new Configuration($this->kernel->isDebug()); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: