Abstract.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Controller_Router_Interface */
  23. require_once 'Zend/Controller/Router/Interface.php';
  24. /**
  25. * Simple first implementation of a router, to be replaced
  26. * with rules-based URI processor.
  27. *
  28. * @category Zend
  29. * @package Zend_Controller
  30. * @subpackage Router
  31. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Controller_Router_Abstract implements Zend_Controller_Router_Interface
  35. {
  36. /**
  37. * URI delimiter
  38. */
  39. const URI_DELIMITER = '/';
  40. /**
  41. * Front controller instance
  42. *
  43. * @var Zend_Controller_Front
  44. */
  45. protected $_frontController;
  46. /**
  47. * Array of invocation parameters to use when instantiating action
  48. * controllers
  49. *
  50. * @var array
  51. */
  52. protected $_invokeParams = array();
  53. /**
  54. * Constructor
  55. *
  56. * @param array $params
  57. */
  58. public function __construct(array $params = array())
  59. {
  60. $this->setParams($params);
  61. }
  62. /**
  63. * Add or modify a parameter to use when instantiating an action controller
  64. *
  65. * @param string $name
  66. * @param mixed $value
  67. * @return Zend_Controller_Router_Abstract
  68. */
  69. public function setParam($name, $value)
  70. {
  71. $name = (string)$name;
  72. $this->_invokeParams[$name] = $value;
  73. return $this;
  74. }
  75. /**
  76. * Set parameters to pass to action controller constructors
  77. *
  78. * @param array $params
  79. * @return Zend_Controller_Router_Abstract
  80. */
  81. public function setParams(array $params)
  82. {
  83. $this->_invokeParams = array_merge($this->_invokeParams, $params);
  84. return $this;
  85. }
  86. /**
  87. * Retrieve a single parameter from the controller parameter stack
  88. *
  89. * @param string $name
  90. * @return mixed
  91. */
  92. public function getParam($name)
  93. {
  94. if (isset($this->_invokeParams[$name])) {
  95. return $this->_invokeParams[$name];
  96. }
  97. return null;
  98. }
  99. /**
  100. * Retrieve action controller instantiation parameters
  101. *
  102. * @return array
  103. */
  104. public function getParams()
  105. {
  106. return $this->_invokeParams;
  107. }
  108. /**
  109. * Clear the controller parameter stack
  110. *
  111. * By default, clears all parameters. If a parameter name is given, clears
  112. * only that parameter; if an array of parameter names is provided, clears
  113. * each.
  114. *
  115. * @param null|string|array single key or array of keys for params to clear
  116. * @return Zend_Controller_Router_Abstract
  117. */
  118. public function clearParams($name = null)
  119. {
  120. if (null === $name) {
  121. $this->_invokeParams = array();
  122. } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
  123. unset($this->_invokeParams[$name]);
  124. } elseif (is_array($name)) {
  125. foreach ($name as $key) {
  126. if (is_string($key) && isset($this->_invokeParams[$key])) {
  127. unset($this->_invokeParams[$key]);
  128. }
  129. }
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Retrieve Front Controller
  135. *
  136. * @return Zend_Controller_Front
  137. */
  138. public function getFrontController()
  139. {
  140. // Used cache version if found
  141. if (null !== $this->_frontController) {
  142. return $this->_frontController;
  143. }
  144. require_once 'Zend/Controller/Front.php';
  145. $this->_frontController = Zend_Controller_Front::getInstance();
  146. return $this->_frontController;
  147. }
  148. /**
  149. * Set Front Controller
  150. *
  151. * @param Zend_Controller_Front $controller
  152. * @return Zend_Controller_Router_Interface
  153. */
  154. public function setFrontController(Zend_Controller_Front $controller)
  155. {
  156. $this->_frontController = $controller;
  157. return $this;
  158. }
  159. }