File indexing completed on 2024-12-29 05:27:46
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Controller 0017 * @subpackage Plugins 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 /** Zend_Controller_Plugin_Abstract */ 0023 // require_once 'Zend/Controller/Plugin/Abstract.php'; 0024 0025 /** 0026 * Render layouts 0027 * 0028 * @uses Zend_Controller_Plugin_Abstract 0029 * @category Zend 0030 * @package Zend_Controller 0031 * @subpackage Plugins 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 * @version $Id$ 0035 */ 0036 class Zend_Layout_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract 0037 { 0038 protected $_layoutActionHelper = null; 0039 0040 /** 0041 * @var Zend_Layout 0042 */ 0043 protected $_layout; 0044 0045 /** 0046 * Constructor 0047 * 0048 * @param Zend_Layout $layout 0049 * @return void 0050 */ 0051 public function __construct(Zend_Layout $layout = null) 0052 { 0053 if (null !== $layout) { 0054 $this->setLayout($layout); 0055 } 0056 } 0057 0058 /** 0059 * Retrieve layout object 0060 * 0061 * @return Zend_Layout 0062 */ 0063 public function getLayout() 0064 { 0065 return $this->_layout; 0066 } 0067 0068 /** 0069 * Set layout object 0070 * 0071 * @param Zend_Layout $layout 0072 * @return Zend_Layout_Controller_Plugin_Layout 0073 */ 0074 public function setLayout(Zend_Layout $layout) 0075 { 0076 $this->_layout = $layout; 0077 return $this; 0078 } 0079 0080 /** 0081 * Set layout action helper 0082 * 0083 * @param Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper 0084 * @return Zend_Layout_Controller_Plugin_Layout 0085 */ 0086 public function setLayoutActionHelper(Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper) 0087 { 0088 $this->_layoutActionHelper = $layoutActionHelper; 0089 return $this; 0090 } 0091 0092 /** 0093 * Retrieve layout action helper 0094 * 0095 * @return Zend_Layout_Controller_Action_Helper_Layout 0096 */ 0097 public function getLayoutActionHelper() 0098 { 0099 return $this->_layoutActionHelper; 0100 } 0101 0102 /** 0103 * postDispatch() plugin hook -- render layout 0104 * 0105 * @param Zend_Controller_Request_Abstract $request 0106 * @return void 0107 */ 0108 public function postDispatch(Zend_Controller_Request_Abstract $request) 0109 { 0110 $layout = $this->getLayout(); 0111 $helper = $this->getLayoutActionHelper(); 0112 0113 // Return early if forward detected 0114 if (!$request->isDispatched() 0115 || $this->getResponse()->isRedirect() 0116 || ($layout->getMvcSuccessfulActionOnly() 0117 && (!empty($helper) && !$helper->isActionControllerSuccessful()))) 0118 { 0119 return; 0120 } 0121 0122 // Return early if layout has been disabled 0123 if (!$layout->isEnabled()) { 0124 return; 0125 } 0126 0127 $response = $this->getResponse(); 0128 $content = $response->getBody(true); 0129 $contentKey = $layout->getContentKey(); 0130 0131 if (isset($content['default'])) { 0132 $content[$contentKey] = $content['default']; 0133 } 0134 if ('default' != $contentKey) { 0135 unset($content['default']); 0136 } 0137 0138 $layout->assign($content); 0139 0140 $fullContent = null; 0141 $obStartLevel = ob_get_level(); 0142 try { 0143 $fullContent = $layout->render(); 0144 $response->setBody($fullContent); 0145 } catch (Exception $e) { 0146 while (ob_get_level() > $obStartLevel) { 0147 $fullContent .= ob_get_clean(); 0148 } 0149 $request->setParam('layoutFullContent', $fullContent); 0150 $request->setParam('layoutContent', $layout->content); 0151 $response->setBody(null); 0152 throw $e; 0153 } 0154 0155 } 0156 }