path = $path; $this->translation = ($translation === null) ? $path : $translation; $this->search = ($translation == stripslashes($path)) ? $path : $this->compile(); $this->case_sensitive = ($case_sensitive === null) ? \Config::get('routing.case_sensitive', true) : $case_sensitive; $this->strip_extension = ($strip_extension === null) ? \Config::get('routing.strip_extension', true) : $strip_extension; $this->name = $name; } /** * Compiles a route. Replaces named params and regex shortcuts. * * @return string compiled route. */ protected function compile() { if ($this->path === '_root_') { return ''; } $search = str_replace(array( ':any', ':everything', ':alnum', ':num', ':alpha', ':segment', ), array( '.+', '.*', '[[:alnum:]]+', '[[:digit:]]+', '[[:alpha:]]+', '[^/]*', ), $this->path); return preg_replace('#(?.+?)', $search); } /** * Attempts to find the correct route for the given URI * * @param \Request $request The URI object * @return array */ public function parse(\Request $request) { $uri = $request->uri->get(); $method = $request->get_method(); if ($uri === '' and $this->path === '_root_') { return $this->matched(); } $result = $this->_parse_search($uri, null, $method); if ($result) { return $result; } return false; } /** * Parses a route match and returns the controller, action and params. * * @param string $uri The matched route * @param array $named_params Named parameters * @return object $this */ public function matched($uri = '', $named_params = array()) { // Clean out all the non-named stuff out of $named_params foreach($named_params as $key => $val) { if (is_numeric($key)) { unset($named_params[$key]); } } $this->named_params = $named_params; if ($this->translation instanceof \Closure) { $this->callable = $this->translation; } else { $path = $this->translation; if ($uri != '') { // strip the extension if needed and there is something to strip if ($this->strip_extension and strrchr($uri, '.') == $ext = '.'.\Input::extension()) { $uri = substr($uri, 0, -(strlen($ext))); } if ($this->case_sensitive) { $path = preg_replace('#^'.$this->search.'$#uD', $this->translation, $uri); } else { $path = preg_replace('#^'.$this->search.'$#uiD', $this->translation, $uri); } } $this->segments = explode('/', trim($path, '/')); } return $this; } /** * Parses an actual route - extracted out of parse() to make it recursive. * * @param string $uri The URI object * @param object $route route object * @param string $method request method * @return array|boolean */ protected function _parse_search($uri, $route = null, $method = null) { if ($route === null) { $route = $this; } if (is_array($route->translation)) { foreach ($route->translation as $r) { $verb = $r[0]; $protocol = isset($r[2]) ? ($r[2] ? 'https' : 'http') : false; if (($protocol === false or $protocol == \Input::protocol()) and $method == strtoupper($verb)) { $r[1]->search = $route->search; $result = $route->_parse_search($uri, $r[1], $method); if ($result) { return $result; } } } return false; } if ($this->case_sensitive) { $result = preg_match('#^'.$route->search.'$#uD', $uri, $params); } else { $result = preg_match('#^'.$route->search.'$#uiD', $uri, $params); } if ($result === 1) { return $route->matched($uri, $params); } else { return false; } } }