|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Router; |
|
4
|
|
|
|
|
5
|
|
|
trait ExtraJobsTrait{ |
|
6
|
|
|
|
|
7
|
|
|
public function before($walking): Router |
|
8
|
|
|
{ |
|
9
|
|
|
return $this->setOnRoute($walking,'before'); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
public static function beforeAll($walking, $except = null): Router |
|
13
|
|
|
{ |
|
14
|
|
|
$excepts = is_array($except) ? $except : [$except]; |
|
15
|
|
|
self::getInstance()->beforeExcepts = $excepts; |
|
16
|
|
|
self::getInstance()->beforeAll = $walking; |
|
17
|
|
|
return self::getInstance()->setOnRoutes($walking,'beforeAll',$excepts); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function after($walking): Router |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->setOnRoute($walking,'after'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function afterAll($walking, $except = null): Router |
|
26
|
|
|
{ |
|
27
|
|
|
$excepts = is_array($except) ? $except : [$except]; |
|
28
|
|
|
self::getInstance()->afterExcepts = $excepts; |
|
29
|
|
|
self::getInstance()->afterAll = $walking; |
|
30
|
|
|
return self::getInstance()->setOnRoutes($walking,'afterAll',$excepts); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function setOnRoute($walking, string $state): Router |
|
34
|
|
|
{ |
|
35
|
|
|
if($this->lastReturn !== null){ |
|
36
|
|
|
$currentGroup = end($this->routers)['group']; |
|
37
|
|
|
|
|
38
|
|
|
foreach ($this->routers as $key => $value) { |
|
39
|
|
|
|
|
40
|
|
|
if($value['group'] === $currentGroup){ |
|
41
|
|
|
$this->routers[$key][$state] = $walking; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
return $this; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->routers[count($this->routers)-1][$state] = $walking; |
|
49
|
|
|
return $this; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function setOnRoutes($walking, string $state, array $excepts): Router |
|
53
|
|
|
{ |
|
54
|
|
|
foreach($this->routers as $r => $route){ |
|
55
|
|
|
if(!in_array($this->routers[$r]['name'],$excepts)){ |
|
56
|
|
|
$this->routers[$r][$state] = $walking; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
return $this; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|