Application::loadRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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