1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Awurth\Slim\Helper\Exception\AccessDeniedException; |
4
|
|
|
use Slim\Handlers\NotAllowed; |
5
|
|
|
use Slim\Handlers\NotFound; |
6
|
|
|
use Slim\Handlers\PhpError; |
7
|
|
|
use Slim\Handlers\Strategies\RequestResponseArgs; |
8
|
|
|
use Slim\Http\Request; |
9
|
|
|
use Slim\Http\Response; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Controller functions signature must be like: |
13
|
|
|
* |
14
|
|
|
* public function controllerAction($request, $response, $arg1, $arg2, $arg3, ...) |
15
|
|
|
* |
16
|
|
|
* https://www.slimframework.com/docs/objects/router.html#route-strategies |
17
|
|
|
*/ |
18
|
|
|
$container['foundHandler'] = function ($container) { |
19
|
|
|
/** @var Request $request */ |
20
|
|
|
$request = $container['request']; |
21
|
|
|
$container['monolog']->info(sprintf('Matched route "%s /%s"', $request->getMethod(), ltrim($request->getUri()->getPath(), '/'))); |
22
|
|
|
|
23
|
|
|
return new RequestResponseArgs(); |
24
|
|
|
}; |
25
|
|
|
|
26
|
|
View Code Duplication |
$container['csrfFailureHandler'] = function ($container) { |
|
|
|
|
27
|
|
|
return function (Request $request, Response $response) use ($container) { |
28
|
|
|
$container['monolog']->error(sprintf('Failed CSRF check on "%s /%s"', $request->getMethod(), ltrim($request->getUri()->getPath(), '/'))); |
29
|
|
|
|
30
|
|
|
$container['flash']->addMessage('error', 'Failed CSRF check'); |
31
|
|
|
|
32
|
|
|
if ('prod' === $this->getEnvironment()) { |
|
|
|
|
33
|
|
|
return $response->withRedirect($request->getUri()->getPath()); |
34
|
|
|
} else { |
35
|
|
|
return $response->write('Failed CSRF check!'); |
36
|
|
|
} |
37
|
|
|
}; |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
View Code Duplication |
$container['notFoundHandler'] = function ($container) { |
|
|
|
|
41
|
|
|
return function (Request $request, Response $response) use ($container) { |
42
|
|
|
$container['monolog']->error(sprintf('No route found for "%s /%s"', $request->getMethod(), ltrim($request->getUri()->getPath(), '/'))); |
43
|
|
|
|
44
|
|
|
if ('prod' === $this->getEnvironment()) { |
|
|
|
|
45
|
|
|
return $response->withStatus(404)->write($container['twig']->fetch('error/404.twig')); |
46
|
|
|
} else { |
47
|
|
|
return (new NotFound())($request, $response); |
48
|
|
|
} |
49
|
|
|
}; |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
View Code Duplication |
$container['notAllowedHandler'] = function ($container) { |
|
|
|
|
53
|
|
|
return function (Request $request, Response $response, array $methods) use ($container) { |
54
|
|
|
$container['monolog']->error(sprintf( |
55
|
|
|
'No route found for "%s /%s": Method not allowed (Allow: %s)', |
56
|
|
|
$request->getMethod(), |
57
|
|
|
ltrim($request->getUri()->getPath(), '/'), |
58
|
|
|
implode(', ', $methods) |
59
|
|
|
)); |
60
|
|
|
|
61
|
|
|
if ('prod' === $this->getEnvironment()) { |
|
|
|
|
62
|
|
|
return $response->withStatus(405)->write($container['twig']->fetch('error/4xx.twig')); |
63
|
|
|
} else { |
64
|
|
|
return (new NotAllowed())($request, $response, $methods); |
65
|
|
|
} |
66
|
|
|
}; |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
$container['accessDeniedHandler'] = function ($container) { |
70
|
|
|
return function (Request $request, Response $response, AccessDeniedException $exception) use ($container) { |
71
|
|
|
$container['monolog']->debug('Access denied, the user does not have access to this section', [ |
72
|
|
|
'exception' => $exception |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
return $response->withStatus(403)->write($container['twig']->fetch('error/403.twig')); |
76
|
|
|
}; |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
$container['errorHandler'] = function ($container) { |
80
|
|
|
return function (Request $request, Response $response, Exception $exception) use ($container) { |
81
|
|
|
if ($exception instanceof AccessDeniedException) { |
82
|
|
|
return $container['accessDeniedHandler']($request, $response, $exception); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$container['monolog']->error('Uncaught PHP Exception '.get_class($exception), [ |
86
|
|
|
'exception' => $exception |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
if ('prod' === $this->getEnvironment()) { |
|
|
|
|
90
|
|
|
return $response->withStatus(500)->write($container['twig']->fetch('error/500.twig')); |
91
|
|
|
} else { |
92
|
|
|
return (new Slim\Handlers\Error(true))($request, $response, $exception); |
93
|
|
|
} |
94
|
|
|
}; |
95
|
|
|
}; |
96
|
|
|
|
97
|
|
|
$container['phpErrorHandler'] = function ($container) { |
98
|
|
|
return function (Request $request, Response $response, Throwable $error) use ($container) { |
99
|
|
|
$container['monolog']->critical('Uncaught PHP Exception '.get_class($error), [ |
100
|
|
|
'exception' => $error |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
if ('prod' === $this->getEnvironment()) { |
|
|
|
|
104
|
|
|
return $response->withStatus(500)->write($container['twig']->fetch('error/500.twig')); |
105
|
|
|
} else { |
106
|
|
|
return (new PhpError(true))($request, $response, $error); |
107
|
|
|
} |
108
|
|
|
}; |
109
|
|
|
}; |
110
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: