|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace Mezon\Router; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait InvalidRouteErrorHandler |
|
7
|
|
|
* |
|
8
|
|
|
* @package Router |
|
9
|
|
|
* @author Dodonov A.A. |
|
10
|
|
|
* @version v.1.0 (2019/08/15) |
|
11
|
|
|
* @copyright Copyright (c) 2019, http://aeon.su |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Error handler for unexisting route |
|
16
|
|
|
* |
|
17
|
|
|
* @author gdever |
|
18
|
|
|
*/ |
|
19
|
|
|
trait InvalidRouteErrorHandler |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Method wich handles invalid route error |
|
24
|
|
|
* |
|
25
|
|
|
* @var ?callable |
|
26
|
|
|
*/ |
|
27
|
|
|
private $invalidRouteErrorHandler = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Method processes no processor found error |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $route |
|
33
|
|
|
* route |
|
34
|
|
|
*/ |
|
35
|
|
|
public function noProcessorFoundErrorHandler(string $route): void |
|
36
|
|
|
{ |
|
37
|
|
|
throw (new \Exception( |
|
38
|
|
|
'The processor was not found for the route ' . $route . ' in ' . $this->getAllRoutesTrace(), |
|
39
|
|
|
- 1)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Method sets InvalidRouteErrorHandler function |
|
44
|
|
|
* |
|
45
|
|
|
* @param callable $function |
|
46
|
|
|
* error handler |
|
47
|
|
|
* |
|
48
|
|
|
* @return ?callable old error handler |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setNoProcessorFoundErrorHandler(callable $function): ?callable |
|
51
|
|
|
{ |
|
52
|
|
|
$oldErrorHandler = $this->invalidRouteErrorHandler; |
|
53
|
|
|
|
|
54
|
|
|
$this->invalidRouteErrorHandler = $function; |
|
55
|
|
|
|
|
56
|
|
|
return $oldErrorHandler; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Method returns error handler |
|
61
|
|
|
* |
|
62
|
|
|
* @return callable |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getNoProcessorErrorHandler(): callable |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->invalidRouteErrorHandler === null) { |
|
67
|
|
|
$this->invalidRouteErrorHandler = [ |
|
68
|
|
|
$this, |
|
69
|
|
|
'noProcessorFoundErrorHandler' |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->invalidRouteErrorHandler; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Method rturns all available routes |
|
78
|
|
|
* |
|
79
|
|
|
* @return string trace |
|
80
|
|
|
* @psalm-suppress PossiblyUndefinedArrayOffset |
|
81
|
|
|
*/ |
|
82
|
|
|
protected abstract function getAllRoutesTrace(): string; |
|
83
|
|
|
} |
|
84
|
|
|
|