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