-
Notifications
You must be signed in to change notification settings - Fork 10
/
Stage.php
200 lines (184 loc) · 6.53 KB
/
Stage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* The MIT License
*
* Copyright (c) 2010 - 2012 Tony R Quilkey <trq@proemframework.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @namespace Proem\Dispatch
*/
namespace Proem\Dispatch;
use Proem\Service\Manager\Template as Manager;
use Proem\Routing\Signal\Event\RouteMatch;
use Proem\Routing\Signal\Event\RouteDispatch;
use Proem\Routing\Signal\Event\RouteExhausted;
/**
* The dispatch stage.
*
* This object sets up a staging area where the router and dispatcher
* can put on there show
*/
class Stage
{
/**
* Store the Services Manager
*
* @var Proem\Services\Manager\Template
*/
protected $assets;
/**
* Store a flag
*/
protected $dispatchable = false;
/**
* Setup the stage and start the dispatch process
*
* Within this single construct we attach listeners
* for both the route.macth & route.exhausted events
*
* We then start processing the routes. Once the dispatchable
* flag is true the route is dispatched and execution moves
* into userland *controller* code
*
* @param Proem\Service\Manager\Template $assets
*/
public function __construct(Manager $assets)
{
$this->assets = $assets;
$this->attachRouteMatchListener();
$this->attachRouteExhaustedListener();
$this->attachRouteDispatchListener();
$this->processRoutes();
}
/**
* Register a callback ready to listen for the route.match Event.
*/
protected function attachRouteMatchListener()
{
if ($this->assets->has('events')) {
$this->assets->get('events')->attach('proem.route.match', [$this, 'testRoute']);
}
}
/**
* Register a callback ready to listen for the route.exhausted Event.
*/
protected function attachRouteExhaustedListener()
{
if ($this->assets->has('events')) {
$this->assets->get('events')->attach('proem.route.exhausted', [$this, 'routesExhausted']);
}
}
/**
* Register a callback ready to listen for the route.dispatch Event.
*/
protected function attachRouteDispatchListener()
{
if ($this->assets->has('events')) {
$this->assets->get('events')->attach('proem.route.dispatch', [$this, 'dispatch']);
}
}
/**
* Iterate through matching routes and trigger a match.route Event
* on each iteration.
*
* A listener returning the bool true indicates that the payload is
* dispatchable. This sets the dispatchable flag to true and will
* exit this method.
*
* If all matching routes have been exhausted a route.exhausted event
* is triggered.
*
* @triggers Proem\Routing\Signal\Event\RouteMatch route.match Check to see if a route is dispatchable
* @triggers Proem\Routing\Signal\Event\RouteDispatch route.dispatch Dispatch a route
* @triggers Proem\Routing\Signal\Event\RouteExhausted route.exhausted All routes exhausted
*/
protected function processRoutes()
{
if ($this->assets->has('router') && $this->assets->has('events')) {
$assets = $this->assets;
$router = $assets->get('router');
$dispatched = false;
while ($payload = $router->route()) {
$assets->get('events')->trigger(
(new RouteMatch('proem.route.match'))->setPayload($payload),
function ($response) use (&$dispatched, &$assets) {
if ($response->has('isDispatchable') && $response->getParam('isDispatchable')) {
$dispatched = true;
$assets->get('events')->trigger(new RouteDispatch('proem.route.dispatch'));
}
}
);
if ($dispatched) {
break;
}
}
if (!$dispatched) {
$assets->get('events')->trigger(new RouteExhausted('proem.route.exhausted'));
}
}
}
/**
* Dispatch the payload.
*/
public function dispatch($e)
{
if ($this->assets->has('dispatch')) {
$this->assets->get('dispatch')->dispatch();
}
}
/**
* Listen for the route.match Event.
*
* Pass the RouteMatch event to the dispatcher and have it tested
* to see if it is dispatchable. Return the result.
*
* @param Proem\Routing\Signal\Event\RouteMatch $e
* @return bool
*/
public function testRoute($event)
{
if ($this->assets->has('dispatch')) {
$this->assets->get('dispatch')->setPayload($event->getPayload());
$this->assets->get('dispatch')->setModule($event->getPayload()->get('module', ''));
$this->assets->get('dispatch')->setController($event->getPayload()->get('controller', ''));
$this->assets->get('dispatch')->setAction($event->getPayload()->get('action', ''));
return $event->setParam(
'isDispatchable',
$this->assets->get('dispatch')->isDispatchable()
);
}
}
/**
* Listen for a route.exhuasted Event.
*
* If triggered, dispatch a very standard default 404
*
* @param Proem\Routing\Signal\Event\RouteMatch $e
*/
public function routesExhausted($e)
{
if ($this->assets->has('response')) {
$this->assets->get('response')
->setHttpStatus(404)
->appendToBody('<h3>404 - Page Not Found</h3>');
}
}
}