forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.c
1155 lines (936 loc) · 36.5 KB
/
dispatcher.c
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+------------------------------------------------------------------------+
| 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> |
+------------------------------------------------------------------------+
*/
#include "dispatcher.h"
#include "dispatcherinterface.h"
#include "diinterface.h"
#include "di/injectionawareinterface.h"
#include "events/eventsawareinterface.h"
#include "exception.h"
#include "filterinterface.h"
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/array.h"
#include "kernel/concat.h"
#include "kernel/operators.h"
#include "kernel/string.h"
#include "kernel/exception.h"
#include "interned-strings.h"
/**
* 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
*/
zend_class_entry *phalcon_dispatcher_ce;
PHP_METHOD(Phalcon_Dispatcher, __construct);
PHP_METHOD(Phalcon_Dispatcher, setDI);
PHP_METHOD(Phalcon_Dispatcher, getDI);
PHP_METHOD(Phalcon_Dispatcher, setEventsManager);
PHP_METHOD(Phalcon_Dispatcher, getEventsManager);
PHP_METHOD(Phalcon_Dispatcher, setActionSuffix);
PHP_METHOD(Phalcon_Dispatcher, setModuleName);
PHP_METHOD(Phalcon_Dispatcher, getModuleName);
PHP_METHOD(Phalcon_Dispatcher, setNamespaceName);
PHP_METHOD(Phalcon_Dispatcher, getNamespaceName);
PHP_METHOD(Phalcon_Dispatcher, setDefaultNamespace);
PHP_METHOD(Phalcon_Dispatcher, getDefaultNamespace);
PHP_METHOD(Phalcon_Dispatcher, setDefaultAction);
PHP_METHOD(Phalcon_Dispatcher, setActionName);
PHP_METHOD(Phalcon_Dispatcher, getActionName);
PHP_METHOD(Phalcon_Dispatcher, setParams);
PHP_METHOD(Phalcon_Dispatcher, getParams);
PHP_METHOD(Phalcon_Dispatcher, setParam);
PHP_METHOD(Phalcon_Dispatcher, getParam);
PHP_METHOD(Phalcon_Dispatcher, getActiveMethod);
PHP_METHOD(Phalcon_Dispatcher, isFinished);
PHP_METHOD(Phalcon_Dispatcher, setReturnedValue);
PHP_METHOD(Phalcon_Dispatcher, getReturnedValue);
PHP_METHOD(Phalcon_Dispatcher, dispatch);
PHP_METHOD(Phalcon_Dispatcher, forward);
PHP_METHOD(Phalcon_Dispatcher, wasForwarded);
PHP_METHOD(Phalcon_Dispatcher, getHandlerClass);
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_dispatcher_setmodulename, 0, 0, 1)
ZEND_ARG_INFO(0, moduleName)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_dispatcher_setnamespacename, 0, 0, 1)
ZEND_ARG_INFO(0, namespaceName)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_dispatcher_setreturnedvalue, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
static const zend_function_entry phalcon_dispatcher_method_entry[] = {
PHP_ME(Phalcon_Dispatcher, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Dispatcher, setDI, arginfo_phalcon_di_injectionawareinterface_setdi, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getDI, arginfo_phalcon_di_injectionawareinterface_getdi, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setEventsManager, arginfo_phalcon_events_eventsawareinterface_seteventsmanager, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getEventsManager, arginfo_phalcon_events_eventsawareinterface_geteventsmanager, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setActionSuffix, arginfo_phalcon_dispatcherinterface_setactionsuffix, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setModuleName, arginfo_phalcon_dispatcher_setmodulename, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getModuleName, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setNamespaceName, arginfo_phalcon_dispatcher_setnamespacename, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getNamespaceName, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setDefaultNamespace, arginfo_phalcon_dispatcherinterface_setdefaultnamespace, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getDefaultNamespace, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setDefaultAction, arginfo_phalcon_dispatcherinterface_setdefaultaction, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setActionName, arginfo_phalcon_dispatcherinterface_setactionname, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getActionName, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setParams, arginfo_phalcon_dispatcherinterface_setparams, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getParams, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setParam, arginfo_phalcon_dispatcherinterface_setparam, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getParam, arginfo_phalcon_dispatcherinterface_getparam, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getActiveMethod, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, isFinished, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, setReturnedValue, arginfo_phalcon_dispatcher_setreturnedvalue, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getReturnedValue, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, dispatch, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, forward, arginfo_phalcon_dispatcherinterface_forward, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, wasForwarded, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Dispatcher, getHandlerClass, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/**
* Phalcon\Dispatcher initializer
*/
PHALCON_INIT_CLASS(Phalcon_Dispatcher){
PHALCON_REGISTER_CLASS(Phalcon, Dispatcher, dispatcher, phalcon_dispatcher_method_entry, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_dependencyInjector"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_eventsManager"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_activeHandler"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_finished"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_bool(phalcon_dispatcher_ce, SL("_forwarded"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_moduleName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_namespaceName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_handlerName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_actionName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_params"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_returnedValue"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_lastHandler"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_defaultNamespace"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_defaultHandler"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_string(phalcon_dispatcher_ce, SL("_defaultAction"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_string(phalcon_dispatcher_ce, SL("_handlerSuffix"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_string(phalcon_dispatcher_ce, SL("_actionSuffix"), "Action", ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_bool(phalcon_dispatcher_ce, SL("_isExactHandler"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_previousHandlerName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_dispatcher_ce, SL("_previousActionName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_NO_DI"), 0 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_CYCLIC_ROUTING"), 1 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_HANDLER_NOT_FOUND"), 2 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_INVALID_HANDLER"), 3 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_INVALID_PARAMS"), 4 TSRMLS_CC);
zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_ACTION_NOT_FOUND"), 5 TSRMLS_CC);
zend_class_implements(phalcon_dispatcher_ce TSRMLS_CC, 3, phalcon_dispatcherinterface_ce, phalcon_di_injectionawareinterface_ce, phalcon_events_eventsawareinterface_ce);
return SUCCESS;
}
/**
* Phalcon\Dispatcher constructor
*/
PHP_METHOD(Phalcon_Dispatcher, __construct){
phalcon_update_property_empty_array(this_ptr, SL("_params") TSRMLS_CC);
}
/**
* Sets the dependency injector
*
* @param Phalcon\DiInterface $dependencyInjector
*/
PHP_METHOD(Phalcon_Dispatcher, setDI){
zval *dependency_injector;
phalcon_fetch_params(0, 1, 0, &dependency_injector);
PHALCON_VERIFY_INTERFACE_EX(dependency_injector, phalcon_diinterface_ce, phalcon_exception_ce, 0);
phalcon_update_property_this(this_ptr, SL("_dependencyInjector"), dependency_injector TSRMLS_CC);
}
/**
* Returns the internal dependency injector
*
* @return Phalcon\DiInterface
*/
PHP_METHOD(Phalcon_Dispatcher, getDI){
RETURN_MEMBER(this_ptr, "_dependencyInjector");
}
/**
* Sets the events manager
*
* @param Phalcon\Events\ManagerInterface $eventsManager
*/
PHP_METHOD(Phalcon_Dispatcher, setEventsManager){
zval *events_manager;
phalcon_fetch_params(0, 1, 0, &events_manager);
phalcon_update_property_this(this_ptr, SL("_eventsManager"), events_manager TSRMLS_CC);
}
/**
* Returns the internal event manager
*
* @return Phalcon\Events\ManagerInterface
*/
PHP_METHOD(Phalcon_Dispatcher, getEventsManager){
RETURN_MEMBER(this_ptr, "_eventsManager");
}
/**
* Sets the default action suffix
*
* @param string $actionSuffix
*/
PHP_METHOD(Phalcon_Dispatcher, setActionSuffix){
zval *action_suffix;
phalcon_fetch_params(0, 1, 0, &action_suffix);
phalcon_update_property_this(this_ptr, SL("_actionSuffix"), action_suffix TSRMLS_CC);
}
/**
* Sets the module where the controller is (only informative)
*
* @param string $moduleName
*/
PHP_METHOD(Phalcon_Dispatcher, setModuleName){
zval *module_name;
phalcon_fetch_params(0, 1, 0, &module_name);
phalcon_update_property_this(this_ptr, SL("_moduleName"), module_name TSRMLS_CC);
}
/**
* Gets the module where the controller class is
*
* @return string
*/
PHP_METHOD(Phalcon_Dispatcher, getModuleName){
RETURN_MEMBER(this_ptr, "_moduleName");
}
/**
* Sets the namespace where the controller class is
*
* @param string $namespaceName
*/
PHP_METHOD(Phalcon_Dispatcher, setNamespaceName){
zval *namespace_name;
phalcon_fetch_params(0, 1, 0, &namespace_name);
phalcon_update_property_this(this_ptr, SL("_namespaceName"), namespace_name TSRMLS_CC);
}
/**
* Gets a namespace to be prepended to the current handler name
*
* @return string
*/
PHP_METHOD(Phalcon_Dispatcher, getNamespaceName){
RETURN_MEMBER(this_ptr, "_namespaceName");
}
/**
* Sets the default namespace
*
* @param string $namespace
*/
PHP_METHOD(Phalcon_Dispatcher, setDefaultNamespace){
zval *namespace;
phalcon_fetch_params(0, 1, 0, &namespace);
phalcon_update_property_this(this_ptr, SL("_defaultNamespace"), namespace TSRMLS_CC);
}
/**
* Returns the default namespace
*
* @return string
*/
PHP_METHOD(Phalcon_Dispatcher, getDefaultNamespace){
RETURN_MEMBER(this_ptr, "_defaultNamespace");
}
/**
* Sets the default action name
*
* @param string $actionName
*/
PHP_METHOD(Phalcon_Dispatcher, setDefaultAction){
zval *action_name;
phalcon_fetch_params(0, 1, 0, &action_name);
phalcon_update_property_this(this_ptr, SL("_defaultAction"), action_name TSRMLS_CC);
}
/**
* Sets the action name to be dispatched
*
* @param string $actionName
*/
PHP_METHOD(Phalcon_Dispatcher, setActionName){
zval *action_name;
phalcon_fetch_params(0, 1, 0, &action_name);
phalcon_update_property_this(this_ptr, SL("_actionName"), action_name TSRMLS_CC);
}
/**
* Gets the lastest dispatched action name
*
* @return string
*/
PHP_METHOD(Phalcon_Dispatcher, getActionName){
RETURN_MEMBER(this_ptr, "_actionName");
}
/**
* Sets action params to be dispatched
*
* @param array $params
*/
PHP_METHOD(Phalcon_Dispatcher, setParams){
zval *params, *exception_message;
phalcon_fetch_params(0, 1, 0, ¶ms);
if (Z_TYPE_P(params) != IS_ARRAY) {
PHALCON_MM_GROW();
PHALCON_INIT_VAR(exception_message);
ZVAL_STRING(exception_message, "Parameters must be an Array", 1);
PHALCON_CALL_METHOD(NULL, this_ptr, "_throwdispatchexception", exception_message);
RETURN_MM_NULL();
}
phalcon_update_property_this(this_ptr, SL("_params"), params TSRMLS_CC);
}
/**
* Gets action params
*
* @return array
*/
PHP_METHOD(Phalcon_Dispatcher, getParams){
RETURN_MEMBER(this_ptr, "_params");
}
/**
* Set a param by its name or numeric index
*
* @param mixed $param
* @param mixed $value
*/
PHP_METHOD(Phalcon_Dispatcher, setParam){
zval *param, *value;
phalcon_fetch_params(0, 2, 0, ¶m, &value);
phalcon_update_property_array(this_ptr, SL("_params"), param, value TSRMLS_CC);
}
/**
* Gets a param by its name or numeric index
*
* @param mixed $param
* @param string|array $filters
* @param mixed $defaultValue
* @return mixed
*/
PHP_METHOD(Phalcon_Dispatcher, getParam){
zval *param, *filters = NULL, *default_value = NULL;
zval *exception_code;
zval *exception_message, *service, *filter = NULL;
zval *params, *param_value, *dependency_injector;
phalcon_fetch_params(0, 1, 2, ¶m, &filters, &default_value);
params = phalcon_fetch_nproperty_this(this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
if (phalcon_array_isset_fetch(¶m_value, params, param)) {
if (filters && Z_TYPE_P(filters) != IS_NULL) {
PHALCON_MM_GROW();
dependency_injector = phalcon_fetch_nproperty_this(this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC);
if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
PHALCON_INIT_VAR(exception_code);
ZVAL_LONG(exception_code, 0);
PHALCON_INIT_VAR(exception_message);
ZVAL_STRING(exception_message, "A dependency injection object is required to access the 'filter' service", 1);
PHALCON_CALL_METHOD(NULL, this_ptr, "_throwdispatchexception", exception_message, exception_code);
RETURN_MM();
}
PHALCON_INIT_VAR(service);
PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_filter);
PHALCON_CALL_METHOD(&filter, dependency_injector, "getshared", service);
PHALCON_VERIFY_INTERFACE(filter, phalcon_filterinterface_ce);
PHALCON_RETURN_CALL_METHOD(filter, "sanitize", param_value, filters);
RETURN_MM();
} else {
RETURN_ZVAL(param_value, 1, 0);
}
}
if (default_value) {
RETURN_ZVAL(default_value, 1, 0);
}
RETURN_NULL();
}
/**
* Returns the current method to be/executed in the dispatcher
*
* @return string
*/
PHP_METHOD(Phalcon_Dispatcher, getActiveMethod){
zval *suffix, *action_name;
suffix = phalcon_fetch_nproperty_this(this_ptr, SL("_actionSuffix"), PH_NOISY TSRMLS_CC);
action_name = phalcon_fetch_nproperty_this(this_ptr, SL("_actionName"), PH_NOISY TSRMLS_CC);
PHALCON_CONCAT_VV(return_value, action_name, suffix);
}
/**
* Checks if the dispatch loop is finished or has more pendent controllers/tasks to disptach
*
* @return boolean
*/
PHP_METHOD(Phalcon_Dispatcher, isFinished){
RETURN_MEMBER(this_ptr, "_finished");
}
/**
* Sets the latest returned value by an action manually
*
* @param mixed $value
*/
PHP_METHOD(Phalcon_Dispatcher, setReturnedValue){
zval *value;
phalcon_fetch_params(0, 1, 0, &value);
phalcon_update_property_this(this_ptr, SL("_returnedValue"), value TSRMLS_CC);
}
/**
* Returns value returned by the lastest dispatched action
*
* @return mixed
*/
PHP_METHOD(Phalcon_Dispatcher, getReturnedValue){
RETURN_MEMBER(this_ptr, "_returnedValue");
}
static int phalcon_dispatcher_fire_event(zval **return_value_ptr, zval *mgr, const char *event, zval *source, zval *data TSRMLS_DC)
{
if (mgr) {
zval *event_name;
int status, status2;
zend_uint nparams = (data ? 3 : 2);
zval *params[3];
MAKE_STD_ZVAL(event_name);
ZVAL_STRING(event_name, event, 0);
params[0] = event_name;
params[1] = source;
params[2] = data;
status = phalcon_call_method(return_value_ptr, mgr, "fire", nparams, params TSRMLS_CC);
if (EG(exception)) {
zval *exception = EG(exception);
Z_ADDREF_P(exception);
zend_clear_exception(TSRMLS_C);
assert(Z_REFCOUNT_P(exception) == 1);
/* exception will be destroyed automatically after return from _handleexception */
Z_DELREF_P(exception);
/* source == this_ptr */
assert(Z_TYPE_P(source) == IS_OBJECT && instanceof_function_ex(Z_OBJCE_P(source), phalcon_dispatcherinterface_ce, 1 TSRMLS_CC));
if (is_phalcon_class(Z_OBJCE_P(source))) {
zval *params[] = { event_name, source, exception };
/* Shortcut, save one method call */
ZVAL_STRING(event_name, "dispatch:beforeException", 0);
status2 = phalcon_call_method(NULL, mgr, "fire", 3, params TSRMLS_CC);
}
else {
zval *params[] = { exception };
status2 = phalcon_call_method(NULL, source, "_handleexception", 1, params TSRMLS_CC);
}
if (FAILURE == status2) {
status = FAILURE;
}
}
ZVAL_NULL(event_name);
zval_ptr_dtor(&event_name);
return status;
}
else if (return_value_ptr) {
MAKE_STD_ZVAL(*return_value_ptr);
ZVAL_TRUE(*return_value_ptr);
}
return SUCCESS;
}
/**
* Dispatches a handle action taking into account the routing parameters
*
* @return object
*/
PHP_METHOD(Phalcon_Dispatcher, dispatch){
zval *exception_code = NULL;
zval *exception_message = NULL;
zval *status = NULL, *value = NULL, *handler = NULL;
zval *camelized_class = NULL, *handler_class = NULL, *has_service = NULL;
zval *was_fresh = NULL, *action_method = NULL, *params = NULL, *call_object = NULL;
zval *exception = NULL;
zval *dependency_injector, *events_manager, *tmp;
zval *handler_suffix, *action_suffix, *namespace_name, *handler_name, *action_name;
int number_dispatches = 0;
PHALCON_MM_GROW();
dependency_injector = phalcon_fetch_property_this(this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC);
if (!dependency_injector || Z_TYPE_P(dependency_injector) != IS_OBJECT) {
PHALCON_INIT_VAR(exception_code);
ZVAL_LONG(exception_code, 0);
PHALCON_INIT_VAR(exception_message);
ZVAL_STRING(exception_message, "A dependency injection container is required to access related dispatching services", 1);
PHALCON_CALL_METHOD(NULL, this_ptr, "_throwdispatchexception", exception_message, exception_code);
RETURN_MM();
}
events_manager = phalcon_fetch_property_this(this_ptr, SL("_eventsManager"), PH_NOISY TSRMLS_CC);
if (events_manager && Z_TYPE_P(events_manager) != IS_OBJECT) {
events_manager = NULL;
}
/**
* Calling beforeDispatchLoop
*/
PHALCON_OBSERVE_OR_NULLIFY_VAR(status);
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(&status, events_manager, "dispatch:beforeDispatchLoop", this_ptr, NULL TSRMLS_CC));
if (PHALCON_IS_FALSE(status)) {
RETURN_MM_FALSE;
}
PHALCON_INIT_VAR(value);
PHALCON_INIT_VAR(handler);
handler_suffix = phalcon_fetch_nproperty_this(this_ptr, SL("_handlerSuffix"), PH_NOISY TSRMLS_CC);
action_suffix = phalcon_fetch_nproperty_this(this_ptr, SL("_actionSuffix"), PH_NOISY TSRMLS_CC);
/**
* Do at least one dispatch
*/
phalcon_update_property_this(this_ptr, SL("_finished"), PHALCON_GLOBAL(z_false) TSRMLS_CC);
while (1) {
/**
* Loop until finished is false
*/
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (zend_is_true(tmp)) {
break;
}
++number_dispatches;
/**
* Throw an exception after 256 consecutive forwards
*/
if (number_dispatches == 256) {
PHALCON_INIT_NVAR(exception_code);
ZVAL_LONG(exception_code, 1);
PHALCON_INIT_NVAR(exception_message);
ZVAL_STRING(exception_message, "Dispatcher has detected a cyclic routing causing stability problems", 1);
PHALCON_CALL_METHOD(NULL, this_ptr, "_throwdispatchexception", exception_message, exception_code);
break;
}
phalcon_update_property_this(this_ptr, SL("_finished"), PHALCON_GLOBAL(z_true) TSRMLS_CC);
/**
* If the current namespace is null we used the set in this_ptr::_defaultNamespace
*/
namespace_name = phalcon_fetch_nproperty_this(this_ptr, SL("_namespaceName"), PH_NOISY TSRMLS_CC);
if (!zend_is_true(namespace_name)) {
namespace_name = phalcon_fetch_nproperty_this(this_ptr, SL("_defaultNamespace"), PH_NOISY TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_namespaceName"), namespace_name TSRMLS_CC);
}
/**
* If the handler is null we use the set in this_ptr::_defaultHandler
*/
handler_name = phalcon_fetch_nproperty_this(this_ptr, SL("_handlerName"), PH_NOISY TSRMLS_CC);
if (!zend_is_true(handler_name)) {
handler_name = phalcon_fetch_nproperty_this(this_ptr, SL("_defaultHandler"), PH_NOISY TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_handlerName"), handler_name TSRMLS_CC);
}
/**
* If the action is null we use the set in this_ptr::_defaultAction
*/
action_name = phalcon_fetch_nproperty_this(this_ptr, SL("_actionName"), PH_NOISY TSRMLS_CC);
if (!zend_is_true(action_name)) {
action_name = phalcon_fetch_nproperty_this(this_ptr, SL("_defaultAction"), PH_NOISY TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_actionName"), action_name TSRMLS_CC);
}
/**
* Calling beforeDispatch
*/
if (events_manager) {
PHALCON_OBSERVE_OR_NULLIFY_VAR(status);
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(&status, events_manager, "dispatch:beforeDispatch", this_ptr, NULL TSRMLS_CC));
if (PHALCON_IS_FALSE(status)) {
continue;
}
/**
* Check if the user made a forward in the listener
*/
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
/**
* We don't camelize the classes if they are in namespaces
*/
if (!phalcon_memnstr_str(handler_name, SL("\\"))) {
PHALCON_INIT_NVAR(camelized_class);
phalcon_camelize(camelized_class, handler_name);
} else if (phalcon_start_with_str(handler_name, SL("\\"))) {
PHALCON_INIT_NVAR(camelized_class);
ZVAL_STRINGL(camelized_class, Z_STRVAL_P(handler_name)+1, Z_STRLEN_P(handler_name)-1, 1);
} else {
camelized_class = handler_name;
}
/**
* Create the complete controller class name prepending the namespace
*/
PHALCON_INIT_NVAR(handler_class);
if (zend_is_true(namespace_name)) {
if (phalcon_end_with_str(namespace_name, SL("\\"))) {
PHALCON_CONCAT_VVV(handler_class, namespace_name, camelized_class, handler_suffix);
} else {
PHALCON_CONCAT_VSVV(handler_class, namespace_name, "\\", camelized_class, handler_suffix);
}
} else {
PHALCON_CONCAT_VV(handler_class, camelized_class, handler_suffix);
}
/**
* Handlers are retrieved as shared instances from the Service Container
*/
PHALCON_CALL_METHOD(&has_service, dependency_injector, "has", handler_class);
if (!zend_is_true(has_service)) {
/**
* DI doesn't have a service with that name, try to load it using an autoloader
*/
PHALCON_INIT_NVAR(has_service);
assert(Z_TYPE_P(handler_class) == IS_STRING);
ZVAL_LONG(has_service, phalcon_class_exists(Z_STRVAL_P(handler_class), Z_STRLEN_P(handler_class), 1 TSRMLS_CC));
}
/**
* If the service cannot be loaded we throw an exception
*/
if (!zend_is_true(has_service)) {
PHALCON_INIT_NVAR(exception_code);
ZVAL_LONG(exception_code, 2);
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_VS(exception_message, handler_class, " handler class cannot be loaded");
PHALCON_CALL_METHOD(&status, this_ptr, "_throwdispatchexception", exception_message, exception_code);
if (PHALCON_IS_FALSE(status)) {
/**
* Check if the user made a forward in the listener
*/
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
break;
}
/**
* Handlers must be only objects
*/
PHALCON_CALL_METHOD(&handler, dependency_injector, "getshared", handler_class);
if (Z_TYPE_P(handler) != IS_OBJECT) {
PHALCON_INIT_NVAR(exception_code);
ZVAL_LONG(exception_code, 3);
PHALCON_INIT_NVAR(exception_message);
ZVAL_STRING(exception_message, "Invalid handler returned from the services container", 1);
PHALCON_CALL_METHOD(&status, this_ptr, "_throwdispatchexception", exception_message, exception_code);
if (PHALCON_IS_FALSE(status)) {
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
break;
}
/**
* If the object was recently created in the DI we initialize it
*/
PHALCON_CALL_METHOD(&was_fresh, dependency_injector, "wasfreshinstance");
/**
* Update the active handler making it available for events
*/
phalcon_update_property_this(this_ptr, SL("_activeHandler"), handler TSRMLS_CC);
/**
* Check if the method exists in the handler
*/
PHALCON_INIT_NVAR(action_method);
PHALCON_CONCAT_VV(action_method, action_name, action_suffix);
if (phalcon_method_exists(handler, action_method TSRMLS_CC) == FAILURE) {
/**
* Call beforeNotFoundAction
*/
if (events_manager) {
PHALCON_OBSERVE_OR_NULLIFY_VAR(status);
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(&status, events_manager, "dispatch:beforeNotFoundAction", this_ptr, NULL TSRMLS_CC));
if (PHALCON_IS_FALSE(status)) {
continue;
}
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
PHALCON_INIT_NVAR(exception_code);
ZVAL_LONG(exception_code, 5);
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVSVS(exception_message, "Action '", action_name, "' was not found on handler '", handler_name, "'");
/**
* Try to throw an exception when an action isn't defined on the object
*/
PHALCON_CALL_METHOD(&status, this_ptr, "_throwdispatchexception", exception_message, exception_code);
if (PHALCON_IS_FALSE(status)) {
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
break;
}
/**
* Calling beforeExecuteRoute
*/
if (events_manager) {
PHALCON_OBSERVE_OR_NULLIFY_VAR(status);
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(&status, events_manager, "dispatch:beforeExecuteRoute", this_ptr, NULL TSRMLS_CC));
if (PHALCON_IS_FALSE(status)) {
continue;
}
/**
* Check if the user made a forward in the listener
*/
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
/**
* Calling beforeExecuteRoute as callback and event
*/
if (phalcon_method_exists_ex(handler, SS("beforeexecuteroute") TSRMLS_CC) == SUCCESS) {
PHALCON_CALL_METHOD(&status, handler, "beforeexecuteroute", this_ptr);
if (PHALCON_IS_FALSE(status)) {
continue;
}
/**
* Check if the user made a forward in the listener
*/
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
/**
* Call the 'initialize' method just once per request
*/
if (PHALCON_IS_TRUE(was_fresh)) {
if (phalcon_method_exists_ex(handler, SS("initialize") TSRMLS_CC) == SUCCESS) {
PHALCON_CALL_METHOD(NULL, handler, "initialize");
}
/**
* Calling afterInitialize
*/
if (events_manager) {
PHALCON_OBSERVE_OR_NULLIFY_VAR(status);
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(&status, events_manager, "dispatch:afterInitialize", this_ptr, NULL TSRMLS_CC));
if (PHALCON_IS_FALSE(status)) {
continue;
}
/**
* Check if the user made a forward in the listener
*/
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
}
/**
* Check if the params is an array
*/
PHALCON_OBS_NVAR(params);
phalcon_read_property_this(¶ms, this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
if (Z_TYPE_P(params) != IS_ARRAY) {
PHALCON_INIT_NVAR(exception_code);
ZVAL_LONG(exception_code, 4);
PHALCON_INIT_NVAR(exception_message);
ZVAL_STRING(exception_message, "Action parameters must be an Array", 1);
/**
* An invalid parameter variable was passed throw an exception
*/
PHALCON_CALL_METHOD(&status, this_ptr, "_throwdispatchexception", exception_message, exception_code);
if (PHALCON_IS_FALSE(status)) {
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
break;
}
/**
* Create a call handler
*/
PHALCON_INIT_NVAR(call_object);
array_init_size(call_object, 2);
phalcon_array_append(&call_object, handler, 0);
phalcon_array_append(&call_object, action_method, 0);
PHALCON_INIT_NVAR(value);
/* Call the method allowing exceptions */
PHALCON_CALL_USER_FUNC_ARRAY_NOEX(value, call_object, params);
/* Check if an exception has ocurred */
if (EG(exception)) {
/* Copy the exception to rethrow it later if needed */
PHALCON_CPY_WRT(exception, EG(exception));
/* Clear the exception */
zend_clear_exception(TSRMLS_C);
/* Try to handle the exception */
PHALCON_CALL_METHOD(&status, this_ptr, "_handleexception", exception);
if (PHALCON_IS_FALSE(status)) {
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
}
else {
/* Exception was not handled, rethrow it */
phalcon_throw_exception(exception TSRMLS_CC);
RETURN_MM();
}
} else {
/* Update the latest value produced by the latest handler */
phalcon_update_property_this(this_ptr, SL("_returnedValue"), value TSRMLS_CC);
}
phalcon_update_property_this(this_ptr, SL("_lastHandler"), handler TSRMLS_CC);
if (events_manager) {
/**
* Call afterExecuteRoute
*/
PHALCON_OBSERVE_OR_NULLIFY_VAR(status);
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(&status, events_manager, "dispatch:afterExecuteRoute", this_ptr, value TSRMLS_CC));
if (PHALCON_IS_FALSE(status)) {
continue;
}
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {
continue;
}
/**
* Call afterDispatch
*/
RETURN_MM_ON_FAILURE(phalcon_dispatcher_fire_event(NULL, events_manager, "dispatch:afterDispatch", this_ptr, NULL TSRMLS_CC));
}
/**
* Calling afterExecuteRoute as callback and event
*/
if (phalcon_method_exists_ex(handler, SS("afterexecuteroute") TSRMLS_CC) == SUCCESS) {
PHALCON_CALL_METHOD(&status, handler, "afterexecuteroute", this_ptr, value);
if (PHALCON_IS_FALSE(status)) {
continue;
}
tmp = phalcon_fetch_nproperty_this(this_ptr, SL("_finished"), PH_NOISY TSRMLS_CC);
if (PHALCON_IS_FALSE(tmp)) {