Application::loadRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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