|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
|
6
|
|
|
use Slim\App; |
|
7
|
|
|
|
|
8
|
|
|
class Application extends App |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $environment; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $rootDir; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $environment |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($environment) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->environment = $environment; |
|
28
|
|
|
$this->rootDir = $this->getRootDir(); |
|
29
|
|
|
|
|
30
|
|
|
parent::__construct($this->loadConfiguration()); |
|
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
|
|
|
$container = $this->getContainer(); |
|
70
|
|
|
require $this->getConfigurationDir().'/container.php'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function loadConfiguration() |
|
74
|
|
|
{ |
|
75
|
|
|
$app = $this; |
|
76
|
|
|
$configuration = [ |
|
77
|
|
|
'settings' => require $this->getConfigurationDir().'/slim.php' |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
if (file_exists($this->getConfigurationDir().'/services.'.$this->getEnvironment().'.php')) { |
|
81
|
|
|
$configuration['settings'] += require $this->getConfigurationDir().'/services.'.$this->getEnvironment().'.php'; |
|
82
|
|
|
} else { |
|
83
|
|
|
$configuration['settings'] += require $this->getConfigurationDir().'/services.php'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $configuration; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function loadMiddleware() |
|
90
|
|
|
{ |
|
91
|
|
|
$app = $this; |
|
92
|
|
|
$container = $this->getContainer(); |
|
93
|
|
|
require $this->getConfigurationDir().'/middleware.php'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function loadRoutes() |
|
97
|
|
|
{ |
|
98
|
|
|
$app = $this; |
|
99
|
|
|
$container = $this->getContainer(); |
|
100
|
|
|
require $this->getConfigurationDir().'/routes.php'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function registerControllers() |
|
104
|
|
|
{ |
|
105
|
|
|
$container = $this->getContainer(); |
|
106
|
|
|
if (file_exists($this->getConfigurationDir().'/controllers.php')) { |
|
107
|
|
|
$controllers = require $this->getConfigurationDir().'/controllers.php'; |
|
108
|
|
|
foreach ($controllers as $key => $class) { |
|
109
|
|
|
$container[$key] = function ($container) use ($class) { |
|
110
|
|
|
return new $class($container); |
|
111
|
|
|
}; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function registerHandlers() |
|
117
|
|
|
{ |
|
118
|
|
|
$container = $this->getContainer(); |
|
119
|
|
|
require $this->getConfigurationDir().'/handlers.php'; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|