private function dispatch(Request $request) { $match = \Route::find($request->domain() . $request->path()); if (!$match) { // try to use fallback metcanism return false; } if (!$match->rule->isMethodAllowed($request)) { // return 405 Method Not Allowed return false; } if (!$match->rule->isSecurityPolicyMatched($request)) { // return 426 Upgrade Required (in fact, makes sense only from plain http -> tls switch) return false; } foreach ($match->rule->getBefore() as $callback) { $before_result = call_user_func(\Route::getFilter($callback), $request, $match); if ($before_result) { return $before_result; } } $action = $match->rule->getAction($request->method()); $with = $match->rule->getWith(); foreach ($match->matches as $name => $value) { $with[$name] = $value; } $callback = \resolve_callback($action, strtolower($request->method())); $result = call_user_func_array($callback, $with); foreach ($match->rule->getAfter() as $callback) { $after_result = call_user_func(\Route::getFilter($callback), $request, $result); if ($after_result) { return $after_result; } } return $result; }
/** * Get a registered filter callback. * * @param string $name * * @return \Closure * @throws \Ape\Exceptions\RouterException */ public function getFilter($name) { if (array_key_exists($name, $this->filters)) { $filter = $this->filters[$name]; $filter = resolve_callback($filter, 'filter'); return $filter; } throw new RouterException("Filter '{$name}' not found"); }
/** * Get validator for rule. * * @param string $rule * * @return callback * @throws \RuntimeException When rule doesn't exists */ public function getValidator($rule) { // do lazy initialization if (!$this->hasValidator($rule)) { // does rule have a domain? $domain = substr($rule, 0, strrpos($rule, '.')); // if rule is domain-less it will be empty string if ($domain && !isset($this->lazy_validators[$domain])) { $provider = $this->lazy_validators[$domain]; unset($this->lazy_validators[$domain]); $this->registerRulesProvider($provider, $domain, true); return $this->getValidator($rule); } throw new \RuntimeException("Validation rule '{$rule}' doesn't registered"); } return resolve_callback(array_get($this->validators, $rule)); }