[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Layout.php
blob23a5754532ba4e246246a5cd79fdbdde80380c5b
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
19 * @version $Id$
22 /**
23 * Provide Layout support for MVC applications
25 * @category Zend
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
30 class Zend_Layout
32 /**
33 * Placeholder container for layout variables
34 * @var Zend_View_Helper_Placeholder_Container
36 protected $_container;
38 /**
39 * Key used to store content from 'default' named response segment
40 * @var string
42 protected $_contentKey = 'content';
44 /**
45 * Are layouts enabled?
46 * @var bool
48 protected $_enabled = true;
50 /**
51 * Helper class
52 * @var string
54 protected $_helperClass = 'Zend_Layout_Controller_Action_Helper_Layout';
56 /**
57 * Inflector used to resolve layout script
58 * @var Zend_Filter_Inflector
60 protected $_inflector;
62 /**
63 * Flag: is inflector enabled?
64 * @var bool
66 protected $_inflectorEnabled = true;
68 /**
69 * Inflector target
70 * @var string
72 protected $_inflectorTarget = ':script.:suffix';
74 /**
75 * Layout view
76 * @var string
78 protected $_layout = 'layout';
80 /**
81 * Layout view script path
82 * @var string
84 protected $_viewScriptPath = null;
86 protected $_viewBasePath = null;
87 protected $_viewBasePrefix = 'Layout_View';
89 /**
90 * Flag: is MVC integration enabled?
91 * @var bool
93 protected $_mvcEnabled = true;
95 /**
96 * Instance registered with MVC, if any
97 * @var Zend_Layout
99 protected static $_mvcInstance;
102 * Flag: is MVC successful action only flag set?
103 * @var bool
105 protected $_mvcSuccessfulActionOnly = true;
108 * Plugin class
109 * @var string
111 protected $_pluginClass = 'Zend_Layout_Controller_Plugin_Layout';
114 * @var Zend_View_Interface
116 protected $_view;
119 * View script suffix for layout script
120 * @var string
122 protected $_viewSuffix = 'phtml';
125 * Constructor
127 * Accepts either:
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
133 * required.
135 * If mvcEnabled flag is false from options, simply sets layout script path.
136 * Otherwise, also instantiates and registers action helper and controller
137 * plugin.
139 * @param string|array|Zend_Config $options
140 * @return void
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);
151 } else {
152 require_once 'Zend/Layout/Exception.php';
153 throw new Zend_Layout_Exception('Invalid option provided to constructor');
157 $this->_initVarContainer();
159 if ($initMvc) {
160 $this->_setMvcEnabled(true);
161 $this->_initMvc();
162 } else {
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;
199 * Reset MVC instance
201 * Unregisters plugins and helpers, and destroys MVC layout instance.
203 * @return void
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');
219 unset($layout);
220 self::$_mvcInstance = null;
225 * Set options en masse
227 * @param array|Zend_Config $options
228 * @return void
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
250 * @return void
252 protected function _initMvc()
254 $this->_initPlugin();
255 $this->_initHelper();
259 * Initialize front controller plugin
261 * @return void
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
284 * @return void
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());
308 return $this;
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;
338 if ($enabled) {
339 $this->enableLayout();
341 return $this;
345 * Get current layout script
347 * @return string
349 public function getLayout()
351 return $this->_layout;
355 * Disable layout
357 * @return Zend_Layout
359 public function disableLayout()
361 $this->_enabled = false;
362 return $this;
366 * Enable layout
368 * @return Zend_Layout
370 public function enableLayout()
372 $this->_enabled = true;
373 return $this;
377 * Is layout enabled?
379 * @return bool
381 public function isEnabled()
383 return $this->_enabled;
387 public function setViewBasePath($path, $prefix = 'Layout_View')
389 $this->_viewBasePath = $path;
390 $this->_viewBasePrefix = $prefix;
391 return $this;
394 public function getViewBasePath()
396 return $this->_viewBasePath;
399 public function setViewScriptPath($path)
401 $this->_viewScriptPath = $path;
402 return $this;
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
424 * @return string
426 public function getLayoutPath()
428 return $this->getViewScriptPath();
432 * Set content key
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;
442 return $this;
446 * Retrieve content key
448 * @return string
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;
464 return $this;
468 * Retrieve MVC enabled flag
470 * @return bool
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;
486 return $this;
490 * Get MVC Successful Action Only Flag
492 * @return bool
494 public function getMvcSuccessfulActionOnly()
496 return $this->_mvcSuccessfulActionOnly;
500 * Set view object
502 * @param Zend_View_Interface $view
503 * @return Zend_Layout
505 public function setView(Zend_View_Interface $view)
507 $this->_view = $view;
508 return $this;
512 * Retrieve helper class
514 * @return string
516 public function getHelperClass()
518 return $this->_helperClass;
522 * Set helper class
524 * @param string $helperClass
525 * @return Zend_Layout
527 public function setHelperClass($helperClass)
529 $this->_helperClass = (string) $helperClass;
530 return $this;
534 * Retrieve plugin class
536 * @return string
538 public function getPluginClass()
540 return $this->_pluginClass;
544 * Set plugin class
546 * @param string $pluginClass
547 * @return Zend_Layout
549 public function setPluginClass($pluginClass)
551 $this->_pluginClass = (string) $pluginClass;
552 return $this;
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);
573 return $this->_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;
585 return $this;
589 * Retrieve layout view script suffix
591 * @return string
593 public function getViewSuffix()
595 return $this->_viewSuffix;
599 * Retrieve inflector target
601 * @return string
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;
617 return $this;
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;
629 return $this;
633 * Retrieve 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;
652 * Enable inflector
654 * @return Zend_Layout
656 public function enableInflector()
658 $this->_inflectorEnabled = true;
659 return $this;
663 * Disable inflector
665 * @return Zend_Layout
667 public function disableInflector()
669 $this->_inflectorEnabled = false;
670 return $this;
674 * Return status of inflector enabled flag
676 * @return bool
678 public function inflectorEnabled()
680 return $this->_inflectorEnabled;
684 * Set layout variable
686 * @param string $key
687 * @param mixed $value
688 * @return void
690 public function __set($key, $value)
692 $this->_container[$key] = $value;
696 * Get layout variable
698 * @param string $key
699 * @return mixed
701 public function __get($key)
703 if (isset($this->_container[$key])) {
704 return $this->_container[$key];
707 return null;
711 * Is a layout variable set?
713 * @param string $key
714 * @return bool
716 public function __isset($key)
718 return (isset($this->_container[$key]));
722 * Unset a layout variable?
724 * @param string $key
725 * @return void
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);
749 return $this;
752 if (is_string($spec)) {
753 $this->_container[$spec] = $value;
754 return $this;
757 require_once 'Zend/Layout/Exception.php';
758 throw new Zend_Layout_Exception('Invalid values passed to assign()');
762 * Render layout
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'.
770 * @param mixed $name
771 * @return mixed
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);
789 } else {
790 $view->setScriptPath($path);
792 } elseif (null !== ($path = $this->getViewBasePath())) {
793 $view->addBasePath($path, $this->_viewBasePrefix);
796 return $view->render($name);