Abstract.php 4.5 KB

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