-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYamlDumper.php
145 lines (125 loc) · 3.64 KB
/
YamlDumper.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
<?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\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
trigger_deprecation('zikula/core-bundle', '3.1', 'The "%s" class is deprecated. Use "%s" instead.', YamlDumper::class, Configurator::class);
/**
* @deprecated
* @internal
*/
class YamlDumper
{
/**
* @var Filesystem
*/
protected $fs;
/**
* @var string
*/
protected $fullPath;
public function __construct(string $configDir, string $filePath = 'services_custom.yaml')
{
$this->fullPath = $configDir . DIRECTORY_SEPARATOR . $filePath;
$this->fs = new Filesystem();
}
/**
* Sets a parameter.
*/
public function setParameter(string $name, $value): void
{
$this->validateName($name, true);
$configuration = $this->parseFile();
$configuration['parameters'][$name] = $value;
$this->dumpFile($configuration);
}
/**
* Set all the parameters.
*/
public function setParameters(array $params = []): void
{
foreach ($params as $name => $value) {
$this->validateName($name, true);
}
$configuration = $this->parseFile();
$configuration['parameters'] = $params;
$this->dumpFile($configuration);
}
/**
* Returns a parameter.
*
* @return mixed The parameter value
*/
public function getParameter(string $name)
{
$this->validateName($name, true);
$configuration = $this->parseFile();
return $configuration['parameters'][$name] ?? null;
}
/**
* Return all the parameters.
*/
public function getParameters(): array
{
$configuration = $this->parseFile();
return $configuration['parameters'] ?? [];
}
/**
* Deletes a parameter.
*/
public function delParameter(string $name): void
{
$this->validateName($name, true);
$configuration = $this->parseFile();
if (isset($configuration['parameters'][$name])) {
unset($configuration['parameters'][$name]);
$this->dumpFile($configuration);
}
}
/**
* Parses a Yaml file and return a configuration array.
*/
protected function parseFile(?string $path = null): array
{
$path = $path ?? $this->fullPath;
if (!$this->fs->exists($path)) {
return [];
}
return Yaml::parse(file_get_contents($path));
}
/**
* Dump configuration into dynamic configuration file.
*/
protected function dumpFile(array $configuration = []): void
{
$yaml = Yaml::dump($configuration);
$this->fs->dumpFile($this->fullPath, $yaml);
}
public function deleteFile(): void
{
$this->fs->remove($this->fullPath);
}
/**
* Validates that the name is correct.
*
* @throws InvalidArgumentException Thrown if the name is invalid
*/
protected function validateName(string $name, bool $isParameter): void
{
if (!is_string($name) || (!$isParameter && 'parameters' === $name) || mb_strlen($name) <= 0) {
if ($isParameter) {
throw new InvalidArgumentException('The parameter name must be a string');
}
throw new InvalidArgumentException('The configuration name must not be "parameters" and must be a string');
}
}
}