|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2019 WEBEWEB |
|
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 WBW\Bundle\CoreBundle\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
15
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
|
16
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Configuration helper. |
|
22
|
|
|
* |
|
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
24
|
|
|
* @package WBW\Bundle\CoreBundle\DependencyInjection |
|
25
|
|
|
*/ |
|
26
|
|
|
class ConfigurationHelper { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get a root node. |
|
30
|
|
|
* |
|
31
|
|
|
* @param TreeBuilder $treeBuilder The tree builder. |
|
32
|
|
|
* @param string $nodeName The node name. |
|
33
|
|
|
* @return ArrayNodeDefinition|NodeDefinition Returns the root node. |
|
34
|
|
|
*/ |
|
35
|
63 |
|
public static function getRootNode(TreeBuilder $treeBuilder, string $nodeName): NodeDefinition { |
|
36
|
|
|
|
|
37
|
63 |
|
$method = "getRootNode"; |
|
38
|
63 |
|
if (true === method_exists($treeBuilder, $method)) { |
|
39
|
49 |
|
return $treeBuilder->$method(); |
|
40
|
|
|
} else { |
|
41
|
14 |
|
$method = "root"; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
14 |
|
return $treeBuilder->$method($nodeName); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Load a YAML configuration. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $directory The directory. |
|
51
|
|
|
* @param string $filename The filename. |
|
52
|
|
|
* @return array Returns the YAML configuration. |
|
53
|
|
|
*/ |
|
54
|
90 |
|
public static function loadYamlConfig(string $directory, string $filename): array { |
|
55
|
|
|
|
|
56
|
90 |
|
$pathname = realpath($directory . "/../Resources/config/" . $filename . ".yml"); |
|
57
|
90 |
|
if (false === $pathname) { |
|
58
|
9 |
|
return []; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
81 |
|
return Yaml::parse(file_get_contents($pathname)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Register a container parameter. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Container $container The container. |
|
68
|
|
|
* @param array $config The configuration. |
|
69
|
|
|
* @param string $alias The alias. |
|
70
|
|
|
* @param string $key The key. |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
63 |
|
public static function registerContainerParameter(Container $container, array $config, string $alias, string $key): void { |
|
74
|
63 |
|
if (false === array_key_exists($key, $config)) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
63 |
|
$container->setParameter(implode(".", [$alias, $key]), $config[$key]); |
|
78
|
63 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Register the container parameters. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Container $container The container. |
|
84
|
|
|
* @param array $config The configuration. |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
63 |
|
public static function registerContainerParameters(Container $container, array $config): void { |
|
88
|
63 |
|
foreach ($config as $k => $v) { |
|
89
|
63 |
|
$container->setParameter($k, $v); |
|
90
|
|
|
} |
|
91
|
63 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|