2
0

Abstract.php 4.4 KB

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