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.
16 * @package Zend_Layout
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Provide Layout support for MVC applications
26 * @package Zend_Layout
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
33 * Placeholder container for layout variables
34 * @var Zend_View_Helper_Placeholder_Container
36 protected $_container;
39 * Key used to store content from 'default' named response segment
42 protected $_contentKey = 'content';
45 * Are layouts enabled?
48 protected $_enabled = true;
54 protected $_helperClass = 'Zend_Layout_Controller_Action_Helper_Layout';
57 * Inflector used to resolve layout script
58 * @var Zend_Filter_Inflector
60 protected $_inflector;
63 * Flag: is inflector enabled?
66 protected $_inflectorEnabled = true;
72 protected $_inflectorTarget = ':script.:suffix';
78 protected $_layout = 'layout';
81 * Layout view script path
84 protected $_viewScriptPath = null;
86 protected $_viewBasePath = null;
87 protected $_viewBasePrefix = 'Layout_View';
90 * Flag: is MVC integration enabled?
93 protected $_mvcEnabled = true;
96 * Instance registered with MVC, if any
99 protected static $_mvcInstance;
102 * Flag: is MVC successful action only flag set?
105 protected $_mvcSuccessfulActionOnly = true;
111 protected $_pluginClass = 'Zend_Layout_Controller_Plugin_Layout';
114 * @var Zend_View_Interface
119 * View script suffix for layout script
122 protected $_viewSuffix = 'phtml';
128 * - A string path to layouts
129 * - An array of options
130 * - A Zend_Config object with options
132 * Layout script path, either as argument or as key in options, is
135 * If mvcEnabled flag is false from options, simply sets layout script path.
136 * Otherwise, also instantiates and registers action helper and controller
139 * @param string|array|Zend_Config $options
142 public function __construct($options = null, $initMvc = false)
144 if (null !== $options) {
145 if (is_string($options)) {
146 $this->setLayoutPath($options);
147 } elseif (is_array($options)) {
148 $this->setOptions($options);
149 } elseif ($options instanceof Zend_Config
) {
150 $this->setConfig($options);
152 require_once 'Zend/Layout/Exception.php';
153 throw new Zend_Layout_Exception('Invalid option provided to constructor');
157 $this->_initVarContainer();
160 $this->_setMvcEnabled(true);
163 $this->_setMvcEnabled(false);
168 * Static method for initialization with MVC support
170 * @param string|array|Zend_Config $options
171 * @return Zend_Layout
173 public static function startMvc($options = null)
175 if (null === self
::$_mvcInstance) {
176 self
::$_mvcInstance = new self($options, true);
179 if (is_string($options)) {
180 self
::$_mvcInstance->setLayoutPath($options);
181 } elseif (is_array($options) ||
$options instanceof Zend_Config
) {
182 self
::$_mvcInstance->setOptions($options);
185 return self
::$_mvcInstance;
189 * Retrieve MVC instance of Zend_Layout object
191 * @return Zend_Layout|null
193 public static function getMvcInstance()
195 return self
::$_mvcInstance;
201 * Unregisters plugins and helpers, and destroys MVC layout instance.
205 public static function resetMvcInstance()
207 if (null !== self
::$_mvcInstance) {
208 $layout = self
::$_mvcInstance;
209 $pluginClass = $layout->getPluginClass();
210 $front = Zend_Controller_Front
::getInstance();
211 if ($front->hasPlugin($pluginClass)) {
212 $front->unregisterPlugin($pluginClass);
215 if (Zend_Controller_Action_HelperBroker
::hasHelper('layout')) {
216 Zend_Controller_Action_HelperBroker
::removeHelper('layout');
220 self
::$_mvcInstance = null;
225 * Set options en masse
227 * @param array|Zend_Config $options
230 public function setOptions($options)
232 if ($options instanceof Zend_Config
) {
233 $options = $options->toArray();
234 } elseif (!is_array($options)) {
235 require_once 'Zend/Layout/Exception.php';
236 throw new Zend_Layout_Exception('setOptions() expects either an array or a Zend_Config object');
239 foreach ($options as $key => $value) {
240 $method = 'set' . ucfirst($key);
241 if (method_exists($this, $method)) {
242 $this->$method($value);
248 * Initialize MVC integration
252 protected function _initMvc()
254 $this->_initPlugin();
255 $this->_initHelper();
259 * Initialize front controller plugin
263 protected function _initPlugin()
265 $pluginClass = $this->getPluginClass();
266 require_once 'Zend/Controller/Front.php';
267 $front = Zend_Controller_Front
::getInstance();
268 if (!$front->hasPlugin($pluginClass)) {
269 if (!class_exists($pluginClass)) {
270 require_once 'Zend/Loader.php';
271 Zend_Loader
::loadClass($pluginClass);
273 $front->registerPlugin(
274 // register to run last | BUT before the ErrorHandler (if its available)
275 new $pluginClass($this),
282 * Initialize action helper
286 protected function _initHelper()
288 $helperClass = $this->getHelperClass();
289 require_once 'Zend/Controller/Action/HelperBroker.php';
290 if (!Zend_Controller_Action_HelperBroker
::hasHelper('layout')) {
291 if (!class_exists($helperClass)) {
292 require_once 'Zend/Loader.php';
293 Zend_Loader
::loadClass($helperClass);
295 Zend_Controller_Action_HelperBroker
::getStack()->offsetSet(-90, new $helperClass($this));
300 * Set options from a config object
302 * @param Zend_Config $config
303 * @return Zend_Layout
305 public function setConfig(Zend_Config
$config)
307 $this->setOptions($config->toArray());
312 * Initialize placeholder container for layout vars
314 * @return Zend_View_Helper_Placeholder_Container
316 protected function _initVarContainer()
318 if (null === $this->_container
) {
319 require_once 'Zend/View/Helper/Placeholder/Registry.php';
320 $this->_container
= Zend_View_Helper_Placeholder_Registry
::getRegistry()->getContainer(__CLASS__
);
323 return $this->_container
;
327 * Set layout script to use
329 * Note: enables layout by default, can be disabled
331 * @param string $name
332 * @param boolean $enabled
333 * @return Zend_Layout
335 public function setLayout($name, $enabled = true)
337 $this->_layout
= (string) $name;
339 $this->enableLayout();
345 * Get current layout script
349 public function getLayout()
351 return $this->_layout
;
357 * @return Zend_Layout
359 public function disableLayout()
361 $this->_enabled
= false;
368 * @return Zend_Layout
370 public function enableLayout()
372 $this->_enabled
= true;
381 public function isEnabled()
383 return $this->_enabled
;
387 public function setViewBasePath($path, $prefix = 'Layout_View')
389 $this->_viewBasePath
= $path;
390 $this->_viewBasePrefix
= $prefix;
394 public function getViewBasePath()
396 return $this->_viewBasePath
;
399 public function setViewScriptPath($path)
401 $this->_viewScriptPath
= $path;
405 public function getViewScriptPath()
407 return $this->_viewScriptPath
;
411 * Set layout script path
413 * @param string $path
414 * @return Zend_Layout
416 public function setLayoutPath($path)
418 return $this->setViewScriptPath($path);
422 * Get current layout script path
426 public function getLayoutPath()
428 return $this->getViewScriptPath();
434 * Key in namespace container denoting default content
436 * @param string $contentKey
437 * @return Zend_Layout
439 public function setContentKey($contentKey)
441 $this->_contentKey
= (string) $contentKey;
446 * Retrieve content key
450 public function getContentKey()
452 return $this->_contentKey
;
456 * Set MVC enabled flag
458 * @param bool $mvcEnabled
459 * @return Zend_Layout
461 protected function _setMvcEnabled($mvcEnabled)
463 $this->_mvcEnabled
= ($mvcEnabled) ?
true : false;
468 * Retrieve MVC enabled flag
472 public function getMvcEnabled()
474 return $this->_mvcEnabled
;
478 * Set MVC Successful Action Only flag
480 * @param bool $successfulActionOnly
481 * @return Zend_Layout
483 public function setMvcSuccessfulActionOnly($successfulActionOnly)
485 $this->_mvcSuccessfulActionOnly
= ($successfulActionOnly) ?
true : false;
490 * Get MVC Successful Action Only Flag
494 public function getMvcSuccessfulActionOnly()
496 return $this->_mvcSuccessfulActionOnly
;
502 * @param Zend_View_Interface $view
503 * @return Zend_Layout
505 public function setView(Zend_View_Interface
$view)
507 $this->_view
= $view;
512 * Retrieve helper class
516 public function getHelperClass()
518 return $this->_helperClass
;
524 * @param string $helperClass
525 * @return Zend_Layout
527 public function setHelperClass($helperClass)
529 $this->_helperClass
= (string) $helperClass;
534 * Retrieve plugin class
538 public function getPluginClass()
540 return $this->_pluginClass
;
546 * @param string $pluginClass
547 * @return Zend_Layout
549 public function setPluginClass($pluginClass)
551 $this->_pluginClass
= (string) $pluginClass;
556 * Get current view object
558 * If no view object currently set, retrieves it from the ViewRenderer.
560 * @todo Set inflector from view renderer at same time
561 * @return Zend_View_Interface
563 public function getView()
565 if (null === $this->_view
) {
566 require_once 'Zend/Controller/Action/HelperBroker.php';
567 $viewRenderer = Zend_Controller_Action_HelperBroker
::getStaticHelper('viewRenderer');
568 if (null === $viewRenderer->view
) {
569 $viewRenderer->initView();
571 $this->setView($viewRenderer->view
);
577 * Set layout view script suffix
579 * @param string $viewSuffix
580 * @return Zend_Layout
582 public function setViewSuffix($viewSuffix)
584 $this->_viewSuffix
= (string) $viewSuffix;
589 * Retrieve layout view script suffix
593 public function getViewSuffix()
595 return $this->_viewSuffix
;
599 * Retrieve inflector target
603 public function getInflectorTarget()
605 return $this->_inflectorTarget
;
609 * Set inflector target
611 * @param string $inflectorTarget
612 * @return Zend_Layout
614 public function setInflectorTarget($inflectorTarget)
616 $this->_inflectorTarget
= (string) $inflectorTarget;
621 * Set inflector to use when resolving layout names
623 * @param Zend_Filter_Inflector $inflector
624 * @return Zend_Layout
626 public function setInflector(Zend_Filter_Inflector
$inflector)
628 $this->_inflector
= $inflector;
635 * @return Zend_Filter_Inflector
637 public function getInflector()
639 if (null === $this->_inflector
) {
640 require_once 'Zend/Filter/Inflector.php';
641 $inflector = new Zend_Filter_Inflector();
642 $inflector->setTargetReference($this->_inflectorTarget
)
643 ->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower')))
644 ->setStaticRuleReference('suffix', $this->_viewSuffix
);
645 $this->setInflector($inflector);
648 return $this->_inflector
;
654 * @return Zend_Layout
656 public function enableInflector()
658 $this->_inflectorEnabled
= true;
665 * @return Zend_Layout
667 public function disableInflector()
669 $this->_inflectorEnabled
= false;
674 * Return status of inflector enabled flag
678 public function inflectorEnabled()
680 return $this->_inflectorEnabled
;
684 * Set layout variable
687 * @param mixed $value
690 public function __set($key, $value)
692 $this->_container
[$key] = $value;
696 * Get layout variable
701 public function __get($key)
703 if (isset($this->_container
[$key])) {
704 return $this->_container
[$key];
711 * Is a layout variable set?
716 public function __isset($key)
718 return (isset($this->_container
[$key]));
722 * Unset a layout variable?
727 public function __unset($key)
729 if (isset($this->_container
[$key])) {
730 unset($this->_container
[$key]);
735 * Assign one or more layout variables
737 * @param mixed $spec Assoc array or string key; if assoc array, sets each
738 * key as a layout variable
739 * @param mixed $value Value if $spec is a key
740 * @return Zend_Layout
741 * @throws Zend_Layout_Exception if non-array/string value passed to $spec
743 public function assign($spec, $value = null)
745 if (is_array($spec)) {
746 $orig = $this->_container
->getArrayCopy();
747 $merged = array_merge($orig, $spec);
748 $this->_container
->exchangeArray($merged);
752 if (is_string($spec)) {
753 $this->_container
[$spec] = $value;
757 require_once 'Zend/Layout/Exception.php';
758 throw new Zend_Layout_Exception('Invalid values passed to assign()');
764 * Sets internal script path as last path on script path stack, assigns
765 * layout variables to view, determines layout name using inflector, and
766 * renders layout view script.
768 * $name will be passed to the inflector as the key 'script'.
773 public function render($name = null)
775 if (null === $name) {
776 $name = $this->getLayout();
779 if ($this->inflectorEnabled() && (null !== ($inflector = $this->getInflector())))
781 $name = $this->_inflector
->filter(array('script' => $name));
784 $view = $this->getView();
786 if (null !== ($path = $this->getViewScriptPath())) {
787 if (method_exists($view, 'addScriptPath')) {
788 $view->addScriptPath($path);
790 $view->setScriptPath($path);
792 } elseif (null !== ($path = $this->getViewBasePath())) {
793 $view->addBasePath($path, $this->_viewBasePrefix
);
796 return $view->render($name);