Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

Config::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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