Completed
Push — master ( 6d0ed9...136960 )
by Alexis
05:49
created

Application::readConfigFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace App;
4
5
use Slim\App;
6
7
class Application extends App
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $environment;
13
14
    /**
15
     * @var string
16
     */
17
    protected $rootDir;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param string $environment
23
     */
24
    public function __construct($environment)
25
    {
26
        $this->environment = $environment;
27
        $this->rootDir = $this->getRootDir();
28
29
        parent::__construct($this->loadConfiguration());
30
31
        $this->configureContainer();
32
        $this->registerHandlers();
33
        $this->loadMiddleware();
34
        $this->registerControllers();
35
        $this->loadRoutes();
36
    }
37
38
    public function getCacheDir()
39
    {
40
        return $this->getRootDir().'/var/cache/'.$this->environment;
41
    }
42
43
    public function getConfigurationDir()
44
    {
45
        return $this->getRootDir().'/config';
46
    }
47
48
    public function getEnvironment()
49
    {
50
        return $this->environment;
51
    }
52
53
    public function getLogDir()
54
    {
55
        return $this->getRootDir().'/var/log';
56
    }
57
58
    public function getRootDir()
59
    {
60
        if (null === $this->rootDir) {
61
            $this->rootDir = dirname(__DIR__);
62
        }
63
64
        return $this->rootDir;
65
    }
66
67
    protected function configureContainer()
68
    {
69
        $this->readConfigFile('container.php', ['container' => $this->getContainer()]);
70
    }
71
72
    protected function loadConfiguration()
73
    {
74
        $settings = $this->readConfigFile('framework.php', ['app' => $this]);
75
76
        if (file_exists($this->getConfigurationDir().'/services.'.$this->getEnvironment().'.php')) {
77
            $filename = 'services.'.$this->getEnvironment().'.php';
78
        } else {
79
            $filename = 'services.php';
80
        }
81
82
        $settings += $this->readConfigFile($filename, ['app' => $this]);
83
84
        return ['settings' => $settings];
85
    }
86
87
    protected function loadMiddleware()
88
    {
89
        $this->readConfigFile('middleware.php', [
90
            'app' => $this,
91
            'container' => $this->getContainer()
92
        ]);
93
    }
94
95
    protected function loadRoutes()
96
    {
97
        $this->readConfigFile('routes.php', [
98
            'app' => $this,
99
            'container' => $this->getContainer()
100
        ]);
101
    }
102
103
    protected function registerControllers()
104
    {
105
        $container = $this->getContainer();
106
        $controllers = $this->readConfigFile('controllers.php', ['container' => $container]);
107
108
        foreach ($controllers as $key => $class) {
109
            $container['controller.'.$key] = function ($container) use ($class) {
110
                return new $class($container);
111
            };
112
        }
113
    }
114
115
    protected function registerHandlers()
116
    {
117
        $this->readConfigFile('handlers.php', ['container' => $this->getContainer()]);
118
    }
119
120
    private function readConfigFile(string $filename, array $params = [])
121
    {
122
        foreach ($params as $name => $value) {
123
            if ($name) {
124
                ${$name} = $value;
125
            }
126
        }
127
128
        return require $this->getConfigurationDir().'/'.$filename;
129
    }
130
}
131