Passed
Push — master ( 451245...2b5fd7 )
by Brent
03:56
created

App::getParameter()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\Console\Application;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9
use Symfony\Component\Yaml\Yaml;
10
11
class App
12
{
13
    /**
14
     * @var ContainerBuilder
15
     */
16
    protected static $container;
17
18
    /**
19
     * @var array
20
     */
21
    protected static $configDefaults = [
22
        'environment'          => 'development',
23
        'directories.src'      => './src',
24
        'directories.public'   => './public',
25
        'directories.cache'    => './.cache',
26
        'directories.htaccess' => './public/.htaccess',
27
        'meta'                 => [],
28
        'minify'               => false,
29
        'engines.template'     => 'smarty',
30
        'engines.image'        => 'gd',
31
        'engines.optimizer'    => true,
32
        'engines.async'        => true,
33
        'cdn'                  => [],
34
        'caches.image'         => true,
35
        'caches.cdn'           => true,
36
        'optimizer.options'    => [],
37
    ];
38
39
    /**
40
     * @param string $configPath
41
     * @param array  $defaultConfig
42
     *
43
     * @return App
44
     */
45
    public static function init(string $configPath = './config.yml', array $defaultConfig = []) : App {
46
        self::$container = new ContainerBuilder();
47
48
        $configFile = Config::getConfigFile($configPath);
49
        $parsedConfig = Yaml::parse($configFile->getContents());
50
        $parsedConfig = Config::parseImports($parsedConfig);
51
52
        $config = array_merge(
53
            self::$configDefaults,
54
            $parsedConfig,
55
            Config::flatten($parsedConfig),
56
            $defaultConfig
57
        );
58
59
        $config['directories.template'] = $config['directories.template'] ?? $config['directories.src'];
60
61
        foreach ($config as $key => $value) {
62
            self::$container->setParameter($key, $value);
63
        }
64
65
        $serviceLoader = new YamlFileLoader(self::$container, new FileLocator(__DIR__));
66
        $serviceLoader->load(__DIR__ . '/../../services.yml');
67
68
        return new self();
69
    }
70
71
    /**
72
     * @param string $id
73
     *
74
     * @return mixed
75
     */
76
    public static function get(string $id) {
77
        return self::$container->get($id);
78
    }
79
80
    /**
81
     * @param string      $id
82
     * @param Application $application
83
     */
84
    public static function setApplication(string $id, Application $application) {
85
        self::$container->set($id, $application);
86
    }
87
88
    /**
89
     * @param string $key
90
     *
91
     * @return mixed
92
     */
93
    public static function getParameter(string $key) {
94
        return self::$container->getParameter($key);
95
    }
96
97
}
98