Passed
Push — master ( 87e26c...e69d87 )
by WEBEWEB
41:09
created

ConfigurationHelper::loadYamlConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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