Layout.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Controller_Action_Helper_Abstract */
  23. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  24. /**
  25. * Helper for interacting with Zend_Layout objects
  26. *
  27. * @uses Zend_Controller_Action_Helper_Abstract
  28. * @category Zend
  29. * @package Zend_Controller
  30. * @subpackage Zend_Controller_Action
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Layout_Controller_Action_Helper_Layout extends Zend_Controller_Action_Helper_Abstract
  35. {
  36. /**
  37. * @var Zend_Controller_Front
  38. */
  39. protected $_frontController;
  40. /**
  41. * @var Zend_Layout
  42. */
  43. protected $_layout;
  44. /**
  45. * @var bool
  46. */
  47. protected $_isActionControllerSuccessful = false;
  48. /**
  49. * Constructor
  50. *
  51. * @param Zend_Layout $layout
  52. * @return void
  53. */
  54. public function __construct(Zend_Layout $layout = null)
  55. {
  56. if (null !== $layout) {
  57. $this->setLayoutInstance($layout);
  58. } else {
  59. /**
  60. * @see Zend_Layout
  61. */
  62. require_once 'Zend/Layout.php';
  63. $layout = Zend_Layout::getMvcInstance();
  64. }
  65. if (null !== $layout) {
  66. $pluginClass = $layout->getPluginClass();
  67. $front = $this->getFrontController();
  68. if ($front->hasPlugin($pluginClass)) {
  69. $plugin = $front->getPlugin($pluginClass);
  70. $plugin->setLayoutActionHelper($this);
  71. }
  72. }
  73. }
  74. public function init()
  75. {
  76. $this->_isActionControllerSuccessful = false;
  77. }
  78. /**
  79. * Get front controller instance
  80. *
  81. * @return Zend_Controller_Front
  82. */
  83. public function getFrontController()
  84. {
  85. if (null === $this->_frontController) {
  86. /**
  87. * @see Zend_Controller_Front
  88. */
  89. require_once 'Zend/Controller/Front.php';
  90. $this->_frontController = Zend_Controller_Front::getInstance();
  91. }
  92. return $this->_frontController;
  93. }
  94. /**
  95. * Get layout object
  96. *
  97. * @return Zend_Layout
  98. */
  99. public function getLayoutInstance()
  100. {
  101. if (null === $this->_layout) {
  102. /**
  103. * @see Zend_Layout
  104. */
  105. require_once 'Zend/Layout.php';
  106. if (null === ($this->_layout = Zend_Layout::getMvcInstance())) {
  107. $this->_layout = new Zend_Layout();
  108. }
  109. }
  110. return $this->_layout;
  111. }
  112. /**
  113. * Set layout object
  114. *
  115. * @param Zend_Layout $layout
  116. * @return Zend_Layout_Controller_Action_Helper_Layout
  117. */
  118. public function setLayoutInstance(Zend_Layout $layout)
  119. {
  120. $this->_layout = $layout;
  121. return $this;
  122. }
  123. /**
  124. * Mark Action Controller (according to this plugin) as Running successfully
  125. *
  126. * @return Zend_Layout_Controller_Action_Helper_Layout
  127. */
  128. public function postDispatch()
  129. {
  130. $this->_isActionControllerSuccessful = true;
  131. return $this;
  132. }
  133. /**
  134. * Did the previous action successfully complete?
  135. *
  136. * @return bool
  137. */
  138. public function isActionControllerSuccessful()
  139. {
  140. return $this->_isActionControllerSuccessful;
  141. }
  142. /**
  143. * Strategy pattern; call object as method
  144. *
  145. * Returns layout object
  146. *
  147. * @return Zend_Layout
  148. */
  149. public function direct()
  150. {
  151. return $this->getLayoutInstance();
  152. }
  153. /**
  154. * Proxy method calls to layout object
  155. *
  156. * @param string $method
  157. * @param array $args
  158. * @return mixed
  159. */
  160. public function __call($method, $args)
  161. {
  162. $layout = $this->getLayoutInstance();
  163. if (method_exists($layout, $method)) {
  164. return call_user_func_array(array($layout, $method), $args);
  165. }
  166. require_once 'Zend/Layout/Exception.php';
  167. throw new Zend_Layout_Exception(sprintf("Invalid method '%s' called on layout action helper", $method));
  168. }
  169. }