|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Configuration\Jms\ConfigurationFactory |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-configuration-jms |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Configuration\Jms; |
|
22
|
|
|
|
|
23
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
24
|
|
|
use TechDivision\Import\ConfigurationFactoryInterface; |
|
25
|
|
|
use TechDivision\Import\Configuration\Jms\Configuration\Params; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The configuration factory implementation. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link https://github.com/techdivision/import-configuration-jms |
|
34
|
|
|
* @link http://www.techdivision.com |
|
35
|
|
|
*/ |
|
36
|
|
|
class ConfigurationFactory implements ConfigurationFactoryInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The configuration parser factory instance used to create a parser that |
|
41
|
|
|
* parsers and merges the configuration from different directories. |
|
42
|
|
|
* |
|
43
|
|
|
* @var \TechDivision\Import\Configuration\Jms\ConfigurationParserFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $configurationParserFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Initializes the instance with the configuration parser factory instance. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \TechDivision\Import\Configuration\Jms\ConfigurationParserFactoryInterface $configurationParserFactory The configuration parser factory instance |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(ConfigurationParserFactoryInterface $configurationParserFactory) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->configurationParserFactory = $configurationParserFactory; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Return's the configuration parser factory instance. |
|
59
|
|
|
* |
|
60
|
|
|
* @return \TechDivision\Import\Configuration\Jms\ConfigurationParserFactoryInterface The configuration parser factory instance |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getConfigurationParserFactory() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->configurationParserFactory; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the configuration parser for the passed format. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $format The format of the configuration file to return the parser for (either one of json, yaml or xml) |
|
71
|
|
|
* |
|
72
|
|
|
* @return \TechDivision\Import\Configuration\Jms\ConfigurationParserInterface The configuration parser instance |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getConfigurationParser($format) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->getConfigurationParserFactory()->factory($format); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Factory implementation to create a new initialized configuration instance. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $filename The configuration filename |
|
83
|
|
|
* @param string $format The format of the configuration file, either one of json, yaml or xml |
|
84
|
|
|
* @param string $params A serialized string with additional params that'll be passed to the configuration |
|
85
|
|
|
* @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration |
|
86
|
|
|
* |
|
87
|
|
|
* @return \TechDivision\Import\Configuration\Jms\Configuration The configuration instance |
|
88
|
|
|
* @throws \Exception Is thrown, if the specified configuration file doesn't exist |
|
89
|
|
|
*/ |
|
90
|
|
|
public function factory($filename, $format = 'json', $params = null, $paramsFile = null) |
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
// try to load the JSON data |
|
94
|
|
|
if ($data = file_get_contents($filename)) { |
|
95
|
|
|
// initialize the JMS serializer, load and return the configuration |
|
96
|
|
|
return $this->factoryFromString($data, $format, $params, $paramsFile); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// throw an exception if the data can not be loaded from the passed file |
|
100
|
|
|
throw new \Exception(sprintf('Can\'t load configuration file %s', $filename)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Factory implementation to create a new initialized configuration instance from a file |
|
105
|
|
|
* with configurations that'll be parsed and merged. |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $directories An array with diretories to parse and merge |
|
108
|
|
|
* @param string $format The format of the configuration file, either one of json, yaml or xml |
|
109
|
|
|
* @param string $params A serialized string with additional params that'll be passed to the configuration |
|
110
|
|
|
* @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public function factoryFromDirectories(array $directories = array(), $format = 'json', $params = null, $paramsFile = null) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->factoryFromString($this->getConfigurationParser($format)->parse($directories), $format, $params, $paramsFile); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Factory implementation to create a new initialized configuration instance. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $data The configuration data |
|
123
|
|
|
* @param string $format The format of the configuration data, either one of json, yaml or xml |
|
124
|
|
|
* @param string $params A serialized string with additional params that'll be passed to the configuration |
|
125
|
|
|
* @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration |
|
126
|
|
|
* |
|
127
|
|
|
* @return \TechDivision\Import\ConfigurationInterface The configuration instance |
|
128
|
|
|
*/ |
|
129
|
|
|
public function factoryFromString($data, $format = 'json', $params = null, $paramsFile = null) |
|
130
|
|
|
{ |
|
131
|
|
|
|
|
132
|
|
|
// initialize the JMS serializer, load and return the configuration |
|
133
|
|
|
$data = $this->toArray($data, Configuration::class, $format); |
|
134
|
|
|
|
|
135
|
|
|
// merge the params, if specified with the --params option |
|
136
|
|
|
if ($params) { |
|
|
|
|
|
|
137
|
|
|
$this->mergeParams( |
|
138
|
|
|
$data, |
|
139
|
|
|
$this->toArray( |
|
140
|
|
|
$params, |
|
141
|
|
|
Params::class, |
|
142
|
|
|
$format |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// merge the param loaded from the file, if specified with the --params-file option |
|
148
|
|
|
if ($paramsFile && is_file($paramsFile)) { |
|
|
|
|
|
|
149
|
|
|
$this->mergeParams( |
|
150
|
|
|
$data, |
|
151
|
|
|
$this->toArray( |
|
152
|
|
|
file_get_contents($paramsFile), |
|
153
|
|
|
Params::class, |
|
154
|
|
|
pathinfo($paramsFile, PATHINFO_EXTENSION) |
|
155
|
|
|
) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// finally, create and return the configuration from the merge data |
|
160
|
|
|
return SerializerBuilder::create()->build()->fromArray($data, Configuration::class); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Creates and returns an array/object tree from the passed array. |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $data The data to create the object tree from |
|
167
|
|
|
* @param string $type The object type to create |
|
168
|
|
|
* |
|
169
|
|
|
* @return mixed The array/object tree from the passed array |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function fromArray(array $data, $type) |
|
172
|
|
|
{ |
|
173
|
|
|
return SerializerBuilder::create()->build()->fromArray($data, $type); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Deserializes the data, converts it into an array and returns it. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $data The data to convert |
|
180
|
|
|
* @param string $type The object type for the deserialization |
|
181
|
|
|
* @param string $format The data format, either one of JSON, XML or YAML |
|
182
|
|
|
* |
|
183
|
|
|
* @return array The data as array |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function toArray($data, $type, $format) |
|
186
|
|
|
{ |
|
187
|
|
|
|
|
188
|
|
|
// load the serializer builde |
|
189
|
|
|
$serializer = SerializerBuilder::create()->build(); |
|
190
|
|
|
|
|
191
|
|
|
// deserialize the data, convert it into an array and return it |
|
192
|
|
|
return $serializer->toArray($serializer->deserialize($data, $type, $format)); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Merge the additional params in the passed configuration data. |
|
197
|
|
|
* |
|
198
|
|
|
* @param array $data The array with configuration data |
|
199
|
|
|
* @param array $params The array with additional params to merge |
|
200
|
|
|
* |
|
201
|
|
|
* @return void |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function mergeParams(&$data, $params) |
|
204
|
|
|
{ |
|
205
|
|
|
|
|
206
|
|
|
// merge the passed params into the configuration data |
|
207
|
|
|
foreach ($params as $paramName => $paramValue) { |
|
208
|
|
|
if (is_array($paramValue)) { |
|
209
|
|
|
foreach ($paramValue as $key => $value) { |
|
210
|
|
|
foreach ($value as $name => $x) { |
|
211
|
|
|
$data[$paramName][$key][$name] = $x; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} else { |
|
215
|
|
|
$data[$paramName] = $paramValue; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: