123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php
- namespace Cube\Controller\Router;
- use Cube\Controller\Request\AbstractRequest,
- Cube\Controller\Router\Route\RouteInterface,
- Cube\Controller\Front;
- abstract class AbstractRouter
- {
- const URI_DELIMITER = '/';
- const DEFAULT_CONTROLLER = 'Index';
- const DEFAULT_ACTION = 'Index';
-
- protected $_routes = array();
-
- protected $_request;
-
- protected $_routeClass;
-
- protected $_cache = false;
-
- public function __construct(array $routes = array())
- {
- $this->_cache = Front::getInstance()->getBootstrap()->getResource('cache');
- if (!empty($routes)) {
- $this->addRoutes($routes);
- }
- }
-
- public function addRoute($route)
- {
- if ($route instanceof 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 setRequest(AbstractRequest $request)
- {
- $this->_request = $this->route($request);
- return $this;
- }
-
- public function getRequest()
- {
- if (!$this->_request instanceof AbstractRequest) {
- $this->setRequest(
- new \Cube\Controller\Request());
- }
- return $this->_request;
- }
-
- protected function _setRouteFromArray(array $route)
- {
- if (!empty($route[0])) {
- return new $this->_routeClass($route[0], $route[1], $route[2]);
- }
- return false;
- }
-
- protected function _getDefaultRoute(AbstractRequest $request)
- {
- $parts = (array)$request->filterInput(array_filter(
- explode(self::URI_DELIMITER, $request->getRequestUri())
- ));
- $modules = (array)Front::getInstance()->getOption('modules');
- $part = ucfirst(array_shift($parts));
- if (in_array($part, $modules)) {
- $request->setModule(strtolower($part));
- $controller = array_shift($parts);
- }
- else {
- $request->setModule($modules[0]);
- $controller = $part;
- }
- $request->setController(
- (!empty($controller)) ? strtolower($controller) : self::DEFAULT_CONTROLLER);
- $action = array_shift($parts);
- $request->setAction(
- (!empty($action)) ? $action : self::DEFAULT_ACTION);
- while ($key = array_shift($parts)) {
- $value = array_shift($parts);
- $request->setParam($key, $value)
- ->setQuery($key, $value);
- }
- return $request;
- }
-
- protected function _getDefaultParams(array $params = null, $addGetParams = false, array $skipParams = null)
- {
- $data = array();
- $this->setRequest(
- new \Cube\Controller\Request());
- $request = $this->getRequest();
- $modules = (array)Front::getInstance()->getOption('modules');
- $module = $request->normalize(
- (isset($params['module'])) ? $params['module'] : $request->getModule(), true);
- $defaultModule = $modules[0];
- if ($module != $defaultModule) {
- $data['module'] = $module;
- }
- $data['controller'] = $request->normalize(
- (isset($params['controller'])) ? $params['controller'] : $request->getController(), true);
- $data['action'] = $request->normalize(
- (isset($params['action'])) ? $params['action'] : $request->getAction(), true);
- if ($addGetParams === true) {
- $params = array_merge((array)$request->getQuery(), (array)$params);
- }
- foreach ((array)$params as $key => $value) {
- if (!in_array($key, array('module', 'controller', 'action')) && !empty($value)) {
- $data[$key] = $value;
- }
- }
- foreach ((array)$skipParams as $key) {
- if (array_key_exists($key, $data)) {
- unset($data[$key]);
- }
- }
- return $data;
- }
-
- protected function _getCache($key = null)
- {
- if ($this->_cache !== false) {
- if ($key === null) {
- return $this->_cache;
- }
- else {
- $methodName = 'get' . ucfirst($key);
- if (method_exists($this->_cache, $methodName)) {
- return $this->_cache->$methodName();
- }
- }
- }
- return false;
- }
-
- abstract public function route(AbstractRequest $request);
-
- abstract public function assemble($params, $name = null, $addBaseUrl = true, $addGetParams = false, array $skipParams = null);
- }
|