|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan\Plugins\WatchingStrategies\Routes; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Http\RedirectResponse; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
|
|
10
|
|
|
class MatchedRoute |
|
11
|
|
|
{ |
|
12
|
|
|
public static $chainData; |
|
13
|
|
|
|
|
14
|
60 |
|
public function handle($request, Closure $next) |
|
15
|
|
|
{ |
|
16
|
60 |
|
$this->writeDebugInfo(); |
|
17
|
|
|
|
|
18
|
60 |
|
$route = $request->route(); |
|
19
|
60 |
|
$matchedRoute = $this->getMatchedRouteInto($route); |
|
20
|
60 |
|
$matchedCallbacks = []; |
|
21
|
60 |
|
foreach ($matchedRoute as $info) { |
|
22
|
60 |
|
foreach (static::$chainData as $routeInfo => $callBacks) { |
|
23
|
58 |
|
if (Str::is($routeInfo, $info)) { |
|
24
|
60 |
|
$matchedCallbacks[] = array_pop($callBacks); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
60 |
|
$this->exec($matchedCallbacks, $route); |
|
30
|
|
|
|
|
31
|
29 |
|
return $next($request); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
60 |
|
private function exec(array $closures, $route) |
|
35
|
|
|
{ |
|
36
|
60 |
|
foreach (Arr::flatten($closures) as $closure) { |
|
37
|
47 |
|
$closure($route); |
|
38
|
|
|
} |
|
39
|
29 |
|
} |
|
40
|
|
|
|
|
41
|
60 |
|
private function getMatchedRouteInto($route) |
|
42
|
|
|
{ |
|
43
|
|
|
$matchedRoute = [ |
|
44
|
60 |
|
$route->getName(), |
|
45
|
60 |
|
$route->getActionName(), |
|
46
|
60 |
|
app('request')->method().$route->uri, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
60 |
|
return array_filter($matchedRoute); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
59 |
|
public function terminate($request, $response) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
59 |
|
if (! ($debug = app()['heyman_reaction_is_happened_in_debug'] ?? false)) { |
|
55
|
32 |
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
27 |
|
$this->writeForDebugbar($debug); |
|
59
|
|
|
|
|
60
|
27 |
|
if (is_a($response, RedirectResponse::class)) { |
|
61
|
4 |
|
$s = session(); |
|
62
|
4 |
|
$s->flash('heyman.debug.info', $debug); |
|
63
|
4 |
|
$s->reflash(); |
|
64
|
4 |
|
$s->save(); |
|
65
|
|
|
} |
|
66
|
27 |
|
} |
|
67
|
|
|
|
|
68
|
60 |
|
private function writeDebugInfo() |
|
69
|
|
|
{ |
|
70
|
60 |
|
if ($debug = session()->get('heyman.debug.info')) { |
|
71
|
|
|
$this->writeForDebugbar($debug); |
|
72
|
|
|
} |
|
73
|
60 |
|
} |
|
74
|
|
|
|
|
75
|
27 |
|
private function writeForDebugbar($debug) |
|
76
|
|
|
{ |
|
77
|
27 |
|
resolve('heyman.debugger')->addMessage('HeyMan Rule Matched in file: '); |
|
78
|
27 |
|
resolve('heyman.debugger')->addMessage($debug[0].' on line: '.$debug[1]); |
|
79
|
27 |
|
resolve('heyman.debugger')->addMessage($debug[2]); |
|
80
|
27 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.