1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan\Hooks; |
4
|
|
|
|
5
|
|
|
use Imanghafoori\HeyMan\WatchingStrategies\RouterEventManager; |
6
|
|
|
use Imanghafoori\HeyMan\YouShouldHave; |
7
|
|
|
|
8
|
|
|
trait RouteHooks |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param mixed ...$url |
12
|
|
|
* |
13
|
|
|
* @return YouShouldHave |
14
|
|
|
*/ |
15
|
35 |
|
public function whenYouVisitUrl(...$url): YouShouldHave |
16
|
|
|
{ |
17
|
35 |
|
return $this->authorizeURL($url, 'GET'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param mixed ...$url |
22
|
|
|
* |
23
|
|
|
* @return YouShouldHave |
24
|
|
|
*/ |
25
|
1 |
|
public function whenYouSendPost(...$url): YouShouldHave |
26
|
|
|
{ |
27
|
1 |
|
return $this->authorizeURL($url, 'POST'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed ...$url |
32
|
|
|
* |
33
|
|
|
* @return YouShouldHave |
34
|
|
|
*/ |
35
|
1 |
|
public function whenYouSendPatch(...$url): YouShouldHave |
36
|
|
|
{ |
37
|
1 |
|
return $this->authorizeURL($url, 'PATCH'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed ...$url |
42
|
|
|
* |
43
|
|
|
* @return YouShouldHave |
44
|
|
|
*/ |
45
|
2 |
|
public function whenYouSendPut(...$url): YouShouldHave |
46
|
|
|
{ |
47
|
2 |
|
return $this->authorizeURL($url, 'PUT'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed ...$url |
52
|
|
|
* |
53
|
|
|
* @return YouShouldHave |
54
|
|
|
*/ |
55
|
1 |
|
public function whenYouSendDelete(...$url): YouShouldHave |
56
|
|
|
{ |
57
|
1 |
|
return $this->authorizeURL($url, 'DELETE'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param mixed ...$routeName |
62
|
|
|
* |
63
|
|
|
* @return YouShouldHave |
64
|
|
|
*/ |
65
|
11 |
|
public function whenYouReachRoute(...$routeName): YouShouldHave |
66
|
|
|
{ |
67
|
11 |
|
return $this->authorizeRoute($this->normalizeInput($routeName)); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param mixed ...$action |
72
|
|
|
* |
73
|
|
|
* @return YouShouldHave |
74
|
|
|
*/ |
75
|
10 |
|
public function whenYouCallAction(...$action): YouShouldHave |
76
|
|
|
{ |
77
|
10 |
|
$action = $this->normalizeAction($action); |
78
|
|
|
|
79
|
10 |
|
return $this->authorizeRoute($action); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $value |
84
|
|
|
* |
85
|
|
|
* @return YouShouldHave |
86
|
|
|
*/ |
87
|
58 |
|
private function authorizeRoute($value): YouShouldHave |
88
|
|
|
{ |
89
|
58 |
|
$this->chain->eventManager = app(RouterEventManager::class)->init($value); |
90
|
|
|
|
91
|
58 |
|
return app(YouShouldHave::class); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $url |
96
|
|
|
* @param $verb |
97
|
|
|
* |
98
|
|
|
* @return YouShouldHave |
99
|
|
|
*/ |
100
|
40 |
|
private function authorizeURL($url, $verb): YouShouldHave |
101
|
|
|
{ |
102
|
40 |
|
return $this->authorizeRoute($this->normalizeUrl($url, $verb)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $url |
107
|
|
|
* @param $verb |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
40 |
|
private function normalizeUrl($url, $verb): array |
112
|
|
|
{ |
113
|
|
|
$removeSlash = function ($url) use ($verb) { |
114
|
40 |
|
return $verb.ltrim($url, '/'); |
115
|
40 |
|
}; |
116
|
|
|
|
117
|
40 |
|
return array_map($removeSlash, $this->normalizeInput($url)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $action |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
10 |
|
private function normalizeAction($action): array |
126
|
|
|
{ |
127
|
|
|
$addNamespace = function ($action) { |
128
|
10 |
|
if ($action = ltrim($action, '\\')) { |
129
|
10 |
|
return $action; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return app()->getNamespace().'\\Http\\Controllers\\'.$action; |
|
|
|
|
133
|
10 |
|
}; |
134
|
|
|
|
135
|
10 |
|
return array_map($addNamespace, $this->normalizeInput($action)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.