Completed
Push — master ( 4b8173...652bbf )
by WEBEWEB
01:31
created

ConfigurationHelper::loadYamlConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
    public static function getRootNode(TreeBuilder $treeBuilder, $nodeName) {
36
37
        $method = "getRootNode";
38
        if (true === method_exists($treeBuilder, $method)) {
39
            return $treeBuilder->$method();
40
        } else {
41
            $method = "root";
42
        }
43
44
        return $treeBuilder->$method($nodeName);
45
    }
46
47
    /**
48
     * Load a YAML configuration.
49
     *
50
     * @param string $filename The filename.
51
     * @return array Returns the YAML configuration.
52
     */
53
    public static function loadYamlConfig($filename) {
54
55
        $pathname = realpath(__DIR__ . "/../Resources/config/" . $filename . ".yml");
56
        if (false === $pathname) {
57
            return [];
58
        }
59
60
        return Yaml::parse(file_get_contents($pathname));
61
    }
62
63
    /**
64
     * Register a container parameter.
65
     *
66
     * @param Container $container The container.
67
     * @param array $config The configuration.
68
     * @param string $alias The alias.
69
     * @param string $key The key.
70
     * @return void
71
     */
72
    public static function registerContainerParameter(Container $container, array $config, $alias, $key) {
73
        if (false === array_key_exists($key, $config)) {
74
            return;
75
        }
76
        $container->setParameter(implode(".", [$alias, $key]), $config[$key]);
77
    }
78
79
    /**
80
     * Register the container parameters.
81
     *
82
     * @param Container $container The container.
83
     * @param array $config The configuration.
84
     * @return void
85
     */
86
    public static function registerContainerParameters(Container $container, array $config) {
87
        foreach ($config as $k => $v) {
88
            $container->setParameter($k, $v);
89
        }
90
    }
91
}
92