|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use WPEmerge\Facades\RouteCondition; |
|
8
|
|
|
use WPEmerge\Middleware\HasMiddlewareTrait; |
|
9
|
|
|
use WPEmerge\Requests\Request; |
|
10
|
|
|
use WPEmerge\Routing\Conditions\ConditionInterface; |
|
11
|
|
|
use WPEmerge\Routing\Conditions\UrlCondition; |
|
12
|
|
|
use WPEmerge\Routing\Conditions\MultipleCondition; |
|
13
|
|
|
|
|
14
|
|
|
class RouteGroup implements RouteInterface, HasRoutesInterface { |
|
15
|
|
|
use HasRoutesTrait { |
|
16
|
|
|
route as traitRoute; |
|
17
|
|
|
group as traitGroup; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
use HasMiddlewareTrait { |
|
21
|
|
|
addMiddleware as traitAddMiddleware; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Route condition |
|
26
|
|
|
* |
|
27
|
|
|
* @var ConditionInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $condition = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor |
|
33
|
|
|
* |
|
34
|
|
|
* @throws Exception |
|
35
|
|
|
* @param mixed $condition |
|
36
|
|
|
* @param Closure $closure |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( $condition, Closure $closure ) { |
|
39
|
|
|
if ( ! $condition instanceof ConditionInterface ) { |
|
40
|
|
|
$condition = RouteCondition::make( $condition ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->condition = $condition; |
|
44
|
|
|
|
|
45
|
|
|
$closure( $this ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the first child route which is satisfied |
|
50
|
|
|
* |
|
51
|
|
|
* @return RouteInterface|null |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getSatisfiedRoute( Request $request ) { |
|
54
|
|
|
$routes = $this->getRoutes(); |
|
55
|
|
|
foreach ( $routes as $route ) { |
|
56
|
|
|
if ( $route->isSatisfied( $request ) ) { |
|
57
|
|
|
return $route; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Merge 2 conditions (in supplied order). |
|
65
|
|
|
* |
|
66
|
|
|
* @param ConditionInterface|null $parent |
|
67
|
|
|
* @param ConditionInterface $child |
|
68
|
|
|
* @return ConditionInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function mergeConditions( $parent, $child ) { |
|
71
|
|
|
if ( $parent === null ) { |
|
72
|
|
|
return $child; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ( $parent instanceof UrlCondition ) { |
|
76
|
|
|
if ( $child instanceof UrlCondition ) { |
|
77
|
|
|
return $parent->concatenate( $child ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Ignore parent if conditions are incompatible. |
|
81
|
|
|
return $child; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return new MultipleCondition( [ $parent, $child ] ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function isSatisfied( Request $request ) { |
|
91
|
|
|
$route = $this->getSatisfiedRoute( $request ); |
|
92
|
|
|
return $route !== null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritDoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function handle( Request $request, $view ) { |
|
99
|
|
|
$route = $this->getSatisfiedRoute( $request ); |
|
100
|
|
|
return $route ? $route->handle( $request, $view ) : null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function route( $methods, $condition, $handler ) { |
|
|
|
|
|
|
107
|
|
|
if ( ! $condition instanceof ConditionInterface ) { |
|
108
|
|
|
$condition = RouteCondition::make( $condition ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$condition = $this->mergeConditions( $this->condition, $condition ); |
|
112
|
|
|
|
|
113
|
|
|
return $this->traitRoute( $methods, $condition, $handler ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritDoc} |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function group( $condition, Closure $closure ) { |
|
|
|
|
|
|
120
|
|
|
if ( ! $condition instanceof ConditionInterface ) { |
|
121
|
|
|
$condition = RouteCondition::make( $condition ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$condition = $this->mergeConditions( $this->condition, $condition ); |
|
125
|
|
|
|
|
126
|
|
|
return $this->traitGroup( $condition, $closure ); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritDoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function addMiddleware( $middleware ) { |
|
133
|
|
|
$routes = $this->getRoutes(); |
|
134
|
|
|
|
|
135
|
|
|
foreach ( $routes as $route ) { |
|
136
|
|
|
$route->addMiddleware( $middleware ); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->traitAddMiddleware( $middleware ); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.