1 /*
2 +----------------------------------------------------------------------+
3 | Yet Another Framework |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Xinchen Hui <laruence@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "php.h"
22
23 #include "php_yaf.h"
24
25 #include "yaf_request.h"
26 #include "yaf_namespace.h"
27 #include "yaf_exception.h"
28 #include "yaf_router.h"
29
30 #include "routes/yaf_route_interface.h"
31 #include "routes/yaf_route_static.h"
32 #include "routes/yaf_route_simple.h"
33 #include "routes/yaf_route_supervar.h"
34 #include "routes/yaf_route_regex.h"
35 #include "routes/yaf_route_rewrite.h"
36 #include "routes/yaf_route_map.h"
37
38 zend_class_entry *yaf_route_ce;
39
yaf_route_instance(yaf_route_t * route,HashTable * config)40 int yaf_route_instance(yaf_route_t *route, HashTable *config) /* {{{ */ {
41 zval *pzval;
42
43 if (UNEXPECTED(config == NULL)) {
44 return 0;
45 }
46
47 if (UNEXPECTED((pzval = zend_hash_str_find(config, ZEND_STRL("type"))) == NULL || IS_STRING != Z_TYPE_P(pzval))) {
48 return 0;
49 }
50
51 if (zend_string_equals_literal_ci(Z_STR_P(pzval), "rewrite")) {
52 zval *match, *router, *verify;
53 if (UNEXPECTED((match = zend_hash_str_find(config, ZEND_STRL("match"))) == NULL || Z_TYPE_P(match) != IS_STRING)) {
54 return 0;
55 }
56
57 if (UNEXPECTED((router = zend_hash_str_find(config, ZEND_STRL("route"))) == NULL || Z_TYPE_P(router) != IS_ARRAY)) {
58 return 0;
59 }
60
61 if (((verify = zend_hash_str_find(config, ZEND_STRL("verify"))) == NULL || Z_TYPE_P(verify) != IS_ARRAY)) {
62 verify = NULL;
63 }
64
65 yaf_route_rewrite_instance(route, Z_STR_P(match), router, verify);
66 } else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "regex")) {
67 zval *match, *router, *verify, *map, *reverse;
68 if (UNEXPECTED((match = zend_hash_str_find(config, ZEND_STRL("match"))) == NULL || Z_TYPE_P(match) != IS_STRING)) {
69 return 0;
70 }
71
72 if (UNEXPECTED((router = zend_hash_str_find(config, ZEND_STRL("route"))) == NULL || Z_TYPE_P(router) != IS_ARRAY)) {
73 return 0;
74 }
75
76 if (((map = zend_hash_str_find(config, ZEND_STRL("map"))) == NULL || Z_TYPE_P(map) != IS_ARRAY)) {
77 map = NULL;
78 }
79
80 if ((verify = zend_hash_str_find(config, ZEND_STRL("verify"))) == NULL || Z_TYPE_P(verify) != IS_ARRAY) {
81 verify = NULL;
82 }
83
84 if ((reverse = zend_hash_str_find(config, ZEND_STRL("reverse"))) == NULL || Z_TYPE_P(reverse) != IS_STRING) {
85 reverse = NULL;
86 }
87
88 yaf_route_regex_instance(route, Z_STR_P(match), router, map, verify, reverse? Z_STR_P(reverse) : NULL);
89 } else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "map")) {
90 zend_string *delimiter = NULL;
91 zend_bool ctl_prefer = 0;
92
93 if ((pzval = zend_hash_str_find(config, ZEND_STRL("controllerPrefer"))) != NULL) {
94 ctl_prefer = zend_is_true(pzval);
95 }
96
97 if ((pzval = zend_hash_str_find(config, ZEND_STRL("delimiter"))) != NULL && Z_TYPE_P(pzval) == IS_STRING) {
98 delimiter = Z_STR_P(pzval);
99 }
100
101 yaf_route_map_instance(route, ctl_prefer, delimiter);
102 } else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "simple")) {
103 zval *m, *c, *a;
104
105 if (UNEXPECTED((m = zend_hash_str_find(config, ZEND_STRL("module"))) == NULL || Z_TYPE_P(m) != IS_STRING)) {
106 return 0;
107 }
108 if (UNEXPECTED((c = zend_hash_str_find(config, ZEND_STRL("controller"))) == NULL || Z_TYPE_P(c) != IS_STRING)) {
109 return 0;
110 }
111 if (UNEXPECTED((a = zend_hash_str_find(config, ZEND_STRL("action"))) == NULL || Z_TYPE_P(a) != IS_STRING)) {
112 return 0;
113 }
114
115 yaf_route_simple_instance(route, Z_STR_P(m), Z_STR_P(c), Z_STR_P(a));
116 } else if (zend_string_equals_literal_ci(Z_STR_P(pzval), "supervar")) {
117 zval *varname;
118 if (UNEXPECTED((varname = zend_hash_str_find(config, ZEND_STRL("varname"))) == NULL || Z_TYPE_P(varname) != IS_STRING)) {
119 return 0;
120 }
121 yaf_route_supervar_instance(route, Z_STR_P(varname));
122 }
123
124 return 1;
125 }
126 /* }}} */
127
128 /** {{{ yaf_route_methods
129 */
130 zend_function_entry yaf_route_methods[] = {
131 PHP_ABSTRACT_ME(yaf_route, route, yaf_route_route_arginfo)
132 PHP_ABSTRACT_ME(yaf_route, assemble, yaf_route_assemble_arginfo)
133 {NULL, NULL, NULL}
134 };
135 /* }}} */
136
137 /** {{{ YAF_STARTUP_FUNCTION
138 */
YAF_STARTUP_FUNCTION(route)139 YAF_STARTUP_FUNCTION(route) {
140 zend_class_entry ce;
141
142 YAF_INIT_CLASS_ENTRY(ce, "Yaf_Route_Interface", "Yaf\\Route_Interface", yaf_route_methods);
143 yaf_route_ce = zend_register_internal_interface(&ce);
144
145 return SUCCESS;
146 }
147 /* }}} */
148
149 /*
150 * Local variables:
151 * tab-width: 4
152 * c-basic-offset: 4
153 * End:
154 * vim600: noet sw=4 ts=4 fdm=marker
155 * vim<600: noet sw=4 ts=4
156 */
157