|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Admin; |
|
4
|
|
|
|
|
5
|
|
|
// Dependencies from PSR-7 (HTTP Messaging) |
|
6
|
|
|
use \Psr\Http\Message\RequestInterface; |
|
7
|
|
|
use \Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
// Dependency from Pimple |
|
10
|
|
|
use \Pimple\Container; |
|
11
|
|
|
|
|
12
|
|
|
// Dependency from 'charcoal-app' |
|
13
|
|
|
use \Charcoal\App\Handler\HandlerInterface; |
|
14
|
|
|
use \Charcoal\App\Module\AbstractModule; |
|
15
|
|
|
|
|
16
|
|
|
// Intra-module ('charcoal-admin') dependencies |
|
17
|
|
|
use \Charcoal\Admin\Config as AdminConfig; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Charcoal Administration Module |
|
21
|
|
|
*/ |
|
22
|
|
|
class AdminModule extends AbstractModule |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Charcoal Administration Setup. |
|
26
|
|
|
* |
|
27
|
|
|
* This module is bound to the `/admin` URL. |
|
28
|
|
|
* |
|
29
|
|
|
* ## Provides |
|
30
|
|
|
* - `charcoal/admin/module` An instance of this module |
|
31
|
|
|
* - Exact type: `\Charcoal\Admin\AdminModule` |
|
32
|
|
|
* - which implements `\Charcoal\Module\ModuleInterface` |
|
33
|
|
|
* - `charcoal/admin/config` |
|
34
|
|
|
* - ` |
|
35
|
|
|
* |
|
36
|
|
|
* ## Dependencies |
|
37
|
|
|
* - `charcoal/config` Provided by \Charcoal\CharcoalModule |
|
38
|
|
|
* |
|
39
|
|
|
* @return AdminModule |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setup() |
|
42
|
|
|
{ |
|
43
|
|
|
// A session is necessary for the admin module |
|
44
|
|
|
if (session_id() === '') { |
|
45
|
|
|
session_start(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$container = $this->app()->getContainer(); |
|
49
|
|
|
|
|
50
|
|
|
$module = $this; |
|
51
|
|
|
$container['charcoal/admin/module'] = function ($c) use ($module) { |
|
|
|
|
|
|
52
|
|
|
return $module; |
|
53
|
|
|
}; |
|
54
|
|
|
|
|
55
|
|
|
$container['charcoal/admin/config'] = function ($c) { |
|
56
|
|
|
$config = new AdminConfig(); |
|
57
|
|
|
|
|
58
|
|
|
if ($c['config']->has('admin')) { |
|
59
|
|
|
$config->merge($c['config']->get('admin')); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $config; |
|
63
|
|
|
}; |
|
64
|
|
|
|
|
65
|
|
|
$config = $container['charcoal/admin/config']; |
|
66
|
|
|
|
|
67
|
|
|
$this->setConfig($config); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$groupIdent = '/'.trim($config['base_path'], '/'); |
|
70
|
|
|
|
|
71
|
|
|
// Add the route group |
|
72
|
|
|
$this->app()->group($groupIdent, 'charcoal/admin/module:setupRoutes') |
|
73
|
|
|
->add('charcoal/admin/module:setupHandlers'); |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set up the module's handlers, via group middleware. |
|
80
|
|
|
* |
|
81
|
|
|
* @param RequestInterface $request A PSR7 request object. |
|
82
|
|
|
* @param ResponseInterface $response A PSR7 response object. |
|
83
|
|
|
* @param callable $next The next callable middleware. |
|
84
|
|
|
* @return ResponseInterface A PSR7 response object. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setupHandlers(RequestInterface $request, ResponseInterface $response, callable $next) |
|
87
|
|
|
{ |
|
88
|
|
|
$config = $this->config(); |
|
89
|
|
|
$container = $this->app()->getContainer(); |
|
90
|
|
|
$baseUrl = $container['request']->getUri()->getBaseUrl(); |
|
91
|
|
|
$adminUrl = rtrim($baseUrl, '/\\').'/'.trim($config['base_path'], '/\\'); |
|
92
|
|
|
|
|
93
|
|
|
if (isset($config['handlers'])) { |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* HTTP 404 (Not Found) handler. |
|
97
|
|
|
* |
|
98
|
|
|
* @param object|HandlerInterface $handler An error handler instance. |
|
99
|
|
|
* @param Container $container A container instance. |
|
100
|
|
|
* @return HandlerInterface |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
$container->extend('notFoundHandler', function ($handler, Container $container) use ($config, $adminUrl) { |
|
|
|
|
|
|
103
|
|
|
if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) { |
|
104
|
|
|
$handler->config()->merge($config['handlers.defaults']); |
|
105
|
|
|
$handler->setBaseUrl($adminUrl); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $handler; |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* HTTP 405 (Not Allowed) handler. |
|
113
|
|
|
* |
|
114
|
|
|
* @param object|HandlerInterface $handler An error handler instance. |
|
115
|
|
|
* @param Container $container A container instance. |
|
116
|
|
|
* @return HandlerInterface |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
$container->extend('notAllowedHandler', function ($handler, Container $container) use ($config, $adminUrl) { |
|
|
|
|
|
|
119
|
|
|
if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) { |
|
120
|
|
|
$handler->config()->merge($config['handlers.defaults']); |
|
121
|
|
|
$handler->setBaseUrl($adminUrl); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $handler; |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* HTTP 500 (Error) handler for PHP 7+ Throwables. |
|
129
|
|
|
* |
|
130
|
|
|
* @param object|HandlerInterface $handler An error handler instance. |
|
131
|
|
|
* @param Container $container A container instance. |
|
132
|
|
|
* @return HandlerInterface |
|
133
|
|
|
*/ |
|
134
|
|
View Code Duplication |
$container->extend('phpErrorHandler', function ($handler, Container $container) use ($config, $adminUrl) { |
|
|
|
|
|
|
135
|
|
|
if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) { |
|
136
|
|
|
$handler->config()->merge($config['handlers.defaults']); |
|
137
|
|
|
$handler->setBaseUrl($adminUrl); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $handler; |
|
141
|
|
|
}); |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* HTTP 500 (Error) handler. |
|
145
|
|
|
* |
|
146
|
|
|
* @param object|HandlerInterface $handler An error handler instance. |
|
147
|
|
|
* @param Container $container A container instance. |
|
148
|
|
|
* @return HandlerInterface |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
$container->extend('errorHandler', function ($handler, Container $container) use ($config, $adminUrl) { |
|
|
|
|
|
|
151
|
|
|
if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) { |
|
152
|
|
|
$handler->config()->merge($config['handlers.defaults']); |
|
153
|
|
|
$handler->setBaseUrl($adminUrl); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $handler; |
|
157
|
|
|
}); |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* HTTP 503 (Service Unavailable) handler. |
|
161
|
|
|
* |
|
162
|
|
|
* This handler is not part of Slim. |
|
163
|
|
|
* |
|
164
|
|
|
* @param object|HandlerInterface $handler An error handler instance. |
|
165
|
|
|
* @param Container $container A container instance. |
|
166
|
|
|
* @return HandlerInterface |
|
167
|
|
|
*/ |
|
168
|
|
View Code Duplication |
$container->extend('shutdownHandler', function ($handler, Container $container) use ($config, $adminUrl) { |
|
|
|
|
|
|
169
|
|
|
if ($handler instanceof HandlerInterface && isset($config['handlers.defaults'])) { |
|
170
|
|
|
$handler->config()->merge($config['handlers.defaults']); |
|
171
|
|
|
$handler->setBaseUrl($adminUrl); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $handler; |
|
175
|
|
|
}); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $next($request, $response); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.