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
|
|
|
* Factory implementation to create a new initialized configuration instance. |
41
|
|
|
* |
42
|
|
|
* @param string $filename The configuration filename |
43
|
|
|
* @param string $format The format of the configuration file, either one of json, yaml or xml |
44
|
|
|
* @param string $params A serialized string with additional params that'll be passed to the configuration |
45
|
|
|
* @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration |
46
|
|
|
* |
47
|
|
|
* @return \TechDivision\Import\Configuration\Jms\Configuration The configuration instance |
48
|
|
|
* @throws \Exception Is thrown, if the specified configuration file doesn't exist |
49
|
|
|
*/ |
50
|
|
|
public function factory($filename, $format = 'json', $params = null, $paramsFile = null) |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
// try to load the JSON data |
54
|
|
|
if ($data = file_get_contents($filename)) { |
55
|
|
|
// initialize the JMS serializer, load and return the configuration |
56
|
|
|
return $this->factoryFromString($data, $format, $params, $paramsFile); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// throw an exception if the data can not be loaded from the passed file |
60
|
|
|
throw new \Exception(sprintf('Can\'t load configuration file %s', $filename)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Factory implementation to create a new initialized configuration instance. |
65
|
|
|
* |
66
|
|
|
* @param string $data The configuration data |
67
|
|
|
* @param string $format The format of the configuration data, either one of json, yaml or xml |
68
|
|
|
* @param string $params A serialized string with additional params that'll be passed to the configuration |
69
|
|
|
* @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration |
70
|
|
|
* |
71
|
|
|
* @return \TechDivision\Import\Configuration\Jms\Configuration The configuration instance |
72
|
|
|
*/ |
73
|
|
|
public function factoryFromString($data, $format = 'json', $params = null, $paramsFile = null) |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
// initialize the JMS serializer, load and return the configuration |
77
|
|
|
$data = $this->toArray($data, Configuration::class, $format); |
78
|
|
|
|
79
|
|
|
// merge the params, if specified with the --params option |
80
|
|
|
if ($params) { |
|
|
|
|
81
|
|
|
$this->mergeParams( |
82
|
|
|
$data, |
83
|
|
|
$this->toArray( |
84
|
|
|
$params, |
85
|
|
|
Params::class, |
86
|
|
|
$format |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// merge the param loaded from the file, if specified with the --params-file option |
92
|
|
|
if ($paramsFile && is_file($paramsFile)) { |
|
|
|
|
93
|
|
|
$this->mergeParams( |
94
|
|
|
$data, |
95
|
|
|
$this->toArray( |
96
|
|
|
file_get_contents($paramsFile), |
97
|
|
|
Params::class, |
98
|
|
|
pathinfo($paramsFile, PATHINFO_EXTENSION) |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// finally, create and return the configuration from the merge data |
104
|
|
|
return SerializerBuilder::create()->build()->fromArray($data, Configuration::class); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates and returns an array/object tree from the passed array. |
109
|
|
|
* |
110
|
|
|
* @param array $data The data to create the object tree from |
111
|
|
|
* @param string $type The object type to create |
112
|
|
|
* |
113
|
|
|
* @return mixed The array/object tree from the passed array |
114
|
|
|
*/ |
115
|
|
|
protected function fromArray(array $data, $type) |
116
|
|
|
{ |
117
|
|
|
return SerializerBuilder::create()->build()->fromArray($data, $type); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Deserializes the data, converts it into an array and returns it. |
122
|
|
|
* |
123
|
|
|
* @param string $data The data to convert |
124
|
|
|
* @param string $type The object type for the deserialization |
125
|
|
|
* @param string $format The data format, either one of JSON, XML or YAML |
126
|
|
|
* |
127
|
|
|
* @return array The data as array |
128
|
|
|
*/ |
129
|
|
|
protected function toArray($data, $type, $format) |
130
|
|
|
{ |
131
|
|
|
|
132
|
|
|
// load the serializer builde |
133
|
|
|
$serializer = SerializerBuilder::create()->build(); |
134
|
|
|
|
135
|
|
|
// deserialize the data, convert it into an array and return it |
136
|
|
|
return $serializer->toArray($serializer->deserialize($data, $type, $format)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Merge the additional params in the passed configuration data. |
141
|
|
|
* |
142
|
|
|
* @param array $data The array with configuration data |
143
|
|
|
* @param array $params The array with additional params to merge |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
protected function mergeParams(&$data, $params) |
148
|
|
|
{ |
149
|
|
|
|
150
|
|
|
// merge the passed params into the configuration data |
151
|
|
|
foreach ($params as $paramName => $paramValue) { |
152
|
|
|
if (is_array($paramValue)) { |
153
|
|
|
foreach ($paramValue as $key => $value) { |
154
|
|
|
foreach ($value as $name => $x) { |
155
|
|
|
$data[$paramName][$key][$name] = $x; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} else { |
159
|
|
|
$data[$paramName] = $paramValue; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: