|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HnrAzevedo\Router; |
|
6
|
|
|
|
|
7
|
|
|
trait CheckTrait |
|
8
|
|
|
{ |
|
9
|
|
|
use Helper; |
|
10
|
|
|
|
|
11
|
|
|
protected array $routesName = []; |
|
12
|
|
|
|
|
13
|
|
|
protected function hasRouteName(string $name): void |
|
14
|
|
|
{ |
|
15
|
|
|
if(!isset($this->routesName[$name])) { |
|
16
|
|
|
throw new \RuntimeException("There is no route named with {$name}"); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function isInNameGroup(): void |
|
21
|
|
|
{ |
|
22
|
|
|
if(!is_null($this->getGroup())) { |
|
23
|
|
|
throw new \RuntimeException("It is not allowed to assign names to groups"); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function isInPseudGroup(): void |
|
28
|
|
|
{ |
|
29
|
|
|
if(!is_null($this->getGroup())) { |
|
30
|
|
|
throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function existRouteName(string $name): void |
|
35
|
|
|
{ |
|
36
|
|
|
if(isset($this->routesName[$name])) { |
|
37
|
|
|
throw new \RuntimeException("There is already a route named with {$name}"); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function checkMethod(array $route, $method): void |
|
42
|
|
|
{ |
|
43
|
|
|
$hasMethod = false; |
|
44
|
|
|
foreach(explode('|', $route['method']) as $routeMethod){ |
|
45
|
|
|
if(@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
46
|
|
|
$hasMethod = true; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
if(!$hasMethod) { |
|
50
|
|
|
throw new \Exception('This route is not released for the accessed method'); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function throwCallable($value): void |
|
55
|
|
|
{ |
|
56
|
|
|
if(is_callable($value)) { |
|
57
|
|
|
throw new \Exception('Passing functions as attributes is not allowed'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function checkControllerMeth(string $controllerMeth): void |
|
62
|
|
|
{ |
|
63
|
|
|
$routeURI = str_replace(['{?','{','}'], '', urldecode(unserialize($this->current()['uri'])->getPath())); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$controller = (string) explode('@', $controllerMeth)[0]; |
|
66
|
|
|
$method = (string) explode('@', $controllerMeth)[1]; |
|
67
|
|
|
|
|
68
|
|
|
if(!class_exists($controller)) { |
|
69
|
|
|
throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if(!method_exists($controller, $method)) { |
|
73
|
|
|
throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|