|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Zikula package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Zikula\Bundle\CoreBundle; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class DynamicConfigDumper. |
|
21
|
|
|
*/ |
|
22
|
|
|
class DynamicConfigDumper extends YamlDumper |
|
23
|
|
|
{ |
|
24
|
|
|
public const CONFIG_GENERATED = 'dynamic/generated.yaml'; |
|
25
|
|
|
|
|
26
|
|
|
public const CONFIG_DEFAULT = 'dynamic/default.yaml'; |
|
27
|
|
|
|
|
28
|
|
|
private $configDir; |
|
29
|
|
|
|
|
30
|
|
|
private $env; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct($configDir, string $env) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->configDir = $configDir; |
|
35
|
|
|
$this->env = $env; |
|
36
|
|
|
$this->fullPath = $configDir . DIRECTORY_SEPARATOR . self::CONFIG_GENERATED; |
|
37
|
|
|
$configDefaultPath = $configDir . DIRECTORY_SEPARATOR . self::CONFIG_DEFAULT; |
|
38
|
|
|
$this->fs = new Filesystem(); |
|
39
|
|
|
|
|
40
|
|
|
if (!$this->fs->exists($this->fullPath)) { |
|
41
|
|
|
// This class is called for the very first time. Make a copy of the default configuration file and safe |
|
42
|
|
|
// it as generated configuration file. |
|
43
|
|
|
$this->fs->copy($configDefaultPath, $this->fullPath); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Dump configuration into dynamic configuration file. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function dumpFile(array $configuration = [], bool $appendEnv = false): void |
|
51
|
|
|
{ |
|
52
|
|
|
$flags = Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE; // for #2889 |
|
53
|
|
|
$yaml = "#This is a dynamically generated configuration file. Do not touch!\n\n" . Yaml::dump($configuration, 4, 4, $flags); |
|
54
|
|
|
$path = $appendEnv ? $this->configDir . '/dynamic/generated_' . $this->env . '.yaml' : $this->fullPath; |
|
55
|
|
|
$this->fs->dumpFile($path, $yaml); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Sets a configuration. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setConfiguration(string $name, $value, bool $appendEnv = false): void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->validateName($name, false); |
|
64
|
|
|
$configuration = $appendEnv ? [] : $this->parseFile(); |
|
65
|
|
|
$configuration[$name] = $value; |
|
66
|
|
|
$this->dumpFile($configuration, $appendEnv); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|