123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- namespace Cube\Controller\Router;
- use Cube\Controller\Request\AbstractRequest,
- Cube\Controller\Front,
- Cube\Cache\Adapter\AbstractAdapter as CacheAdapter;
- class Rewrite extends AbstractRouter
- {
- const URI_DELIMITER = '/';
- const DEFAULT_CONTROLLER = 'Index';
- const DEFAULT_ACTION = 'Index';
-
- protected $_routes = array();
-
- protected $_routeClass = '\Cube\Controller\Router\Route\Rewrite';
-
- protected $_cacheableParams = array(
- 'module', 'controller', 'action'
- );
-
- public function addRoute($route)
- {
- if ($route instanceof Route\RouteInterface) {
- $this->_routes[] = $route;
- }
- else if (is_array($route)) {
- $route = $this->_setRouteFromArray($route);
- if ($route !== false) {
- $this->_routes[] = $route;
- }
- }
- else {
- throw new \BadMethodCallException('The route object must be an instance of Cube\Controller\Router\Route\AbstractRoute or an array');
- }
- return $this;
- }
-
- public function addRoutes(array $routes)
- {
- foreach ($routes as $route) {
- $this->addRoute($route);
- }
- return $this;
- }
-
- public function getRoutes()
- {
- return $this->_routes;
- }
-
- public function getRoute($name)
- {
- foreach ($this->_routes as $route) {
-
- if ($route->getName() == $name) {
- return $route;
- }
- }
- throw new \OutOfBoundsException(
- sprintf("The route named '%s' does not exist.", $name));
- }
-
- public function getCacheableParams()
- {
- return $this->_cacheableParams;
- }
-
- public function setCacheableParams($cacheableParams)
- {
- $this->_cacheableParams = $cacheableParams;
- return $this;
- }
-
- public function route(AbstractRequest $request)
- {
- $requestUri = $request->getRequestUri();
- $matched = false;
- foreach ($this->_routes as $route) {
-
- if ($route->match($requestUri) === true) {
- $params = array_merge($route->getDefaults(), $route->getParams());
- $request->setModule($route->getModule())
- ->setController($route->getController())
- ->setAction($route->getAction())
- ->setParams($params)
- ->setQuery($params);
- $matched = true;
- }
- }
- if ($matched === false) {
- $request = $this->_getDefaultRoute($request);
- }
- return $request;
- }
-
- public function assemble($params, $name = null, $addBaseUrl = true, $addGetParams = false, array $skipParams = null)
- {
- $cacheRoutes = false;
- $cacheFile = null;
- $url = null;
-
-
- if ($name !== null) {
- $route = $this->getRoute($name);
- $url = $route->assemble($params, true);
- }
- else if (is_string($params)) {
- $url = $params;
- }
-
- if ($url === null) {
- $paramsKeys = array_keys((array)$params);
- $params = $this->_getDefaultParams($params, $addGetParams, $skipParams);
- if (count(array_intersect($paramsKeys, $this->_cacheableParams)) > 0) {
- $cacheFileName = json_encode(array(
- $params, $name, $addBaseUrl, $addGetParams, $skipParams
- ));
-
- $cacheRoutes = $this->_getCache('cacheRoutes');
- if ($cacheRoutes !== false) {
- $cacheFile = md5($cacheFileName);
- if (($cachedUrl = $this->_cache->read($cacheFile, CacheAdapter::ROUTES)) !== false) {
- return $cachedUrl;
- }
- }
- }
-
- foreach ($this->_routes as $route) {
-
- $assembled = $route->assemble($params);
- if ($assembled !== null) {
- $url = $assembled;
- }
- }
- }
- if (!isset($url)) {
- $url = $this->_defaultRouteAssemble($params);
- }
- if (!preg_match('#^[a-z]+://#', $url)) {
- $url = (($addBaseUrl) ? Front::getInstance()->getRequest()->getBaseUrl() : '')
- . self::URI_DELIMITER
- . ltrim($url, self::URI_DELIMITER);
- }
-
- if (!empty($url) && $cacheRoutes !== false) {
- $this->_cache->write($cacheFile, CacheAdapter::ROUTES, $url);
- }
- return $url;
- }
-
- protected function _setRouteFromArray(array $route)
- {
- if (!empty($route[0])) {
- return new Route\Rewrite($route[0], $route[1], $route[2]);
- }
- return false;
- }
-
- protected function _defaultRouteAssemble(array $params = null)
- {
- $url = array();
- $get = array();
- foreach ((array)$params as $key => $value) {
- if (preg_match('#^[a-zA-Z0-9_-]+$#', $key)) {
- if (!is_array($value)) {
- if (!in_array($key, array('module', 'controller', 'action'))) {
- array_push($url, $key);
- }
- array_push($url, $value);
- }
- else {
- foreach ((array)$value as $val) {
- if (!empty($val)) {
- $get[] = $key . '[]=' . $val;
- }
- }
- }
- }
- }
- $uri = implode(self::URI_DELIMITER, $url);
- if (count($get) > 0) {
- $uri .= self::URI_DELIMITER . '?' . implode('&', $get);
- }
- return $uri;
- }
- }
|