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
|
|
|
$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
|
|
|
$handlers = require $this->getConfigurationDir().'/handlers.php'; |
120
|
|
|
foreach ($handlers as $name => $callable) { |
121
|
|
|
$container[$name] = $callable; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|