1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\routing; |
4
|
|
|
|
5
|
|
|
use DomainException; |
6
|
|
|
use EE_Dependency_Map; |
7
|
|
|
use EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface; |
8
|
|
|
use EventEspresso\core\services\assets\AssetManagerInterface; |
9
|
|
|
use EventEspresso\core\services\json\JsonDataNode; |
10
|
|
|
use EventEspresso\core\services\loaders\LoaderInterface; |
11
|
|
|
use EventEspresso\core\services\request\RequestInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Route |
15
|
|
|
* - class for detecting and matching with incoming requests |
16
|
|
|
* (this can be done by directly examining the incoming Request |
17
|
|
|
* or via a Route Match Specification class for better SRP and sharing) |
18
|
|
|
* - registers dependencies for any classes that are required from that point forwards in the request |
19
|
|
|
* - loads additional classes for handling the request |
20
|
|
|
* |
21
|
|
|
* @package EventEspresso\core\services\routing |
22
|
|
|
* @author Brent Christensen |
23
|
|
|
* @since $VID:$ |
24
|
|
|
*/ |
25
|
|
|
abstract class Route implements RouteInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var AssetManagerInterface $asset_manager |
30
|
|
|
*/ |
31
|
|
|
protected $asset_manager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var EE_Dependency_Map $dependency_map |
35
|
|
|
*/ |
36
|
|
|
protected $dependency_map; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var JsonDataNode $data_node |
40
|
|
|
*/ |
41
|
|
|
protected $data_node; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var LoaderInterface $loader |
45
|
|
|
*/ |
46
|
|
|
protected $loader; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var RequestInterface $request |
50
|
|
|
*/ |
51
|
|
|
protected $request; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var RouteMatchSpecificationInterface $specification |
55
|
|
|
*/ |
56
|
|
|
protected $specification; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var boolean $handled |
60
|
|
|
*/ |
61
|
|
|
private $handled = false; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Route constructor. |
66
|
|
|
* |
67
|
|
|
* @param EE_Dependency_Map $dependency_map |
68
|
|
|
* @param LoaderInterface $loader |
69
|
|
|
* @param RequestInterface $request |
70
|
|
|
* @param JsonDataNode $data_node |
71
|
|
|
* @param RouteMatchSpecificationInterface $specification |
72
|
|
|
*/ |
73
|
|
|
public function __construct( |
74
|
|
|
EE_Dependency_Map $dependency_map, |
75
|
|
|
LoaderInterface $loader, |
76
|
|
|
RequestInterface $request, |
77
|
|
|
JsonDataNode $data_node = null, |
78
|
|
|
RouteMatchSpecificationInterface $specification = null |
79
|
|
|
) { |
80
|
|
|
$this->dependency_map = $dependency_map; |
81
|
|
|
$this->data_node = $data_node; |
82
|
|
|
$this->loader = $loader; |
83
|
|
|
$this->request = $request; |
84
|
|
|
$this->specification = $specification; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @since $VID:$ |
90
|
|
|
*/ |
91
|
|
|
abstract protected function registerDependencies(); |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* implements logic required to run during request |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
* @since $VID:$ |
99
|
|
|
*/ |
100
|
|
|
abstract protected function requestHandler(); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param JsonDataNode $data_node |
105
|
|
|
*/ |
106
|
|
|
protected function setDataNode($data_node) |
107
|
|
|
{ |
108
|
|
|
$this->data_node = $data_node; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param RouteMatchSpecificationInterface $specification |
114
|
|
|
*/ |
115
|
|
|
protected function setSpecification($specification) |
116
|
|
|
{ |
117
|
|
|
$this->specification = $specification; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return JsonDataNode |
123
|
|
|
*/ |
124
|
|
|
public function dataNode() |
125
|
|
|
{ |
126
|
|
|
return $this->data_node; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* runs route requestHandler() if |
132
|
|
|
* - route has not previously been handled |
133
|
|
|
* - route specification matches for current request |
134
|
|
|
* sets route handled property based on results returned by requestHandler() |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
* @since $VID:$ |
138
|
|
|
*/ |
139
|
|
|
public function handleRequest() |
140
|
|
|
{ |
141
|
|
|
if ($this->isNotHandled()) { |
142
|
|
|
$this->initialize(); |
143
|
|
|
if ($this->matchesCurrentRequest()) { |
144
|
|
|
do_action('AHEE__EventEspresso_core_domain_entities_routes_handlers_Route__handleRequest', $this); |
145
|
|
|
$this->registerDependencies(); |
146
|
|
|
$handled = $this->requestHandler(); |
147
|
|
|
if (! is_bool($handled)) { |
148
|
|
|
throw new DomainException( |
149
|
|
|
esc_html__( |
150
|
|
|
'Route::requestHandler() must return a boolean to indicate whether the request has been handled or not.', |
151
|
|
|
'event_espresso' |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
$this->handled = filter_var($handled, FILTER_VALIDATE_BOOLEAN); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
return $this->handled; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* called just before matchesCurrentRequest() |
164
|
|
|
* and allows Route to perform any setup required such as calling setSpecification() |
165
|
|
|
* |
166
|
|
|
* @since $VID:$ |
167
|
|
|
*/ |
168
|
|
|
public function initialize() |
169
|
|
|
{ |
170
|
|
|
// do nothing by default |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
final public function isHandled() |
178
|
|
|
{ |
179
|
|
|
return $this->handled; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
final public function isNotHandled() |
187
|
|
|
{ |
188
|
|
|
return ! $this->handled; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* returns true if the current request matches this route |
194
|
|
|
* child classes can override and use Request directly to match route with request |
195
|
|
|
* or supply a RouteMatchSpecification class and just use the below |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
* @since $VID:$ |
199
|
|
|
*/ |
200
|
|
|
public function matchesCurrentRequest() |
201
|
|
|
{ |
202
|
|
|
return $this->specification instanceof RouteMatchSpecificationInterface |
203
|
|
|
? $this->specification->isMatchingRoute() |
204
|
|
|
: false; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|