PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/zend/Zend/Layout/Controller/Plugin/Layout.php

http://zoop.googlecode.com/
PHP | 156 lines | 70 code | 16 blank | 70 comment | 11 complexity | abdcd487224cb3b399ac74aec6319a13 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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 Plugins
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Plugin_Abstract */
  22. require_once 'Zend/Controller/Plugin/Abstract.php';
  23. /**
  24. * Render layouts
  25. *
  26. * @uses Zend_Controller_Plugin_Abstract
  27. * @category Zend
  28. * @package Zend_Controller
  29. * @subpackage Plugins
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @version $Id: Layout.php 20096 2010-01-06 02:05:09Z bkarwin $
  33. */
  34. class Zend_Layout_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
  35. {
  36. protected $_layoutActionHelper = null;
  37. /**
  38. * @var Zend_Layout
  39. */
  40. protected $_layout;
  41. /**
  42. * Constructor
  43. *
  44. * @param Zend_Layout $layout
  45. * @return void
  46. */
  47. public function __construct(Zend_Layout $layout = null)
  48. {
  49. if (null !== $layout) {
  50. $this->setLayout($layout);
  51. }
  52. }
  53. /**
  54. * Retrieve layout object
  55. *
  56. * @return Zend_Layout
  57. */
  58. public function getLayout()
  59. {
  60. return $this->_layout;
  61. }
  62. /**
  63. * Set layout object
  64. *
  65. * @param Zend_Layout $layout
  66. * @return Zend_Layout_Controller_Plugin_Layout
  67. */
  68. public function setLayout(Zend_Layout $layout)
  69. {
  70. $this->_layout = $layout;
  71. return $this;
  72. }
  73. /**
  74. * Set layout action helper
  75. *
  76. * @param Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper
  77. * @return Zend_Layout_Controller_Plugin_Layout
  78. */
  79. public function setLayoutActionHelper(Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper)
  80. {
  81. $this->_layoutActionHelper = $layoutActionHelper;
  82. return $this;
  83. }
  84. /**
  85. * Retrieve layout action helper
  86. *
  87. * @return Zend_Layout_Controller_Action_Helper_Layout
  88. */
  89. public function getLayoutActionHelper()
  90. {
  91. return $this->_layoutActionHelper;
  92. }
  93. /**
  94. * postDispatch() plugin hook -- render layout
  95. *
  96. * @param Zend_Controller_Request_Abstract $request
  97. * @return void
  98. */
  99. public function postDispatch(Zend_Controller_Request_Abstract $request)
  100. {
  101. $layout = $this->getLayout();
  102. $helper = $this->getLayoutActionHelper();
  103. // Return early if forward detected
  104. if (!$request->isDispatched()
  105. || $this->getResponse()->isRedirect()
  106. || ($layout->getMvcSuccessfulActionOnly()
  107. && (!empty($helper) && !$helper->isActionControllerSuccessful())))
  108. {
  109. return;
  110. }
  111. // Return early if layout has been disabled
  112. if (!$layout->isEnabled()) {
  113. return;
  114. }
  115. $response = $this->getResponse();
  116. $content = $response->getBody(true);
  117. $contentKey = $layout->getContentKey();
  118. if (isset($content['default'])) {
  119. $content[$contentKey] = $content['default'];
  120. }
  121. if ('default' != $contentKey) {
  122. unset($content['default']);
  123. }
  124. $layout->assign($content);
  125. $fullContent = null;
  126. $obStartLevel = ob_get_level();
  127. try {
  128. $fullContent = $layout->render();
  129. $response->setBody($fullContent);
  130. } catch (Exception $e) {
  131. while (ob_get_level() > $obStartLevel) {
  132. $fullContent .= ob_get_clean();
  133. }
  134. $request->setParam('layoutFullContent', $fullContent);
  135. $request->setParam('layoutContent', $layout->content);
  136. $response->setBody(null);
  137. throw $e;
  138. }
  139. }
  140. }