PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Controller/Router/Route/Module.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 284 lines | 150 code | 43 blank | 91 comment | 35 complexity | 856248dd771e4a6f94b8e5b61017d00c MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Router
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Module.php 24182 2011-07-03 13:43:05Z adamlundrigan $
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Controller_Router_Route_Abstract */
  23. require_once 'Zend/Controller/Router/Route/Abstract.php';
  24. /**
  25. * Module Route
  26. *
  27. * Default route for module functionality
  28. *
  29. * @package Zend_Controller
  30. * @subpackage Router
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @see http://manuals.rubyonrails.com/read/chapter/65
  34. */
  35. class Zend_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Abstract
  36. {
  37. /**
  38. * Default values for the route (ie. module, controller, action, params)
  39. * @var array
  40. */
  41. protected $_defaults;
  42. protected $_values = array();
  43. protected $_moduleValid = false;
  44. protected $_keysSet = false;
  45. /**#@+
  46. * Array keys to use for module, controller, and action. Should be taken out of request.
  47. * @var string
  48. */
  49. protected $_moduleKey = 'module';
  50. protected $_controllerKey = 'controller';
  51. protected $_actionKey = 'action';
  52. /**#@-*/
  53. /**
  54. * @var Zend_Controller_Dispatcher_Interface
  55. */
  56. protected $_dispatcher;
  57. /**
  58. * @var Zend_Controller_Request_Abstract
  59. */
  60. protected $_request;
  61. public function getVersion() {
  62. return 1;
  63. }
  64. /**
  65. * Instantiates route based on passed Zend_Config structure
  66. */
  67. public static function getInstance(Zend_Config $config)
  68. {
  69. $frontController = Zend_Controller_Front::getInstance();
  70. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  71. $dispatcher = $frontController->getDispatcher();
  72. $request = $frontController->getRequest();
  73. return new self($defs, $dispatcher, $request);
  74. }
  75. /**
  76. * Constructor
  77. *
  78. * @param array $defaults Defaults for map variables with keys as variable names
  79. * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
  80. * @param Zend_Controller_Request_Abstract $request Request object
  81. */
  82. public function __construct(array $defaults = array(),
  83. Zend_Controller_Dispatcher_Interface $dispatcher = null,
  84. Zend_Controller_Request_Abstract $request = null)
  85. {
  86. $this->_defaults = $defaults;
  87. if (isset($request)) {
  88. $this->_request = $request;
  89. }
  90. if (isset($dispatcher)) {
  91. $this->_dispatcher = $dispatcher;
  92. }
  93. }
  94. /**
  95. * Set request keys based on values in request object
  96. *
  97. * @return void
  98. */
  99. protected function _setRequestKeys()
  100. {
  101. if (null !== $this->_request) {
  102. $this->_moduleKey = $this->_request->getModuleKey();
  103. $this->_controllerKey = $this->_request->getControllerKey();
  104. $this->_actionKey = $this->_request->getActionKey();
  105. }
  106. if (null !== $this->_dispatcher) {
  107. $this->_defaults += array(
  108. $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
  109. $this->_actionKey => $this->_dispatcher->getDefaultAction(),
  110. $this->_moduleKey => $this->_dispatcher->getDefaultModule()
  111. );
  112. }
  113. $this->_keysSet = true;
  114. }
  115. /**
  116. * Matches a user submitted path. Assigns and returns an array of variables
  117. * on a successful match.
  118. *
  119. * If a request object is registered, it uses its setModuleName(),
  120. * setControllerName(), and setActionName() accessors to set those values.
  121. * Always returns the values as an array.
  122. *
  123. * @param string $path Path used to match against this routing map
  124. * @return array An array of assigned values or a false on a mismatch
  125. */
  126. public function match($path, $partial = false)
  127. {
  128. $this->_setRequestKeys();
  129. $values = array();
  130. $params = array();
  131. if (!$partial) {
  132. $path = trim($path, self::URI_DELIMITER);
  133. } else {
  134. $matchedPath = $path;
  135. }
  136. if ($path != '') {
  137. $path = explode(self::URI_DELIMITER, $path);
  138. if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
  139. $values[$this->_moduleKey] = array_shift($path);
  140. $this->_moduleValid = true;
  141. }
  142. if (count($path) && !empty($path[0])) {
  143. $values[$this->_controllerKey] = array_shift($path);
  144. }
  145. if (count($path) && !empty($path[0])) {
  146. $values[$this->_actionKey] = array_shift($path);
  147. }
  148. if ($numSegs = count($path)) {
  149. for ($i = 0; $i < $numSegs; $i = $i + 2) {
  150. $key = urldecode($path[$i]);
  151. $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
  152. $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
  153. }
  154. }
  155. }
  156. if ($partial) {
  157. $this->setMatchedPath($matchedPath);
  158. }
  159. $this->_values = $values + $params;
  160. return $this->_values + $this->_defaults;
  161. }
  162. /**
  163. * Assembles user submitted parameters forming a URL path defined by this route
  164. *
  165. * @param array $data An array of variable and value pairs used as parameters
  166. * @param bool $reset Weither to reset the current params
  167. * @return string Route path with user submitted parameters
  168. */
  169. public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
  170. {
  171. if (!$this->_keysSet) {
  172. $this->_setRequestKeys();
  173. }
  174. $params = (!$reset) ? $this->_values : array();
  175. foreach ($data as $key => $value) {
  176. if ($value !== null) {
  177. $params[$key] = $value;
  178. } elseif (isset($params[$key])) {
  179. unset($params[$key]);
  180. }
  181. }
  182. $params += $this->_defaults;
  183. $url = '';
  184. if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
  185. if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
  186. $module = $params[$this->_moduleKey];
  187. }
  188. }
  189. unset($params[$this->_moduleKey]);
  190. $controller = $params[$this->_controllerKey];
  191. unset($params[$this->_controllerKey]);
  192. $action = $params[$this->_actionKey];
  193. unset($params[$this->_actionKey]);
  194. foreach ($params as $key => $value) {
  195. $key = ($encode) ? urlencode($key) : $key;
  196. if (is_array($value)) {
  197. foreach ($value as $arrayValue) {
  198. $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
  199. $url .= self::URI_DELIMITER . $key;
  200. $url .= self::URI_DELIMITER . $arrayValue;
  201. }
  202. } else {
  203. if ($encode) $value = urlencode($value);
  204. $url .= self::URI_DELIMITER . $key;
  205. $url .= self::URI_DELIMITER . $value;
  206. }
  207. }
  208. if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
  209. if ($encode) $action = urlencode($action);
  210. $url = self::URI_DELIMITER . $action . $url;
  211. }
  212. if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
  213. if ($encode) $controller = urlencode($controller);
  214. $url = self::URI_DELIMITER . $controller . $url;
  215. }
  216. if (isset($module)) {
  217. if ($encode) $module = urlencode($module);
  218. $url = self::URI_DELIMITER . $module . $url;
  219. }
  220. return ltrim($url, self::URI_DELIMITER);
  221. }
  222. /**
  223. * Return a single parameter of route's defaults
  224. *
  225. * @param string $name Array key of the parameter
  226. * @return string Previously set default
  227. */
  228. public function getDefault($name) {
  229. if (isset($this->_defaults[$name])) {
  230. return $this->_defaults[$name];
  231. }
  232. }
  233. /**
  234. * Return an array of defaults
  235. *
  236. * @return array Route defaults
  237. */
  238. public function getDefaults() {
  239. return $this->_defaults;
  240. }
  241. }