-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurator.php
210 lines (186 loc) · 7.26 KB
/
Configurator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Bundle\CoreBundle;
use InvalidArgumentException;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
/**
* This class manages configuration, validation and write-to-file for a select
* subgroup of bundle configs (see $configurablePackages).
*
* To use, one must `loadPackages` by name. `Set` configuration as needed.
* Then `write` the package config to the desired location. When the config
* is loaded, the existing (if any) config settings are loaded in addition
* to any default settings.
*
* The class defaults to write only the minimal required config (removing default
* values) into the main `config/packages` (no env) directory.
*/
class Configurator
{
/** @var string */
private $configDir;
/** @var Filesystem */
private $fs;
/** @var array */
private $processedConfigurations = [];
/** @var array */
private $defaultConfigurations = [];
/** @var string[] */
private $configurablePackages = [
'core' => 'Zikula\Bundle\CoreBundle\DependencyInjection\Configuration',
'zikula_security_center' => 'Zikula\SecurityCenterModule\DependencyInjection\Configuration',
'zikula_theme' => 'Zikula\ThemeModule\DependencyInjection\Configuration',
'zikula_routes' => 'Zikula\RoutesModule\DependencyInjection\Configuration',
'zikula_settings' => 'Zikula\SettingsModule\DependencyInjection\Configuration'
];
public function __construct(string $projectDir)
{
$this->configDir = $projectDir . '/config';
$this->fs = new Filesystem();
}
public function loadPackages($packages, string $env = 'prod'): void
{
if (!is_array($packages)) {
$packages = (array) $packages;
}
foreach ($packages as $package) {
if (!isset($this->configurablePackages[$package])) {
throw new \InvalidArgumentException(sprintf('Package %s is not available for configuration. Please configure manually.', $package));
}
$configs = [];
foreach ($this->getPaths($env) as $path) {
if (file_exists($fullPath = $path . $package . '.yaml') && false !== $contents = file_get_contents($fullPath)) {
$configs[] = Yaml::parse($contents)[$package];
}
}
$configuration = new $this->configurablePackages[$package]();
$this->processedConfigurations[$package] = $this->process(
$configuration,
$configs
);
$this->defaultConfigurations[$package] = $this->process($configuration);
}
}
/**
* @throws InvalidConfigurationException
*/
public function validatePackage(string $package): array
{
$config = $this->getAll($package);
/** @var ConfigurationInterface $configuration */
$configuration = new $this->configurablePackages[$package]();
return $this->process(
$configuration,
[$package => $config]
);
}
/**
* @throws InvalidConfigurationException
*/
public function process(ConfigurationInterface $configuration, array $config = []): array
{
$processor = new Processor();
return $processor->processConfiguration(
$configuration,
$config
);
}
/**
* @throws IOException if the file(s) cannot be written to
*/
public function write(bool $min = true, string $env = ''): void
{
foreach (array_keys($this->processedConfigurations) as $package) {
$this->validatePackage($package);
$this->writePackage($package, $min, $env);
}
}
/**
* @throws IOException if the file cannot be written to
*/
public function writePackage(string $package, bool $min = true, string $env = ''): void
{
$config = $min ? $this->arrayDiffAssocRecursive($this->processedConfigurations[$package], $this->getDefaults($package)) : $this->processedConfigurations[$package];
$basePath = $this->configDir . '/packages/' . (!empty($env) ? $env . '/' : '');
$path = $basePath . $package . '.yaml';
if (!empty($config)) {
$flags = Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE;
$input = [$package => $config];
$yaml = Yaml::dump($input, 4, 4, $flags);
$this->fs->dumpFile($path, $yaml);
} elseif ($this->fs->exists($path)) {
$this->fs->remove($path);
}
}
public function set(string $package, string $key, $value): void
{
if (!array_key_exists($key, $this->processedConfigurations[$package])) {
throw new InvalidArgumentException(sprintf('Cannot set %s in the %s package because it is not configurable', $key, $package));
}
$this->processedConfigurations[$package][$key] = $value;
}
public function get(string $package, string $key)
{
if (!isset($this->processedConfigurations[$package][$key])) {
throw new InvalidArgumentException(sprintf('The %s is not set in the %s package', $key, $package));
}
return $this->processedConfigurations[$package][$key];
}
public function getAll(string $package): array
{
if (!isset($this->processedConfigurations[$package])) {
throw new InvalidArgumentException(sprintf('The %s package is not set', $package));
}
return $this->processedConfigurations[$package];
}
public function getDefaults(string $package): array
{
if (!isset($this->defaultConfigurations[$package])) {
throw new InvalidArgumentException(sprintf('The %s package is not set', $package));
}
return $this->defaultConfigurations[$package];
}
private function getPaths(string $env = 'prod'): array
{
return [
0 => $this->configDir . '/packages/',
1 => $this->configDir . '/packages/' . $env,
];
}
/**
* @author Giosh 2013-03-15
* @see https://www.php.net/manual/en/function.array-diff-assoc.php#111675
*/
public function arrayDiffAssocRecursive(array $array1, array $array2): array
{
$difference=[];
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$newDiff = $this->arrayDiffAssocRecursive($value, $array2[$key]);
if (!empty($newDiff)) {
$difference[$key] = $newDiff;
}
}
} elseif (!array_key_exists($key, $array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
}