Abstract.php 4.4 KB

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