/** * * Given a URI path, matches it against a rewrite rule and returns the * rewritten path. * * @param string|Solar_Uri $spec The original URI path. * * @return string The rewritten path. * */ public function match($spec) { $this->_explain = null; // pre-empt if no rules if (!$this->_rewrite) { $this->_explain = 'no rules'; return; } // convert spec to a path if ($spec instanceof Solar_Uri_Action) { $oldpath = trim($spec->getFrontPath(), '/'); } elseif ($spec instanceof Solar_Uri) { $oldpath = trim($spec->getPath()); } else { $oldpath = $spec; } // go through each of the rules to find a match foreach ($this->_rewrite as $name => $info) { if (!is_array($info)) { // shorthand format $info = array('pattern' => $name, 'rewrite' => $info, 'replace' => array(), 'default' => array()); } else { // longhand format $info += $this->_default; } // consolidate replacements $replace = $info['replace'] + $this->_replace; // convert replacements in the pattern $pattern = str_replace(array_keys($replace), array_values($replace), $info['pattern']); // trim slashes and wrap as a full regex $pattern = '#^' . trim($pattern, '/') . '$#'; // is it a match? if (preg_match($pattern, $oldpath)) { // rewrite to new path and trim slashes $rewrite = trim($info['rewrite'], '/'); $newpath = preg_replace($pattern, $rewrite, $oldpath); $this->_explain = "matched rule '{$name}'"; return trim($newpath, '/'); } } $this->_explain = 'no matches'; }
/** * * Checks the URI to see what page name and controller class we should * route to. * * @param Solar_Uri $uri The URI to route on. * * @return void * */ protected function _routing($uri) { // first path-element is the page-name. $page = strtolower(reset($uri->path)); if (empty($page)) { // page-name is blank. get the default class. // remove the empty element from the path. $class = $this->_getPageClass($this->_default); array_shift($uri->path); $this->_explain['routing_page'] = "empty, using default page '{$this->_default}'"; } elseif (in_array($page, $this->_disable)) { // page-name is disabled. get the default class. // leave existing elements in the path. $class = $this->_getPageClass($this->_default); $this->_explain['routing_page'] = "'{$page}' disabled, using default page '{$this->_default}'"; } else { // look for a controller for the requested page. $class = $this->_getPageClass($page); if (!$class) { // no class for the page-name. get the default class. // leave existing elements in the path. $class = $this->_getPageClass($this->_default); $this->_explain['routing_page'] = "no class for page '{$page}', using default page '{$this->_default}'"; } else { // found a class. don't need the page-name any more, so // remove it from the path. array_shift($uri->path); $this->_explain['routing_page'] = $page; } } $this->_explain['routing_class'] = $class; $this->_explain['routing_uri'] = $uri->getPath(); return array($page, $class); }