1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\routing; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\domain\entities\routing\handlers\RouteInterface; |
6
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
7
|
|
|
use EventEspresso\core\services\collections\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RouteCollection |
11
|
|
|
* SplObjectStorage Collection of \EventEspresso\core\domain\entities\routing\handlers\RouteInterface objects |
12
|
|
|
* |
13
|
|
|
* @package EventEspresso\core\services\routing |
14
|
|
|
* @author Brent Christensen |
15
|
|
|
* @since 4.9.71.p |
16
|
|
|
*/ |
17
|
|
|
class RouteCollection extends Collection |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const COLLECTION_NAME = 'routes'; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* RouteMatchSpecificationCollection constructor |
25
|
|
|
* |
26
|
|
|
* @throws InvalidInterfaceException |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
parent::__construct( |
31
|
|
|
'EventEspresso\core\domain\entities\routing\handlers\RouteInterface', |
32
|
|
|
RouteCollection::COLLECTION_NAME |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* getIdentifier |
39
|
|
|
* Overrides EventEspresso\core\services\collections\Collection::getIdentifier() |
40
|
|
|
* If no $identifier is supplied, then the fully qualified class name is used |
41
|
|
|
* |
42
|
|
|
* @param $object |
43
|
|
|
* @param mixed $identifier |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function getIdentifier($object, $identifier = null) |
47
|
|
|
{ |
48
|
|
|
return ! empty($identifier) |
49
|
|
|
? $identifier |
50
|
|
|
: get_class($object); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* finds and returns all Routes that have yet to be handled |
56
|
|
|
* |
57
|
|
|
* @return RouteInterface[] |
58
|
|
|
*/ |
59
|
|
|
public function getRoutesForCurrentRequest() |
60
|
|
|
{ |
61
|
|
|
$routes = []; |
62
|
|
|
$this->rewind(); |
63
|
|
|
while ($this->valid()) { |
64
|
|
|
/** @var RouteInterface $route */ |
65
|
|
|
$route = $this->current(); |
66
|
|
|
if ($route->matchesCurrentRequest()) { |
67
|
|
|
$routes[] = $route; |
68
|
|
|
} |
69
|
|
|
$this->next(); |
70
|
|
|
} |
71
|
|
|
$this->rewind(); |
72
|
|
|
return $routes; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* calls RouteInterface::handleRequest() on all Routes that |
78
|
|
|
* - match current request |
79
|
|
|
* - have yet to be handled |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function handleRoutesForCurrentRequest() |
84
|
|
|
{ |
85
|
|
|
$this->rewind(); |
86
|
|
|
while ($this->valid()) { |
87
|
|
|
$this->current()->handleRequest(); |
88
|
|
|
$this->next(); |
89
|
|
|
} |
90
|
|
|
$this->rewind(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|