/** * {@inheritDoc} */ public function addHandler($status, $handler = null) { if (!is_null($handler)) { $this->handlers[(int) $status] = $handler; // debug message if ($this instanceof DebuggableInterface) { $this->debug(Message::get(Message::DEBUG_ADD_HANDLER, is_object($handler) ? get_class($handler) : gettype($handler), get_class($this))); } } return $this; }
/** * {@inheritDoc} */ public function addCollector(CollectorInterface $collector) { // set debug if ($this instanceof DebuggableInterface) { // debug message $this->info(Message::get(Message::DEBUG_ADD_COLLECTOR, get_class($collector))); // set collector debug mode also if ($this->getDebugMode() && $collector instanceof DebuggableInterface) { $collector->setDebugMode(true); } } $this->collectors[] = $collector; return $this; }
/** * Set collector level handler if result has no handler set yet * * @param ResultInterface $result desc * @return self * @access protected */ protected function setCollectorHandler(ResultInterface $result) { $status = $result->getStatus(); if (is_null($result->getHandler()) && $this->getHandler($status)) { // debug message $this->debug(Message::get(Message::DEBUG_SET_C_HANDLER, get_class($this), $status)); $result->setHandler($this->getHandler($status)); } return $this; }
/** * {@inheritDoc} */ public function addRoute(RouteInterface $route) { if ($route) { throw new LogicException(Message::get(Message::ROUTE_DISALLOWED, __CLASS__), Message::ROUTE_DISALLOWED); } }
/** * {@inheritDoc} */ public function runExtensions($stage, ResultInterface $result) { if ($this->sortExtensions($stage)) { foreach ($this->sorted[$stage] as $ext) { // debug message if ($this instanceof DebuggableInterface) { $this->debug(Message::get(Message::DEBUG_RUN_EXTENSION, get_class($this), is_object($ext) ? get_class($ext) : gettype($ext), $stage)); } if (false !== $ext($result, $stage)) { continue; } return false; } } return true; }
/** * {@inheritDoc} */ public function setPattern($pattern = '') { // error checking if (substr_count($pattern, '[') !== substr_count($pattern, ']') || substr_count($pattern, '{') !== substr_count($pattern, '}')) { throw new LogicException(Message::get(Message::PATTERN_MALFORMED, $pattern), Message::PATTERN_MALFORMED); } // check default values in the pattern if (false !== strpos($pattern, '=')) { $pattern = $this->extractDefaultValues($pattern); } $this->pattern = $pattern; return $this; }
/** * Fix matched placeholders, return with unique route key * * @param array $matches desc * @return array [ $name, $matches ] * @access protected */ protected function fixMatches($matches) { // remove numeric keys and empty group match foreach ($matches as $idx => $val) { if (is_int($idx) || '' === $val) { unset($matches[$idx]); } } // get route key/name $routeKey = array_keys($matches)[0]; $len = strlen($routeKey); // fix remainging match key $res = []; foreach ($matches as $key => $val) { if ($key != $routeKey) { $res[substr($key, 0, -$len)] = $val; } } // debug $this->debug(Message::get(Message::DEBUG_MATCH_REGEX, $this->regex[$routeKey])); return [$routeKey, $res]; }
/** * Fix matched placeholders, return with unique route key * * @param string $name the route key/name * @param array $matches desc * @return array [ $name, $matches ] * @access protected */ protected function fixMatches($name, array $matches) { $res = []; $map = $this->maps[$name]; foreach ($matches as $idx => $val) { if ($idx > 0 && '' !== $val) { $res[$map[$idx - 1]] = $val; } } // debug $this->debug(Message::get(Message::DEBUG_MATCH_REGEX, $this->regex[$name])); return [$name, $res]; }
/** * {@inheritDoc} */ public function addRoute(RouteInterface $route) { // generate unique key $routeKey = $this->getRouteKey($route); // related http methods $methods = $route->getMethods(); // parse pattern if not yet if (!isset($this->routes[$routeKey])) { $this->routes[$routeKey] = []; $this->parser->parse($routeKey, $route->getPattern()); } // save routes foreach ($methods as $method) { // duplication found if (isset($this->routes[$routeKey][$method])) { throw new LogicException(Message::get(Message::ROUTE_DUPLICATED, $route->getPattern(), $method), Message::ROUTE_DUPLICATED); } $this->routes[$routeKey][$method] = $route; } // debug message $this->info(Message::get(Message::DEBUG_ADD_ROUTE, $route->getPattern(), join('|', $methods))); return $this; }
/** * Execute dispatcher's default handler * * @return false * @access protected */ protected function defaultHandler() { if ($this->runExtensions(self::BEFORE_DEFAULT, $this->result)) { $status = $this->result->getStatus(); $handler = $this->getHandler($status); if ($handler) { $handler($this->result); } else { echo Message::get(Message::DEBUG_NEED_HANDLER, $status); } $this->runExtensions(self::AFTER_DEFAULT, $this->result); } return false; }