00001 <?php
00002
00003 require_once dirname(__FILE__) . '/routebase.cls.php';
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 class ParameterizedRoute extends RouteBase {
00094
00095
00096
00097
00098
00099
00100
00101
00102 private $params = array();
00103
00104
00105
00106
00107
00108
00109
00110
00111 private $types = array();
00112
00113
00114
00115
00116
00117 private static $handlers = array();
00118
00119
00120
00121
00122 public function weight_against_path($path) {
00123 $def_pathstack = new PathStack($this->path);
00124 $url_pathstack = new PathStack($path);
00125
00126 $ret = self::WEIGHT_NO_MATCH;
00127
00128 $simple_check = true;
00129 $cf_delta = $url_pathstack->count_front() - $def_pathstack->count_front();
00130 $lastchar = substr(rtrim($this->path, '/'), -1);
00131 switch ($lastchar) {
00132 case '!':
00133 $simple_check = ($cf_delta == 0 || $cf_delta == -1);
00134 break;
00135 case '%':
00136 $simple_check = ($cf_delta >= 0);
00137 break;
00138 case '*':
00139 $simple_check = ($cf_delta >= -1);
00140 break;
00141 default:
00142 $simple_check = ($cf_delta == 0);
00143 break;
00144 }
00145
00146 if ($simple_check) {
00147 $ret = self::WEIGHT_FULL_MATCH;
00148 $count = 0;
00149 while ($def_elem = $def_pathstack->shift()) {
00150 $count++;
00151 $url_elem = '';
00152 switch (substr($def_elem, -1)) {
00153 case '*':
00154 case '%':
00155
00156 $url_elem = $url_pathstack->implode_front();
00157 $url_pathstack->clear_front();
00158 break;
00159 default:
00160 $url_elem = $url_pathstack->shift();
00161 break;
00162 }
00163
00164 if ($this->validate($def_elem, $url_elem) == false) {
00165 $ret = self::WEIGHT_NO_MATCH;
00166 break;
00167 }
00168 }
00169 }
00170
00171 return $ret;
00172 }
00173
00174
00175
00176
00177
00178
00179 protected function initialize_adjust_path($page_data) {
00180
00181 $page_data->get_pathstack()->clear_front();
00182 }
00183
00184
00185
00186
00187
00188
00189 protected function get_types() {
00190 return $this->types;
00191 }
00192
00193
00194
00195
00196
00197
00198 protected function get_handler($type) {
00199 return Arr::get_item(self::$handlers, $type, false);
00200 }
00201
00202
00203
00204
00205
00206
00207 protected function get_handler_for_key($key) {
00208 $types = $this->get_types();
00209 $type = Arr::get_item($types, $key, 's');
00210 return $this->get_handler($type);
00211 }
00212
00213
00214
00215
00216 private function add_value_and_type($name, $value, $type) {
00217 $this->params[$name] = $value;
00218 $this->types[$name] = $type;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 private function validate($this_path_elem, $element_to_validate) {
00230
00231 if ($this_path_elem == '*') {
00232 return true;
00233 }
00234 if (strpos($this_path_elem, '{') === false) {
00235 return ($this_path_elem == $element_to_validate);
00236 }
00237 $optional = (substr($this_path_elem, -1) === '!');
00238 if ($optional && $element_to_validate === false) {
00239 return true;
00240 }
00241
00242 $tag = '#\{[^\}]*\}#';
00243
00244 $texts = preg_split($tag, $this_path_elem, -1);
00245
00246 $tags = '';
00247 preg_match_all($tag, $this_path_elem, $tags);
00248 $tags = $tags[0];
00249
00250
00251
00252 $names = array();
00253 $types = array();
00254 $regexp = $this->build_validate_regexp($texts, $tags, $names, $types);
00255 $matches = array();
00256 if (preg_match($regexp, $element_to_validate, $matches)) {
00257 if (count($matches) == count($names) + 1) {
00258 foreach($names as $key => $name) {
00259 $this->add_value_and_type($name, $matches[$key + 1], array_shift($types));
00260 }
00261 return true;
00262 }
00263 }
00264 return false;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 private function build_validate_regexp($arr_texts, $arr_expr, &$arr_names, &$arr_types) {
00277 $ret = '';
00278 $c = count($arr_expr);
00279 for($i = 0; $i < $c; $i++) {
00280 $ret .= preg_quote(array_shift($arr_texts));
00281 $expr = trim($arr_expr[$i], '{}');
00282 $name = '';
00283 $reg_ex = $this->build_expresssion_regexp($expr, $name, $type);
00284 $ret .= '(' . $reg_ex . ')';
00285 $arr_names[] = $name;
00286 $arr_types[] = $type;
00287 }
00288 $last = array_shift($arr_texts);
00289 switch ($last) {
00290 case '*':
00291 case '%':
00292 case '!':
00293 break;
00294 default:
00295 $ret .= preg_quote($last);
00296 break;
00297 }
00298 $ret = '#^' . $ret . '$#';
00299 return $ret;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309 private function build_expresssion_regexp($expression, &$name, &$type) {
00310 $arr_expression = explode(':', $expression);
00311 $name = Arr::get_item($arr_expression, 0, '');
00312 $type = Arr::get_item($arr_expression, 1, 's');
00313 $params = Arr::get_item($arr_expression, 2, false);
00314 if (empty($name)) {
00315 throw new Exception(tr('Illegal expression name found: %i', 'core', array('%i' => $expression)));
00316 }
00317
00318 $handler = $this->get_handler($type);
00319 if ($handler) {
00320 return $handler->get_validate_regex($params);
00321 }
00322 else {
00323 return $this->build_unknown_type_regexp($type, $params);
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334 protected function build_unknown_type_regexp($type, $params) {
00335 throw new Exception(tr('Illegal expression type found: %t', 'core', array('%t' => $type)));
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 protected function invoke_action_func($controller, $funcname, $page_data) {
00348 $this->check_action_func($controller, $funcname);
00349 return $this->call_user_func_named($controller, $funcname, $page_data, $this->params);
00350 }
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361 protected function call_user_func_named($controller, $function, $page_data, $params) {
00362 $reflect = new ReflectionMethod($controller, $function);
00363 $params['page_data'] = $page_data;
00364 $real_params = array();
00365 foreach ($reflect->getParameters() as $i => $param) {
00366 $pname = $param->getName();
00367 if (array_key_exists($pname, $params)) {
00368 $real_params[] = $params[$pname];
00369 }
00370 else if ($param->isDefaultValueAvailable()) {
00371 $real_params[] = $param->getDefaultValue();
00372 }
00373 else {
00374 throw new Exception('Call to function ' . $function . ' missing parameter ' . $pname);
00375 }
00376 }
00377 return call_user_func_array(array($controller, $function), $real_params);
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 protected function build_url_path($params) {
00391 $path = $this->path;
00392 if (is_array($params) || is_object($params)) {
00393 foreach($params as $key => $value) {
00394 $path = $this->replace_path_variable($path, $key, $value);
00395 }
00396 }
00397 if (is_object($params)) {
00398 $path = $this->replace_path_variable($path, '_class_', get_class($params));
00399 if ($params instanceof IDBTable) {
00400 $path = $this->replace_path_variable($path, '_model_', $params->get_table_name());
00401 }
00402 }
00403
00404
00405 $reg_optional_asterix = '#\{[^\}]*\}\*#';
00406 $reg_optional_exklamation = '#\{[^\}]*\}\!#';
00407 $path = preg_replace($reg_optional_asterix, '', $path);
00408 $path = preg_replace($reg_optional_exklamation, '', $path);
00409
00410 return $path;
00411 }
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 protected function replace_path_variable($path, $key, $value) {
00422 if (is_object($value) || is_array($value)) {
00423 return $path;
00424 }
00425
00426
00427
00428 $path = str_replace('{' . $key . '}', '{' . $key . ':s}', $path);
00429
00430
00431 $reg = '#\{' . $key . ':([^:\}]*)[:]?.*?\}#';
00432 $matches = array();
00433 if (preg_match($reg, $path, $matches)) {
00434 $handler = $this->get_handler($matches[1]);
00435 if ($handler) {
00436 $value = $handler->preprocess_build_url($value);
00437 }
00438 }
00439
00440 $reg = '#\{' . $key . ':[^}]*\}[*%]#';
00441 $replace = implode('/', array_map(array($this, 'preprocess_replace_value'), explode('/', Cast::string($value))));
00442 $path = preg_replace($reg, $replace, $path);
00443 $reg = '#\{' . $key . ':[^}]*\}!?#';
00444 $replace = $this->preprocess_replace_value($value);
00445 $path = preg_replace($reg, $replace, $path);
00446
00447 return $path;
00448 }
00449
00450
00451
00452
00453 protected function preprocess_replace_value($value) {
00454 return str_replace(array('%2F', '%3F', '%26'), array('%252F', '%253F', '%2526'), urlencode(Cast::string($value)));
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464 public static function init_handlers() {
00465 $files = Load::get_files_from_directory('controller/base/routes/parameterizedroutehandlers', '*.handler.php');
00466 foreach($files as $file => $inc) {
00467 include_once $inc;
00468 $cls = Load::filename_to_classname($file, 'ParameterizedRouteHandler', 'handler');
00469 if (!class_exists($cls)) {
00470 throw new Exception('ParameterizedRoute: ' . $inc . ' does not define class ' . $cls);
00471 }
00472 self::add_handler(new $cls());
00473 }
00474 }
00475
00476
00477
00478
00479 public static function add_handler(IParameterizedRouteHandler $handler) {
00480 self::$handlers[$handler->get_type_key()] = $handler;
00481 }
00482 }
00483
00484 ParameterizedRoute::init_handlers();