1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\entities\routing\handlers; |
4
|
|
|
|
5
|
|
|
use DomainException; |
6
|
|
|
use EE_Dependency_Map; |
7
|
|
|
use EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface; |
8
|
|
|
use EventEspresso\core\services\loaders\LoaderInterface; |
9
|
|
|
use EventEspresso\core\services\request\RequestInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Route |
13
|
|
|
* - class for detecting and matching with incoming requests |
14
|
|
|
* (this can be done by directly examining the incoming Request |
15
|
|
|
* or via a Route Match Specification class for better SRP and sharing) |
16
|
|
|
* - registers dependencies for any classes that are required from that point forwards in the request |
17
|
|
|
* - loads additional classes for handling the request |
18
|
|
|
* |
19
|
|
|
* @package EventEspresso\core\services\routing |
20
|
|
|
* @author Brent Christensen |
21
|
|
|
* @since $VID:$ |
22
|
|
|
*/ |
23
|
|
|
abstract class Route implements RouteInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
// /** will most likely need this for routing so pencilled it in for now |
27
|
|
|
// * @type CommandBusInterface $command_bus |
28
|
|
|
// */ |
29
|
|
|
// protected $command_bus; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EE_Dependency_Map $dependency_map |
33
|
|
|
*/ |
34
|
|
|
protected $dependency_map; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoaderInterface $loader |
38
|
|
|
*/ |
39
|
|
|
protected $loader; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var RequestInterface $request |
43
|
|
|
*/ |
44
|
|
|
protected $request; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var RouteMatchSpecificationInterface $specification |
48
|
|
|
*/ |
49
|
|
|
private $specification; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var boolean $handled |
53
|
|
|
*/ |
54
|
|
|
private $handled = false; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Route constructor. |
59
|
|
|
* |
60
|
|
|
* @param EE_Dependency_Map $dependency_map |
61
|
|
|
* @param LoaderInterface $loader |
62
|
|
|
* @param RequestInterface $request |
63
|
|
|
* @param RouteMatchSpecificationInterface $specification |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
EE_Dependency_Map $dependency_map, |
67
|
|
|
LoaderInterface $loader, |
68
|
|
|
RequestInterface $request, |
69
|
|
|
RouteMatchSpecificationInterface $specification = null |
70
|
|
|
) { |
71
|
|
|
$this->dependency_map = $dependency_map; |
72
|
|
|
$this->loader = $loader; |
73
|
|
|
$this->request = $request; |
74
|
|
|
$this->specification = $specification; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @since $VID:$ |
80
|
|
|
*/ |
81
|
|
|
abstract protected function registerDependencies(); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* implements logic required to run during request |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
* @since $VID:$ |
89
|
|
|
*/ |
90
|
|
|
abstract protected function requestHandler(); |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* returns true if the current request matches this route |
95
|
|
|
* child classes can override and use Request directly to match route with request |
96
|
|
|
* or supply a RouteMatchSpecification class and just use the below |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
* @since $VID:$ |
100
|
|
|
*/ |
101
|
|
|
public function matchesCurrentRequest() |
102
|
|
|
{ |
103
|
|
|
return $this->specification instanceof RouteMatchSpecificationInterface |
104
|
|
|
? $this->specification->isMatchingRoute() |
105
|
|
|
: false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
final public function isHandled() |
113
|
|
|
{ |
114
|
|
|
return $this->handled; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param bool $handled |
120
|
|
|
*/ |
121
|
|
|
private function setHandled($handled) |
122
|
|
|
{ |
123
|
|
|
$this->handled = filter_var($handled, FILTER_VALIDATE_BOOLEAN); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* runs route requestHandler() if |
129
|
|
|
* - route has not previously been handled |
130
|
|
|
* - route specification matches for current request |
131
|
|
|
* sets route handled property based on results returned by requestHandler() |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
* @throws DomainException |
135
|
|
|
* @since $VID:$ |
136
|
|
|
*/ |
137
|
|
|
final public function handleRequest() |
138
|
|
|
{ |
139
|
|
|
if (! $this->isHandled() && $this->matchesCurrentRequest()) { |
140
|
|
|
do_action('AHEE__EventEspresso_core_domain_entities_routes_handlers_Route__handleRequest', $this); |
141
|
|
|
$this->registerDependencies(); |
142
|
|
|
$handled = $this->requestHandler(); |
143
|
|
|
if (! is_bool($handled)) { |
144
|
|
|
throw new DomainException( |
145
|
|
|
esc_html__( |
146
|
|
|
'Route::requestHandler() must return a boolean to indicate whether the request has been handled or not.', |
147
|
|
|
'event_espresso' |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
$this->setHandled($handled); |
152
|
|
|
} |
153
|
|
|
return $this->handled; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|