-
Notifications
You must be signed in to change notification settings - Fork 26
/
handlers.php
109 lines (92 loc) · 4.13 KB
/
handlers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
use Awurth\Slim\Helper\Exception\AccessDeniedException;
use Slim\Handlers\NotAllowed;
use Slim\Handlers\NotFound;
use Slim\Handlers\PhpError;
use Slim\Handlers\Strategies\RequestResponseArgs;
use Slim\Http\Request;
use Slim\Http\Response;
/**
* Controller functions signature must be like:
*
* public function controllerAction($request, $response, $arg1, $arg2, $arg3, ...)
*
* https://www.slimframework.com/docs/objects/router.html#route-strategies
*/
$container['foundHandler'] = function ($container) {
/** @var Request $request */
$request = $container['request'];
$container['monolog']->info(sprintf('Matched route "%s /%s"', $request->getMethod(), ltrim($request->getUri()->getPath(), '/')));
return new RequestResponseArgs();
};
$container['csrfFailureHandler'] = function ($container) {
return function (Request $request, Response $response) use ($container) {
$container['monolog']->error(sprintf('Failed CSRF check on "%s /%s"', $request->getMethod(), ltrim($request->getUri()->getPath(), '/')));
$container['flash']->addMessage('error', 'Failed CSRF check');
if ('prod' === $this->getEnvironment()) {
return $response->withRedirect($request->getUri()->getPath());
} else {
return $response->write('Failed CSRF check!');
}
};
};
$container['notFoundHandler'] = function ($container) {
return function (Request $request, Response $response) use ($container) {
$container['monolog']->error(sprintf('No route found for "%s /%s"', $request->getMethod(), ltrim($request->getUri()->getPath(), '/')));
if ('prod' === $this->getEnvironment()) {
return $response->withStatus(404)->write($container['twig']->fetch('error/404.twig'));
} else {
return (new NotFound())($request, $response);
}
};
};
$container['notAllowedHandler'] = function ($container) {
return function (Request $request, Response $response, array $methods) use ($container) {
$container['monolog']->error(sprintf(
'No route found for "%s /%s": Method not allowed (Allow: %s)',
$request->getMethod(),
ltrim($request->getUri()->getPath(), '/'),
implode(', ', $methods)
));
if ('prod' === $this->getEnvironment()) {
return $response->withStatus(405)->write($container['twig']->fetch('error/4xx.twig'));
} else {
return (new NotAllowed())($request, $response, $methods);
}
};
};
$container['accessDeniedHandler'] = function ($container) {
return function (Request $request, Response $response, AccessDeniedException $exception) use ($container) {
$container['monolog']->debug('Access denied, the user does not have access to this section', [
'exception' => $exception
]);
return $response->withStatus(403)->write($container['twig']->fetch('error/403.twig'));
};
};
$container['errorHandler'] = function ($container) {
return function (Request $request, Response $response, Exception $exception) use ($container) {
if ($exception instanceof AccessDeniedException) {
return $container['accessDeniedHandler']($request, $response, $exception);
}
$container['monolog']->error('Uncaught PHP Exception '.get_class($exception), [
'exception' => $exception
]);
if ('prod' === $this->getEnvironment()) {
return $response->withStatus(500)->write($container['twig']->fetch('error/500.twig'));
} else {
return (new Slim\Handlers\Error(true))($request, $response, $exception);
}
};
};
$container['phpErrorHandler'] = function ($container) {
return function (Request $request, Response $response, Throwable $error) use ($container) {
$container['monolog']->critical('Uncaught PHP Exception '.get_class($error), [
'exception' => $error
]);
if ('prod' === $this->getEnvironment()) {
return $response->withStatus(500)->write($container['twig']->fetch('error/500.twig'));
} else {
return (new PhpError(true))($request, $response, $error);
}
};
};