Abstract.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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-2012 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-2012 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. * @var Zend_Controller_Front
  43. */
  44. protected $_frontController;
  45. /**
  46. * Array of invocation parameters to use when instantiating action
  47. * controllers
  48. * @var array
  49. */
  50. protected $_invokeParams = array();
  51. /**
  52. * Constructor
  53. *
  54. * @param array $params
  55. * @return void
  56. */
  57. public function __construct(array $params = array())
  58. {
  59. $this->setParams($params);
  60. }
  61. /**
  62. * Add or modify a parameter to use when instantiating an action controller
  63. *
  64. * @param string $name
  65. * @param mixed $value
  66. * @return Zend_Controller_Router
  67. */
  68. public function setParam($name, $value)
  69. {
  70. $name = (string) $name;
  71. $this->_invokeParams[$name] = $value;
  72. return $this;
  73. }
  74. /**
  75. * Set parameters to pass to action controller constructors
  76. *
  77. * @param array $params
  78. * @return Zend_Controller_Router
  79. */
  80. public function setParams(array $params)
  81. {
  82. $this->_invokeParams = array_merge($this->_invokeParams, $params);
  83. return $this;
  84. }
  85. /**
  86. * Retrieve a single parameter from the controller parameter stack
  87. *
  88. * @param string $name
  89. * @return mixed
  90. */
  91. public function getParam($name)
  92. {
  93. if(isset($this->_invokeParams[$name])) {
  94. return $this->_invokeParams[$name];
  95. }
  96. return null;
  97. }
  98. /**
  99. * Retrieve action controller instantiation parameters
  100. *
  101. * @return array
  102. */
  103. public function getParams()
  104. {
  105. return $this->_invokeParams;
  106. }
  107. /**
  108. * Clear the controller parameter stack
  109. *
  110. * By default, clears all parameters. If a parameter name is given, clears
  111. * only that parameter; if an array of parameter names is provided, clears
  112. * each.
  113. *
  114. * @param null|string|array single key or array of keys for params to clear
  115. * @return Zend_Controller_Router
  116. */
  117. public function clearParams($name = null)
  118. {
  119. if (null === $name) {
  120. $this->_invokeParams = array();
  121. } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
  122. unset($this->_invokeParams[$name]);
  123. } elseif (is_array($name)) {
  124. foreach ($name as $key) {
  125. if (is_string($key) && isset($this->_invokeParams[$key])) {
  126. unset($this->_invokeParams[$key]);
  127. }
  128. }
  129. }
  130. return $this;
  131. }
  132. /**
  133. * Retrieve Front Controller
  134. *
  135. * @return Zend_Controller_Front
  136. */
  137. public function getFrontController()
  138. {
  139. // Used cache version if found
  140. if (null !== $this->_frontController) {
  141. return $this->_frontController;
  142. }
  143. require_once 'Zend/Controller/Front.php';
  144. $this->_frontController = Zend_Controller_Front::getInstance();
  145. return $this->_frontController;
  146. }
  147. /**
  148. * Set Front Controller
  149. *
  150. * @param Zend_Controller_Front $controller
  151. * @return Zend_Controller_Router_Interface
  152. */
  153. public function setFrontController(Zend_Controller_Front $controller)
  154. {
  155. $this->_frontController = $controller;
  156. return $this;
  157. }
  158. }