1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Minotaur |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Minotaur\Http; |
22
|
|
|
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
24
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Logs exceptions that occur further inside the layer pipeline. |
28
|
|
|
*/ |
29
|
|
|
class Reporter implements \Minotaur\Route\Plugin |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \Minotaur\ErrorLogger |
33
|
|
|
*/ |
34
|
|
|
private $errorLogger; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new error Reporter. |
38
|
|
|
* |
39
|
|
|
* @param $errorLogger - The error logger |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(\Minotaur\ErrorLogger $errorLogger) |
42
|
|
|
{ |
43
|
1 |
|
$this->errorLogger = $errorLogger; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets the plugin priority; larger means first. |
48
|
|
|
* |
49
|
|
|
* @return - The plugin priority |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
public function getPriority(): int |
52
|
|
|
{ |
53
|
|
|
return PHP_INT_MAX - 1; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Middleware request–response handling. |
58
|
|
|
* |
59
|
|
|
* Performs a typical passthru (i.e. `return $next($req, $res);`), but in |
60
|
|
|
* the event an `Exception` occurs, the `$errorLogger` is called, then the |
61
|
|
|
* `Exception` is rethrown (most likely to be caught higher in the queue). |
62
|
|
|
* |
63
|
|
|
* @param $request - The server request |
64
|
|
|
* @param $response - The response |
65
|
|
|
* @param callable $next The next layer. (function (Request,Response): Response) |
66
|
|
|
* @return - The response |
|
|
|
|
67
|
|
|
* @throws \Exception rethrows any exception that occurs after logging. |
68
|
|
|
*/ |
69
|
1 |
|
public function __invoke(Request $request, Response $response, callable $next): Response |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
1 |
|
return $next($request, $response); |
73
|
1 |
|
} catch (\Exception $e) { |
74
|
1 |
|
$this->errorLogger->log($e); |
75
|
1 |
|
throw $e; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.