Created
October 28, 2010 21:19
-
-
Save robzienert/652350 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Metra_Controller_Router_Route_GlamourSite extends Zend_Controller_Router_Route_Abstract | |
| { | |
| /** | |
| * @var Project_Controller_Router_Route_GlamourSite | |
| */ | |
| private static $_instance; | |
| /** | |
| * @var string | |
| */ | |
| private $_currentRoute; | |
| /** | |
| * @var Zend_Controller_Router_Route_Hostname | |
| */ | |
| private $_hostnameRoute; | |
| /** | |
| * @var Zend_Controller_Request_Abstract | |
| */ | |
| private $_request; | |
| /** | |
| * @var array | |
| */ | |
| private $_routes = array(); | |
| /** | |
| * @var array | |
| */ | |
| private $_values = array(); | |
| /** | |
| * Matches a user submitted path with parts defined by a map. Assigns and | |
| * returns an array of variables on a successful match. | |
| * | |
| * This route is a two-step process, taking design elements primarily from | |
| * the Rewrite Router, but also operates like a Chain by attaching a Hostname | |
| * route to the route. | |
| * | |
| * @param string $request | |
| * @return array|false | |
| */ | |
| public function match($request) | |
| { | |
| if (!$this->hasHostnameRoute()) { | |
| throw new RuntimeException('Cannot match request: Hostname router has not been set!'); | |
| } | |
| if (!$this->hasRoutes()) { | |
| throw new RuntimeException('Cannot match request: Routes have not been set!'); | |
| } | |
| $hostnameResult = $this->getHostnameRoute()->match($request); | |
| if (!$hostnameResult) { | |
| return false; | |
| } | |
| foreach (array_reverse($this->_routes) as $name => $route) { | |
| if (method_exists($route, 'isAbstract') && $route->isAbstract()) { | |
| continue; | |
| } | |
| if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) { | |
| $match = $request->getPathInfo(); | |
| } else { | |
| $match = $request; | |
| } | |
| if (($params = $route->match($match))) { | |
| $this->_values = $hostnameResult + $params; | |
| $this->_currentRoute = $name; | |
| return $this->_values; | |
| } | |
| } | |
| return false; | |
| } | |
| /** | |
| * Assembles user submitted parameters forming a URL path defined by this | |
| * route. | |
| * | |
| * @TODO Add support for appending the hostname router value to the assembled | |
| * route. Currently it functions as an either/or; where if a current route | |
| * is not presently set, it will return the assembled hostname route; | |
| * otherwise the current route will be assembled and returned. | |
| * | |
| * @param array $data | |
| * @param bool $reset | |
| * @param bool $encode | |
| * @return string | |
| */ | |
| public function assemble($data = array(), $reset = false, $encode = false) | |
| { | |
| if (null === $this->_currentRoute || !isset($this->_routes[$this->_currentRoute])) { | |
| $url = $this->getHostnameRoute()->assemble($data, $reset, $encode); | |
| } else { | |
| $route = $this->_routes[$this->_currentRoute]; | |
| $url = $route->assemble($data, $reset, $encode); | |
| } | |
| return $url; | |
| } | |
| public static function getInstance(Zend_Config $config) | |
| { | |
| if (null === self::$_instance) { | |
| self::$_instance = new self(); | |
| } | |
| return self::$_instance; | |
| } | |
| /** | |
| * Set the Hostname route used for the glamour site. | |
| * | |
| * @param Zend_Controller_Router_Route_Hostname $route | |
| * @return Project_Controller_Router_Route_GlamourSite | |
| */ | |
| public function setHostnameRoute(Zend_Controller_Router_Route_Hostname $route) | |
| { | |
| $this->_hostnameRoute = $route; | |
| return $this; | |
| } | |
| /** | |
| * Get the Hostname route used for this glamour site. | |
| * | |
| * @return Zend_Controller_Router_Route_Hostname | |
| */ | |
| public function getHostnameRoute() | |
| { | |
| return $this->_hostnameRoute; | |
| } | |
| /** | |
| * Returns whether or not the route has a hostname route attached. | |
| * | |
| * @return bool | |
| */ | |
| public function hasHostnameRoute() | |
| { | |
| return (bool) null !== $this->getHostnameRoute(); | |
| } | |
| /** | |
| * Sets the request object to the route; which is used as a pass-through to | |
| * the Hostname route. | |
| * | |
| * @param Zend_Controller_Request_Abstract|null $request | |
| * @return Project_Controller_Router_Route_GlamourSite | |
| */ | |
| public function setRequest($request) | |
| { | |
| $this->_request = $request; | |
| return $this; | |
| } | |
| /** | |
| * Get the request object | |
| * | |
| * @return Zend_Controller_Request_Abstract | |
| */ | |
| public function getRequest() | |
| { | |
| return $this->_request; | |
| } | |
| /** | |
| * Adds a new route to the stack | |
| * | |
| * @param string $name | |
| * @param Zend_Controller_Router_Route_Interface $route | |
| * @return Project_Controller_Router_Route_GlamourSite | |
| */ | |
| public function addRoute($name, Zend_Controller_Router_Route_Interface $route) | |
| { | |
| if (method_exists($route, 'setRequest')) { | |
| $route->setRequest($this->getRequest()); | |
| } | |
| $this->_routes[$name] = $route; | |
| return $this; | |
| } | |
| /** | |
| * Returns whether or not the route has any child routes. | |
| * | |
| * @return bool | |
| */ | |
| public function hasRoutes() | |
| { | |
| return (bool) count($this->_routes); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment