ActionStack.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2015 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. /**
  23. * @see Zend_Controller_Action_Helper_Abstract
  24. */
  25. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  26. /**
  27. * Add to action stack
  28. *
  29. * @uses Zend_Controller_Action_Helper_Abstract
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage Zend_Controller_Action_Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Controller_Action_Helper_ActionStack extends Zend_Controller_Action_Helper_Abstract
  37. {
  38. /**
  39. * @var Zend_Controller_Plugin_ActionStack
  40. */
  41. protected $_actionStack;
  42. /**
  43. * Constructor
  44. *
  45. * Register action stack plugin
  46. *
  47. * @return void
  48. */
  49. public function __construct()
  50. {
  51. $front = Zend_Controller_Front::getInstance();
  52. if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack')) {
  53. /**
  54. * @see Zend_Controller_Plugin_ActionStack
  55. */
  56. require_once 'Zend/Controller/Plugin/ActionStack.php';
  57. $this->_actionStack = new Zend_Controller_Plugin_ActionStack();
  58. $front->registerPlugin($this->_actionStack, 97);
  59. } else {
  60. $this->_actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
  61. }
  62. }
  63. /**
  64. * Push onto the stack
  65. *
  66. * @param Zend_Controller_Request_Abstract $next
  67. * @return Zend_Controller_Action_Helper_ActionStack Provides a fluent interface
  68. */
  69. public function pushStack(Zend_Controller_Request_Abstract $next)
  70. {
  71. $this->_actionStack->pushStack($next);
  72. return $this;
  73. }
  74. /**
  75. * Push a new action onto the stack
  76. *
  77. * @param string $action
  78. * @param string $controller
  79. * @param string $module
  80. * @param array $params
  81. * @throws Zend_Controller_Action_Exception
  82. * @return Zend_Controller_Action_Helper_ActionStack
  83. */
  84. public function actionToStack($action, $controller = null, $module = null, array $params = array())
  85. {
  86. if ($action instanceof Zend_Controller_Request_Abstract) {
  87. return $this->pushStack($action);
  88. } elseif (!is_string($action)) {
  89. /**
  90. * @see Zend_Controller_Action_Exception
  91. */
  92. require_once 'Zend/Controller/Action/Exception.php';
  93. throw new Zend_Controller_Action_Exception('ActionStack requires either a request object or minimally a string action');
  94. }
  95. $request = $this->getRequest();
  96. if ($request instanceof Zend_Controller_Request_Abstract === false){
  97. /**
  98. * @see Zend_Controller_Action_Exception
  99. */
  100. require_once 'Zend/Controller/Action/Exception.php';
  101. throw new Zend_Controller_Action_Exception('Request object not set yet');
  102. }
  103. $controller = (null === $controller) ? $request->getControllerName() : $controller;
  104. $module = (null === $module) ? $request->getModuleName() : $module;
  105. /**
  106. * @see Zend_Controller_Request_Simple
  107. */
  108. require_once 'Zend/Controller/Request/Simple.php';
  109. $newRequest = new Zend_Controller_Request_Simple($action, $controller, $module, $params);
  110. return $this->pushStack($newRequest);
  111. }
  112. /**
  113. * Perform helper when called as $this->_helper->actionStack() from an action controller
  114. *
  115. * Proxies to {@link simple()}
  116. *
  117. * @param string $action
  118. * @param string $controller
  119. * @param string $module
  120. * @param array $params
  121. * @return boolean
  122. */
  123. public function direct($action, $controller = null, $module = null, array $params = array())
  124. {
  125. return $this->actionToStack($action, $controller, $module, $params);
  126. }
  127. }