1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\routing; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\domain\entities\routing\handlers\RouteInterface; |
6
|
|
|
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay; |
7
|
|
|
use EventEspresso\core\exceptions\InvalidClassException; |
8
|
|
|
use EventEspresso\core\services\loaders\LoaderInterface; |
9
|
|
|
use Exception; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RouteHandler |
13
|
|
|
* Description |
14
|
|
|
* |
15
|
|
|
* @package EventEspresso\core\domain\services\admin |
16
|
|
|
* @author Brent Christensen |
17
|
|
|
* @since $VID:$ |
18
|
|
|
*/ |
19
|
|
|
class RouteHandler |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var LoaderInterface |
24
|
|
|
*/ |
25
|
|
|
private $loader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RouteCollection $routes |
29
|
|
|
*/ |
30
|
|
|
private $routes; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* RouteHandler constructor. |
35
|
|
|
* |
36
|
|
|
* @param LoaderInterface $loader |
37
|
|
|
* @param RouteCollection $routes |
38
|
|
|
*/ |
39
|
|
|
public function __construct(LoaderInterface $loader, RouteCollection $routes) |
40
|
|
|
{ |
41
|
|
|
$this->loader = $loader; |
42
|
|
|
$this->routes = $routes; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $fqcn Fully Qualified Class Name for Route |
48
|
|
|
* @param bool $handle if true [default] will immediately call RouteInterface::handleRequest() |
49
|
|
|
* @throws Exception |
50
|
|
|
* @since $VID:$ |
51
|
|
|
*/ |
52
|
|
|
public function addRoute($fqcn, $handle = true) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
$route = $this->loader->getShared($fqcn); |
56
|
|
|
if (! $route instanceof RouteInterface) { |
57
|
|
|
throw new InvalidClassException( |
58
|
|
|
sprintf( |
59
|
|
|
esc_html__( |
60
|
|
|
'The supplied FQCN (%1$s) must be an instance of RouteInterface.', |
61
|
|
|
'event_espresso' |
62
|
|
|
), |
63
|
|
|
$fqcn |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
$this->routes->add($route); |
68
|
|
|
if ($handle) { |
69
|
|
|
$route->handleRequest(); |
70
|
|
|
} |
71
|
|
|
} catch (Exception $exception) { |
72
|
|
|
new ExceptionStackTraceDisplay($exception); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* finds and returns all Routes that have yet to be handled |
79
|
|
|
* |
80
|
|
|
* @return RouteInterface[] |
81
|
|
|
*/ |
82
|
|
|
public function getRoutesForCurrentRequest() |
83
|
|
|
{ |
84
|
|
|
return $this->routes->getRoutesForCurrentRequest(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* calls RouteInterface::handleRequest() on all Routes that |
90
|
|
|
* - match current request |
91
|
|
|
* - have yet to be handled |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function handleRoutesForCurrentRequest() |
96
|
|
|
{ |
97
|
|
|
$this->routes->handleRoutesForCurrentRequest(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|