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