1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Manager\Config; |
13
|
|
|
|
14
|
|
|
use Puli\Manager\Api\Config\Config; |
15
|
|
|
use Puli\Manager\Api\Config\ConfigFile; |
16
|
|
|
use Puli\Manager\Api\Factory\FactoryManager; |
17
|
|
|
use Puli\Manager\Api\FileNotFoundException; |
18
|
|
|
use Puli\Manager\Api\InvalidConfigException; |
19
|
|
|
use Puli\Manager\Api\Storage\ReadException; |
20
|
|
|
use Puli\Manager\Api\Storage\Storage; |
21
|
|
|
use Puli\Manager\Api\Storage\WriteException; |
22
|
|
|
use stdClass; |
23
|
|
|
use Webmozart\Json\Conversion\ConversionFailedException; |
24
|
|
|
use Webmozart\Json\Conversion\JsonConverter; |
25
|
|
|
use Webmozart\Json\DecodingFailedException; |
26
|
|
|
use Webmozart\Json\EncodingFailedException; |
27
|
|
|
use Webmozart\Json\JsonDecoder; |
28
|
|
|
use Webmozart\Json\JsonEncoder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Loads and saves configuration files. |
32
|
|
|
* |
33
|
|
|
* @since 1.0 |
34
|
|
|
* |
35
|
|
|
* @author Bernhard Schussek <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class ConfigFileStorage |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var Storage |
41
|
|
|
*/ |
42
|
|
|
private $storage; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var JsonConverter |
46
|
|
|
*/ |
47
|
|
|
private $configFileConverter; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var JsonEncoder |
51
|
|
|
*/ |
52
|
|
|
private $jsonEncoder; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var JsonDecoder |
56
|
|
|
*/ |
57
|
|
|
private $jsonDecoder; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var FactoryManager |
61
|
|
|
*/ |
62
|
|
|
private $factoryManager; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Creates a new configuration file storage. |
66
|
|
|
* |
67
|
|
|
* @param Storage $storage The file storage. |
68
|
|
|
* @param JsonConverter $configFileConverter The JSON converter for |
69
|
|
|
* {@link ConfigFile} |
70
|
|
|
* instances. |
71
|
|
|
* @param JsonEncoder $jsonEncoder The JSON encoder. |
72
|
|
|
* @param JsonDecoder $jsonDecoder The JSON decoder. |
73
|
|
|
* @param FactoryManager|null $factoryManager The manager used to |
74
|
|
|
* regenerate the factory |
75
|
|
|
* class after saving the |
76
|
|
|
* config file. |
77
|
|
|
*/ |
78
|
54 |
View Code Duplication |
public function __construct( |
|
|
|
|
79
|
|
|
Storage $storage, |
80
|
|
|
JsonConverter $configFileConverter, |
81
|
|
|
JsonEncoder $jsonEncoder, |
82
|
|
|
JsonDecoder $jsonDecoder, |
83
|
|
|
FactoryManager $factoryManager = null |
84
|
|
|
) { |
85
|
54 |
|
$this->storage = $storage; |
86
|
54 |
|
$this->configFileConverter = $configFileConverter; |
87
|
54 |
|
$this->jsonEncoder = $jsonEncoder; |
88
|
54 |
|
$this->jsonDecoder = $jsonDecoder; |
89
|
54 |
|
$this->factoryManager = $factoryManager; |
90
|
54 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Loads a configuration file from a path. |
94
|
|
|
* |
95
|
|
|
* If the path does not exist, an empty configuration file is returned. |
96
|
|
|
* |
97
|
|
|
* @param string $path The path to the configuration file. |
98
|
|
|
* @param Config|null $baseConfig The configuration that the loaded |
99
|
|
|
* configuration will inherit its values |
100
|
|
|
* from. |
101
|
|
|
* |
102
|
|
|
* @return ConfigFile The loaded configuration file. |
103
|
|
|
* |
104
|
|
|
* @throws FileNotFoundException If the file does not exist. |
105
|
|
|
* @throws ReadException If the file cannot be read. |
106
|
|
|
* @throws InvalidConfigException If the file contains invalid configuration. |
107
|
|
|
*/ |
108
|
50 |
View Code Duplication |
public function loadConfigFile($path, Config $baseConfig = null) |
|
|
|
|
109
|
|
|
{ |
110
|
50 |
|
$json = $this->storage->read($path); |
111
|
49 |
|
$jsonData = $this->decode($json, $path); |
112
|
|
|
|
113
|
|
|
try { |
114
|
48 |
|
return $this->configFileConverter->fromJson($jsonData, array( |
115
|
48 |
|
'path' => $path, |
116
|
48 |
|
'baseConfig' => $baseConfig, |
117
|
|
|
)); |
118
|
1 |
|
} catch (ConversionFailedException $e) { |
119
|
1 |
|
throw new InvalidConfigException(sprintf( |
120
|
1 |
|
'The JSON in %s could not be converted: %s', |
121
|
|
|
$path, |
122
|
1 |
|
$e->getMessage() |
123
|
1 |
|
), 0, $e); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Saves a configuration file. |
129
|
|
|
* |
130
|
|
|
* The configuration file is saved to the same path that it was read from. |
131
|
|
|
* |
132
|
|
|
* @param ConfigFile $configFile The configuration file to save. |
133
|
|
|
* |
134
|
|
|
* @throws WriteException If the file cannot be written. |
135
|
|
|
* @throws InvalidConfigException If the file contains invalid configuration. |
136
|
|
|
*/ |
137
|
4 |
View Code Duplication |
public function saveConfigFile(ConfigFile $configFile) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
try { |
140
|
4 |
|
$jsonData = $this->configFileConverter->toJson($configFile); |
141
|
1 |
|
} catch (ConversionFailedException $e) { |
142
|
1 |
|
throw new InvalidConfigException(sprintf( |
143
|
1 |
|
'The data written to %s could not be converted: %s', |
144
|
1 |
|
$configFile->getPath(), |
145
|
1 |
|
$e->getMessage() |
146
|
1 |
|
), 0, $e); |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
$json = $this->encode($jsonData, $configFile->getPath()); |
150
|
|
|
|
151
|
2 |
|
$this->storage->write($configFile->getPath(), $json); |
152
|
|
|
|
153
|
2 |
|
if ($this->factoryManager) { |
154
|
1 |
|
$this->factoryManager->autoGenerateFactoryClass(); |
155
|
|
|
} |
156
|
2 |
|
} |
157
|
|
|
|
158
|
3 |
View Code Duplication |
private function encode(stdClass $jsonData, $path) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
try { |
161
|
3 |
|
return $this->jsonEncoder->encode($jsonData); |
162
|
1 |
|
} catch (EncodingFailedException $e) { |
163
|
1 |
|
throw new InvalidConfigException(sprintf( |
164
|
1 |
|
'The configuration in %s could not be encoded: %s', |
165
|
|
|
$path, |
166
|
1 |
|
$e->getMessage() |
167
|
1 |
|
), 0, $e); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
49 |
View Code Duplication |
private function decode($json, $path) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
try { |
174
|
49 |
|
return $this->jsonDecoder->decode($json); |
175
|
1 |
|
} catch (DecodingFailedException $e) { |
176
|
1 |
|
throw new InvalidConfigException(sprintf( |
177
|
1 |
|
'The configuration in %s could not be decoded: %s', |
178
|
|
|
$path, |
179
|
1 |
|
$e->getMessage() |
180
|
1 |
|
), 0, $e); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.