Passed
Push — master ( 3473af...b022ea )
by Brent
02:56
created

Config::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace brendt\stitcher;
4
5
use brendt\stitcher\engine\EnginePlugin;
6
use brendt\stitcher\factory\ProviderFactory;
7
use brendt\stitcher\factory\TemplateEngineFactory;
8
use Leafo\ScssPhp\Compiler;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Yaml\Yaml;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use brendt\stitcher\engine\smarty\SmartyEngine;
13
14
/**
15
 * Class Config
16
 * @package brendt\stitcher
17
 */
18
class Config {
19
20
    /**
21
     * @var array
22
     */
23
    protected static $config;
24
25
    /**
26
     * @var ContainerBuilder
27
     */
28
    protected static $container;
29
30
    public static function getConfig() {
31
        return self::$config;
32
    }
33
34
    /**
35
     * @param string $root
36
     * @param string $name
37
     */
38
    public static function load($root = './', $name = 'config.yml') {
39
        $finder = new Finder();
40
        $configFiles = $finder->files()->in($root)->name($name);
41
        $config = [];
42
43
        foreach ($configFiles as $configFile) {
44
            $config += Yaml::parse($configFile->getContents());
45
        }
46
47
        foreach ($config as $key => $value) {
48
            self::$config[$key] = $value;
49
        }
50
51
        self::$container = new ContainerBuilder();
52
        self::$container->register('factory.provider', ProviderFactory::class);
53
        self::$container->register('factory.template.engine', TemplateEngineFactory::class);
54
        self::$container->register('engine.smarty', SmartyEngine::class);
55
        self::$container->register('engine.plugin', EnginePlugin::class);
56
        self::$container->register('engine.minify.css', CSSmin::class);
57
        self::$container->register('engine.sass', Compiler::class)
58
            ->addMethodCall('addImportPath', ['path' => Config::get('directories.src')]);
59
    }
60
61
    /**
62
     * @param $id
63
     *
64
     * @return object
65
     */
66
    public static function getDependency($id) {
67
        return self::$container->get($id);
68
    }
69
70
    /**
71
     * @param $key
72
     *
73
     * @return mixed|null
74
     */
75
    public static function get($key) {
76
        $keys = explode('.', $key);
77
        $config = self::$config;
78
79
        reset($keys);
80
81
        while (($key = current($keys)) && isset($config[$key])) {
82
            $hasNext = next($keys);
83
84
            if (!$hasNext) {
85
                return $config[$key];
86
            }
87
88
            $config = $config[$key];
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * @param $key
96
     * @param $value
97
     */
98
    public static function set($key, $value) {
99
        $keys = explode('.', $key);
100
        $configEntry = self::createConfigEntry($keys, $value);
101
102
        self::$config = array_merge(self::$config, $configEntry);
103
    }
104
105
    /**
106
     * Reset the config
107
     */
108
    public static function reset() {
109
        self::$config = [];
110
    }
111
112
    /**
113
     * @param $keys
114
     * @param $value
115
     *
116
     * @return array
117
     */
118
    private static function createConfigEntry($keys, $value) {
119
        $configEntry = [];
120
        $key = array_shift($keys);
121
122
        if (count($keys)) {
123
            $configEntry[$key] = self::createConfigEntry($keys, $value);
124
        } else {
125
            $configEntry[$key] = $value;
126
        }
127
128
        return $configEntry;
129
    }
130
131
}
132