forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.zep
709 lines (599 loc) · 15.9 KB
/
dispatcher.zep
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
| Rack Lin <racklin@gmail.com> |
+------------------------------------------------------------------------+
*/
namespace Phalcon;
/**
* Phalcon\Dispatcher
*
* This is the base class for Phalcon\Mvc\Dispatcher and Phalcon\CLI\Dispatcher.
* This class can't be instantiated directly, you can use it to create your own dispatchers
*/
abstract class Dispatcher implements \Phalcon\DispatcherInterface, \Phalcon\Di\InjectionAwareInterface, \Phalcon\Events\EventsAwareInterface
{
protected _dependencyInjector;
protected _eventsManager;
protected _activeHandler;
protected _finished;
protected _forwarded = false;
protected _moduleName = null;
protected _namespaceName = null;
protected _handlerName = null;
protected _actionName = null;
protected _params;
protected _returnedValue = null;
protected _lastHandler = null;
protected _defaultNamespace = null;
protected _defaultHandler = null;
protected _defaultAction = "";
protected _handlerSuffix = "";
protected _actionSuffix = "Action";
const EXCEPTION_NO_DI = 0;
const EXCEPTION_CYCLIC_ROUTING = 1;
const EXCEPTION_HANDLER_NOT_FOUND = 2;
const EXCEPTION_INVALID_HANDLER = 3;
const EXCEPTION_INVALID_PARAMS = 4;
const EXCEPTION_ACTION_NOT_FOUND = 5;
/**
* Phalcon\Dispatcher constructor
*
*/
public function __construct()
{
let this->_params = [];
}
/**
* Sets the dependency injector
*
* @param Phalcon\DiInterface dependencyInjector
*/
public function setDI(<\Phalcon\DiInterface> dependencyInjector)
{
let this->_dependencyInjector = dependencyInjector;
}
/**
* Returns the internal dependency injector
*
* @return Phalcon\DiInterface
*/
public function getDI() -> <\Phalcon\DiInterface>
{
return this->_dependencyInjector;
}
/**
* Sets the events manager
*
* @param Phalcon\Events\ManagerInterface eventsManager
*/
public function setEventsManager(<\Phalcon\Events\ManagerInterface> eventsManager)
{
let this->_eventsManager = eventsManager;
}
/**
* Returns the internal event manager
*
* @return Phalcon\Events\ManagerInterface
*/
public function getEventsManager() -> <\Phalcon\Events\ManagerInterface>
{
return this->_eventsManager;
}
/**
* Sets the default action suffix
*
* @param string actionSuffix
*/
public function setActionSuffix(string actionSuffix)
{
let this->_actionSuffix = actionSuffix;
}
/**
* Sets the module where the controller is (only informative)
*
* @param string moduleName
*/
public function setModuleName(string moduleName)
{
let this->_moduleName = moduleName;
}
/**
* Gets the module where the controller class is
*
* @return string
*/
public function getModuleName() -> string
{
return this->_moduleName;
}
/**
* Sets the namespace where the controller class is
*
* @param string namespaceName
*/
public function setNamespaceName(string namespaceName)
{
let this->_namespaceName = namespaceName;
}
/**
* Gets a namespace to be prepended to the current handler name
*
* @return string
*/
public function getNamespaceName() -> string
{
return this->_namespaceName;
}
/**
* Sets the default namespace
*
* @param string namespaceName
*/
public function setDefaultNamespace(string namespaceName)
{
let this->_defaultNamespace = namespaceName;
}
/**
* Returns the default namespace
*
* @return string
*/
public function getDefaultNamespace() -> string
{
return this->_defaultNamespace;
}
/**
* Sets the default action name
*
* @param string actionName
*/
public function setDefaultAction(string actionName)
{
let this->_defaultAction = actionName;
}
/**
* Sets the action name to be dispatched
*
* @param string actionName
*/
public function setActionName(string actionName)
{
let this->_actionName = actionName;
}
/**
* Gets the latest dispatched action name
*
* @return string
*/
public function getActionName() -> string
{
return this->_actionName;
}
/**
* Sets action params to be dispatched
*
* @param array params
*/
public function setParams(var params)
{
if typeof params != "array" {
this->{"_throwDispatchException"}("Parameters must be an Array");
return null;
}
let this->_params = params;
}
/**
* Gets action params
*
* @return array
*/
public function getParams()
{
return this->_params;
}
/**
* Set a param by its name or numeric index
*
* @param mixed param
* @param mixed value
*/
public function setParam(var param, var value)
{
let this->_params[param] = value;
}
/**
* Gets a param by its name or numeric index
*
* @param mixed param
* @param string|array filters
* @param mixed defaultValue
* @return mixed
*/
public function getParam(param, filters=null, defaultValue=null)
{
var params, filter, paramValue, dependencyInjector;
let params = this->_params;
if isset params[param] {
let paramValue = params[param];
if filters !== null {
let dependencyInjector = this->_dependencyInjector;
if typeof dependencyInjector != "object" {
this->{"_throwDispatchException"}("A dependency injection object is required to access the 'filter' service", self::EXCEPTION_NO_DI);
}
let filter = <\Phalcon\Filter> dependencyInjector->getShared("filter");
return filter->sanitize(paramValue, filters);
} else {
return paramValue;
}
}
return defaultValue;
}
/**
* Returns the current method to be/executed in the dispatcher
*
* @return string
*/
public function getActiveMethod() -> string
{
return this->_actionName . this->_actionSuffix;
}
/**
* Checks if the dispatch loop is finished or has more pendent controllers/tasks to dispatch
*
* @return boolean
*/
public function isFinished() -> boolean
{
return this->_finished;
}
/**
* Sets the latest returned value by an action manually
*
* @param mixed value
*/
public function setReturnedValue(var value)
{
let this->_returnedValue = value;
}
/**
* Returns value returned by the lastest dispatched action
*
* @return mixed
*/
public function getReturnedValue()
{
return this->_returnedValue;
}
/**
* Dispatches a handle action taking into account the routing parameters
*
* @return object
*/
public function dispatch()
{
boolean hasService;
int numberDispatches;
var value, handler, dependencyInjector, namespaceName, handlerName,
actionName, camelizedClass, params, eventsManager,
handlerSuffix, actionSuffix, handlerClass, status, actionMethod,
wasFresh = false;
let dependencyInjector = <\Phalcon\DiInterface> this->_dependencyInjector;
if typeof dependencyInjector != "object" {
this->{"_throwDispatchException"}("A dependency injection container is required to access related dispatching services", self::EXCEPTION_NO_DI);
return false;
}
/**
* Calling beforeDispatchLoop
*/
let eventsManager = <\Phalcon\Events\Manager> this->_eventsManager;
if typeof eventsManager == "object" {
if eventsManager->fire("dispatch:beforeDispatchLoop", this) === false {
return false;
}
}
let value = null,
handler = null,
numberDispatches = 0,
handlerSuffix = this->_handlerSuffix,
actionSuffix = this->_actionSuffix,
this->_finished = false;
while !this->_finished {
let numberDispatches++;
/**
* Throw an exception after 256 consecutive forwards
*/
if numberDispatches == 256 {
this->{"_throwDispatchException"}("Dispatcher has detected a cyclic routing causing stability problems", self::EXCEPTION_CYCLIC_ROUTING);
break;
}
let this->_finished = true;
/**
* If the current namespace is null we used the set in this->_defaultNamespace
*/
let namespaceName = this->_namespaceName;
if !namespaceName {
let namespaceName = this->_defaultNamespace;
let this->_namespaceName = namespaceName;
}
/**
* If the handler is null we use the set in this->_defaultHandler
*/
let handlerName = this->_handlerName;
if !handlerName {
let handlerName = this->_defaultHandler;
let this->_handlerName = handlerName;
}
/**
* If the action is null we use the set in this->_defaultAction
*/
let actionName = this->_actionName;
if !actionName {
let actionName = this->_defaultAction;
let this->_actionName = actionName;
}
/**
* Calling beforeDispatch
*/
if typeof eventsManager == "object" {
if eventsManager->fire("dispatch:beforeDispatch", this) === false {
continue;
}
/**
* Check if the user made a forward in the listener
*/
if this->_finished === false {
continue;
}
}
/**
* We don't camelize the classes if they are in namespaces
*/
if !memstr(handlerName, "\\") {
let camelizedClass = camelize(handlerName);
} else {
let camelizedClass = handlerName;
}
/**
* Create the complete controller class name prepending the namespace
*/
if namespaceName {
if ends_with(namespaceName, "\\") {
let handlerClass = namespaceName . camelizedClass . handlerSuffix;
} else {
let handlerClass = namespaceName . "\\" . camelizedClass . handlerSuffix;
}
} else {
let handlerClass = camelizedClass . handlerSuffix;
}
/**
* Handlers are retrieved as shared instances from the Service Container
*/
let hasService = (bool) dependencyInjector->has(handlerClass);
if !hasService {
/**
* DI doesn't have a service with that name, try to load it using an autoloader
*/
let hasService = (bool) class_exists(handlerClass);
}
/**
* If the service can be loaded we throw an exception
*/
if !hasService {
let status = this->{"_throwDispatchException"}(handlerClass . " handler class cannot be loaded", self::EXCEPTION_HANDLER_NOT_FOUND);
if status === false {
/**
* Check if the user made a forward in the listener
*/
if this->_finished === false {
continue;
}
}
break;
}
/**
* Handlers must be only objects
*/
let handler = dependencyInjector->getShared(handlerClass);
/**
* If the object was recently created in the DI we initialize it
*/
if dependencyInjector->wasFreshInstance() === true {
let wasFresh = true;
}
if typeof handler != "object" {
let status = this->{"_throwDispatchException"}("Invalid handler returned from the services container", self::EXCEPTION_INVALID_HANDLER);
if status === false {
if this->_finished === false {
continue;
}
}
break;
}
let this->_activeHandler = handler;
/**
* Check if the params is an array
*/
let params = this->_params;
if typeof params != "array" {
/**
* An invalid parameter variable was passed throw an exception
*/
let status = this->{"_throwDispatchException"}("Action parameters must be an Array", self::EXCEPTION_INVALID_PARAMS);
if status === false {
if this->_finished === false {
continue;
}
}
break;
}
/**
* Check if the method exists in the handler
*/
let actionMethod = actionName . actionSuffix;
if !method_exists(handler, actionMethod) {
/**
* Call beforeNotFoundAction
*/
if typeof eventsManager == "object" {
if eventsManager->fire("dispatch:beforeNotFoundAction", this) === false {
continue;
}
if this->_finished === false {
continue;
}
}
/**
* Try to throw an exception when an action isn't defined on the object
*/
let status = this->{"_throwDispatchException"}("Action '" . actionName . "' was not found on handler '" . handlerName . "'", self::EXCEPTION_ACTION_NOT_FOUND);
if status === false {
if this->_finished === false {
continue;
}
}
break;
}
/**
* Calling beforeExecuteRoute
*/
if typeof eventsManager == "object" {
if eventsManager->fire("dispatch:beforeExecuteRoute", this) === false {
continue;
}
/**
* Check if the user made a forward in the listener
*/
if this->_finished === false {
continue;
}
}
/**
* Calling beforeExecuteRoute as callback and event
*/
if method_exists(handler, "beforeExecuteRoute") {
if handler->beforeExecuteRoute(this) === false {
continue;
}
/**
* Check if the user made a forward in the listener
*/
if this->_finished === false {
continue;
}
}
if (wasFresh === true) {
if method_exists(handler, "initialize") {
handler->initialize();
}
}
/**
* We update the latest value produced by the latest handler
*/
let this->_returnedValue = call_user_func_array([handler, actionMethod], params),
this->_lastHandler = handler;
/**
* Calling afterExecuteRoute
*/
if typeof eventsManager == "object" {
if eventsManager->fire("dispatch:afterExecuteRoute", this, value) === false {
continue;
}
if this->_finished === false {
continue;
}
/**
* Call afterDispatch
*/
eventsManager->fire("dispatch:afterDispatch", this);
}
/**
* Calling afterExecuteRoute as callback and event
*/
if method_exists(handler, "afterExecuteRoute") {
if handler->afterExecuteRoute(this, value) === false {
continue;
}
if this->_finished === false {
continue;
}
}
}
/**
* Call afterDispatchLoop
*/
if typeof eventsManager == "object" {
eventsManager->fire("dispatch:afterDispatchLoop", this);
}
return handler;
}
/**
* Forwards the execution flow to another controller/action
* Dispatchers are unique per module. Forwarding between modules is not allowed
*
*<code>
* $this->dispatcher->forward(array("controller" => "posts", "action" => "index"));
*</code>
*
* @param array forward
*/
public function forward(var forward)
{
var namespaceName, controllerName, params, actionName, taskName;
if typeof forward != "array" {
this->{"_throwDispatchException"}("Forward parameter must be an Array");
return null;
}
/**
* Check if we need to forward to another namespace
*/
if fetch namespaceName, forward["namespace"] {
let this->_namespaceName = namespaceName;
}
/**
* Check if we need to forward to another controller
*/
if fetch controllerName, forward["controller"] {
let this->_handlerName = controllerName;
} else {
if fetch taskName, forward["task"] {
let this->_handlerName = taskName;
}
}
/**
* Check if we need to forward to another action
*/
if fetch actionName, forward["action"] {
let this->_actionName = actionName;
}
/**
* Check if we need to forward changing the current parameters
*/
if fetch params, forward["params"] {
let this->_params = params;
}
let this->_finished = false,
this->_forwarded = true;
}
/**
* Check if the current executed action was forwarded by another one
*
* @return boolean
*/
public function wasForwarded() -> boolean
{
return this->_forwarded;
}
}