|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\routing; |
|
4
|
|
|
|
|
5
|
|
|
use EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface; |
|
6
|
|
|
use EventEspresso\core\exceptions\InvalidClassException; |
|
7
|
|
|
use EventEspresso\core\services\collections\CollectionDetails; |
|
8
|
|
|
use EventEspresso\core\services\collections\CollectionDetailsException; |
|
9
|
|
|
use EventEspresso\core\services\collections\CollectionInterface; |
|
10
|
|
|
use EventEspresso\core\services\collections\CollectionLoader; |
|
11
|
|
|
use EventEspresso\core\services\collections\CollectionLoaderException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class RouteMatchSpecificationManager |
|
15
|
|
|
* Manages setup of the RouteMatchSpecificationsCollection |
|
16
|
|
|
* and compares specifications in it via the currentRequestMatches() method. |
|
17
|
|
|
* Folders containing RouteMatchSpecification classes can be added to the collection using the |
|
18
|
|
|
* FHEE__EventEspresso_core_services_route_match_RouteMatchSpecificationManager__populateSpecificationCollection__collection_FQCNs |
|
19
|
|
|
* filter prior to the AHEE__EE_System__initialize hookpoint |
|
20
|
|
|
* |
|
21
|
|
|
* |
|
22
|
|
|
* @package EventEspresso\core\services\routing |
|
23
|
|
|
* @author Brent Christensen |
|
24
|
|
|
* @since 4.9.71.p |
|
25
|
|
|
*/ |
|
26
|
|
|
class RouteMatchSpecificationManager |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var CollectionInterface[]|RouteMatchSpecificationInterface[] $specifications |
|
30
|
|
|
*/ |
|
31
|
|
|
private $specifications; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var RouteMatchSpecificationFactory $specifications_factory |
|
35
|
|
|
*/ |
|
36
|
|
|
private $specifications_factory; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* RouteMatchSpecificationManager constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param RouteMatchSpecificationCollection $specifications |
|
43
|
|
|
* @param RouteMatchSpecificationFactory $specifications_factory |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
RouteMatchSpecificationCollection $specifications, |
|
47
|
|
|
RouteMatchSpecificationFactory $specifications_factory |
|
48
|
|
|
) { |
|
49
|
|
|
$this->specifications = $specifications; |
|
50
|
|
|
$this->specifications_factory = $specifications_factory; |
|
51
|
|
|
add_action('AHEE__EE_System__loadRouteMatchSpecifications', array($this, 'initialize')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Perform any early setup required for block editors to functions |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
* @throws CollectionLoaderException |
|
60
|
|
|
* @throws CollectionDetailsException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function initialize() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->populateSpecificationCollection(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return CollectionInterface|\EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface[] |
|
70
|
|
|
* @throws CollectionLoaderException |
|
71
|
|
|
* @throws CollectionDetailsException |
|
72
|
|
|
* @since 4.9.71.p |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
private function populateSpecificationCollection() |
|
75
|
|
|
{ |
|
76
|
|
|
$loader = new CollectionLoader( |
|
77
|
|
|
new CollectionDetails( |
|
78
|
|
|
// collection name |
|
79
|
|
|
RouteMatchSpecificationCollection::COLLECTION_NAME, |
|
80
|
|
|
// collection interface |
|
81
|
|
|
'EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface', |
|
82
|
|
|
// FQCNs for classes to add (all classes within each namespace will be loaded) |
|
83
|
|
|
apply_filters( |
|
84
|
|
|
'FHEE__EventEspresso_core_services_route_match_RouteMatchSpecificationManager__populateSpecificationCollection__collection_FQCNs', |
|
85
|
|
|
array( |
|
86
|
|
|
'EventEspresso\core\domain\entities\routing\specifications\admin', |
|
87
|
|
|
'EventEspresso\core\domain\entities\routing\specifications\frontend', |
|
88
|
|
|
) |
|
89
|
|
|
), |
|
90
|
|
|
// filepaths to classes to add |
|
91
|
|
|
array(), |
|
92
|
|
|
// file mask to use if parsing folder for files to add |
|
93
|
|
|
'', |
|
94
|
|
|
// what to use as identifier for collection entities |
|
95
|
|
|
// using CLASS NAME prevents duplicates (works like a singleton) |
|
96
|
|
|
CollectionDetails::ID_CLASS_NAME |
|
97
|
|
|
), |
|
98
|
|
|
$this->specifications, |
|
99
|
|
|
null, |
|
100
|
|
|
$this->specifications_factory |
|
101
|
|
|
); |
|
102
|
|
|
return $loader->getCollection(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Given the FQCN for a RouteMatchSpecification, will return true if the current request matches |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $routeMatchSpecificationFqcn fully qualified class name |
|
110
|
|
|
* @return bool |
|
111
|
|
|
* @throws InvalidClassException |
|
112
|
|
|
* @since 4.9.71.p |
|
113
|
|
|
*/ |
|
114
|
|
|
public function routeMatchesCurrentRequest($routeMatchSpecificationFqcn) |
|
115
|
|
|
{ |
|
116
|
|
|
/** @var \EventEspresso\core\domain\entities\routing\specifications\RouteMatchSpecificationInterface $specification */ |
|
117
|
|
|
$specification = $this->specifications->get($routeMatchSpecificationFqcn); |
|
118
|
|
|
if (! $specification instanceof $routeMatchSpecificationFqcn) { |
|
119
|
|
|
throw new InvalidClassException($routeMatchSpecificationFqcn); |
|
120
|
|
|
} |
|
121
|
|
|
return $specification->isMatchingRoute(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Handy method for development that returns |
|
127
|
|
|
* a list of existing RouteMatchSpecification classes |
|
128
|
|
|
* matching the current request that can be used to identify it. |
|
129
|
|
|
* If no matches are returned (or nothing acceptable) |
|
130
|
|
|
* then create a new one that better matches your requirements. |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
* @since 4.9.71.p |
|
134
|
|
|
*/ |
|
135
|
|
|
public function findRouteMatchSpecificationsMatchingCurrentRequest() |
|
136
|
|
|
{ |
|
137
|
|
|
$matches = array(); |
|
138
|
|
|
foreach ($this->specifications as $specification) { |
|
139
|
|
|
/** @var RouteMatchSpecificationInterface $specification */ |
|
140
|
|
|
if ($specification->isMatchingRoute()) { |
|
141
|
|
|
$matches[] = get_class($specification); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
return $matches; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|