00001 <?php
00030 class Zend_Layout
00031 {
00036 protected $_container;
00037
00042 protected $_contentKey = 'content';
00043
00048 protected $_enabled = true;
00049
00054 protected $_helperClass = 'Zend_Layout_Controller_Action_Helper_Layout';
00055
00060 protected $_inflector;
00061
00066 protected $_inflectorEnabled = true;
00067
00072 protected $_inflectorTarget = ':script.:suffix';
00073
00078 protected $_layout = 'layout';
00079
00084 protected $_viewScriptPath = null;
00085
00086 protected $_viewBasePath = null;
00087 protected $_viewBasePrefix = 'Layout_View';
00088
00093 protected $_mvcEnabled = true;
00094
00099 protected static $_mvcInstance;
00100
00105 protected $_mvcSuccessfulActionOnly = true;
00106
00111 protected $_pluginClass = 'Zend_Layout_Controller_Plugin_Layout';
00112
00116 protected $_view;
00117
00122 protected $_viewSuffix = 'phtml';
00123
00142 public function __construct($options = null, $initMvc = false)
00143 {
00144 if (null !== $options) {
00145 if (is_string($options)) {
00146 $this->setLayoutPath($options);
00147 } elseif (is_array($options)) {
00148 $this->setOptions($options);
00149 } elseif ($options instanceof Zend_Config) {
00150 $this->setConfig($options);
00151 } else {
00152 require_once 'Zend/Layout/Exception.php';
00153 throw new Zend_Layout_Exception('Invalid option provided to constructor');
00154 }
00155 }
00156
00157 $this->_initVarContainer();
00158
00159 if ($initMvc) {
00160 $this->_setMvcEnabled(true);
00161 $this->_initMvc();
00162 } else {
00163 $this->_setMvcEnabled(false);
00164 }
00165 }
00166
00173 public static function startMvc($options = null)
00174 {
00175 if (null === self::$_mvcInstance) {
00176 self::$_mvcInstance = new self($options, true);
00177 }
00178
00179 if (is_string($options)) {
00180 self::$_mvcInstance->setLayoutPath($options);
00181 } elseif (is_array($options) || $options instanceof Zend_Config) {
00182 self::$_mvcInstance->setOptions($options);
00183 }
00184
00185 return self::$_mvcInstance;
00186 }
00187
00193 public static function getMvcInstance()
00194 {
00195 return self::$_mvcInstance;
00196 }
00197
00205 public static function resetMvcInstance()
00206 {
00207 if (null !== self::$_mvcInstance) {
00208 $layout = self::$_mvcInstance;
00209 $pluginClass = $layout->getPluginClass();
00210 $front = Zend_Controller_Front::getInstance();
00211 if ($front->hasPlugin($pluginClass)) {
00212 $front->unregisterPlugin($pluginClass);
00213 }
00214
00215 if (Zend_Controller_Action_HelperBroker::hasHelper('layout')) {
00216 Zend_Controller_Action_HelperBroker::removeHelper('layout');
00217 }
00218
00219 unset($layout);
00220 self::$_mvcInstance = null;
00221 }
00222 }
00223
00230 public function setOptions($options)
00231 {
00232 if ($options instanceof Zend_Config) {
00233 $options = $options->toArray();
00234 } elseif (!is_array($options)) {
00235 require_once 'Zend/Layout/Exception.php';
00236 throw new Zend_Layout_Exception('setOptions() expects either an array or a Zend_Config object');
00237 }
00238
00239 foreach ($options as $key => $value) {
00240 $method = 'set' . ucfirst($key);
00241 if (method_exists($this, $method)) {
00242 $this->$method($value);
00243 }
00244 }
00245 }
00246
00252 protected function _initMvc()
00253 {
00254 $this->_initPlugin();
00255 $this->_initHelper();
00256 }
00257
00263 protected function _initPlugin()
00264 {
00265 $pluginClass = $this->getPluginClass();
00266 require_once 'Zend/Controller/Front.php';
00267 $front = Zend_Controller_Front::getInstance();
00268 if (!$front->hasPlugin($pluginClass)) {
00269 if (!class_exists($pluginClass)) {
00270 require_once 'Zend/Loader.php';
00271 Zend_Loader::loadClass($pluginClass);
00272 }
00273 $front->registerPlugin(
00274
00275 new $pluginClass($this),
00276 99
00277 );
00278 }
00279 }
00280
00286 protected function _initHelper()
00287 {
00288 $helperClass = $this->getHelperClass();
00289 require_once 'Zend/Controller/Action/HelperBroker.php';
00290 if (!Zend_Controller_Action_HelperBroker::hasHelper('layout')) {
00291 if (!class_exists($helperClass)) {
00292 require_once 'Zend/Loader.php';
00293 Zend_Loader::loadClass($helperClass);
00294 }
00295 Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-90, new $helperClass($this));
00296 }
00297 }
00298
00305 public function setConfig(Zend_Config $config)
00306 {
00307 $this->setOptions($config->toArray());
00308 return $this;
00309 }
00310
00316 protected function _initVarContainer()
00317 {
00318 if (null === $this->_container) {
00319 require_once 'Zend/View/Helper/Placeholder/Registry.php';
00320 $this->_container = Zend_View_Helper_Placeholder_Registry::getRegistry()->getContainer(__CLASS__);
00321 }
00322
00323 return $this->_container;
00324 }
00325
00335 public function setLayout($name, $enabled = true)
00336 {
00337 $this->_layout = (string) $name;
00338 if ($enabled) {
00339 $this->enableLayout();
00340 }
00341 return $this;
00342 }
00343
00349 public function getLayout()
00350 {
00351 return $this->_layout;
00352 }
00353
00359 public function disableLayout()
00360 {
00361 $this->_enabled = false;
00362 return $this;
00363 }
00364
00370 public function enableLayout()
00371 {
00372 $this->_enabled = true;
00373 return $this;
00374 }
00375
00381 public function isEnabled()
00382 {
00383 return $this->_enabled;
00384 }
00385
00386
00387 public function setViewBasePath($path, $prefix = 'Layout_View')
00388 {
00389 $this->_viewBasePath = $path;
00390 $this->_viewBasePrefix = $prefix;
00391 return $this;
00392 }
00393
00394 public function getViewBasePath()
00395 {
00396 return $this->_viewBasePath;
00397 }
00398
00399 public function setViewScriptPath($path)
00400 {
00401 $this->_viewScriptPath = $path;
00402 return $this;
00403 }
00404
00405 public function getViewScriptPath()
00406 {
00407 return $this->_viewScriptPath;
00408 }
00409
00416 public function setLayoutPath($path)
00417 {
00418 return $this->setViewScriptPath($path);
00419 }
00420
00426 public function getLayoutPath()
00427 {
00428 return $this->getViewScriptPath();
00429 }
00430
00439 public function setContentKey($contentKey)
00440 {
00441 $this->_contentKey = (string) $contentKey;
00442 return $this;
00443 }
00444
00450 public function getContentKey()
00451 {
00452 return $this->_contentKey;
00453 }
00454
00461 protected function _setMvcEnabled($mvcEnabled)
00462 {
00463 $this->_mvcEnabled = ($mvcEnabled) ? true : false;
00464 return $this;
00465 }
00466
00472 public function getMvcEnabled()
00473 {
00474 return $this->_mvcEnabled;
00475 }
00476
00483 public function setMvcSuccessfulActionOnly($successfulActionOnly)
00484 {
00485 $this->_mvcSuccessfulActionOnly = ($successfulActionOnly) ? true : false;
00486 return $this;
00487 }
00488
00494 public function getMvcSuccessfulActionOnly()
00495 {
00496 return $this->_mvcSuccessfulActionOnly;
00497 }
00498
00505 public function setView(Zend_View_Interface $view)
00506 {
00507 $this->_view = $view;
00508 return $this;
00509 }
00510
00516 public function getHelperClass()
00517 {
00518 return $this->_helperClass;
00519 }
00520
00527 public function setHelperClass($helperClass)
00528 {
00529 $this->_helperClass = (string) $helperClass;
00530 return $this;
00531 }
00532
00538 public function getPluginClass()
00539 {
00540 return $this->_pluginClass;
00541 }
00542
00549 public function setPluginClass($pluginClass)
00550 {
00551 $this->_pluginClass = (string) $pluginClass;
00552 return $this;
00553 }
00554
00563 public function getView()
00564 {
00565 if (null === $this->_view) {
00566 require_once 'Zend/Controller/Action/HelperBroker.php';
00567 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
00568 if (null === $viewRenderer->view) {
00569 $viewRenderer->initView();
00570 }
00571 $this->setView($viewRenderer->view);
00572 }
00573 return $this->_view;
00574 }
00575
00582 public function setViewSuffix($viewSuffix)
00583 {
00584 $this->_viewSuffix = (string) $viewSuffix;
00585 return $this;
00586 }
00587
00593 public function getViewSuffix()
00594 {
00595 return $this->_viewSuffix;
00596 }
00597
00603 public function getInflectorTarget()
00604 {
00605 return $this->_inflectorTarget;
00606 }
00607
00614 public function setInflectorTarget($inflectorTarget)
00615 {
00616 $this->_inflectorTarget = (string) $inflectorTarget;
00617 return $this;
00618 }
00619
00626 public function setInflector(Zend_Filter_Inflector $inflector)
00627 {
00628 $this->_inflector = $inflector;
00629 return $this;
00630 }
00631
00637 public function getInflector()
00638 {
00639 if (null === $this->_inflector) {
00640 require_once 'Zend/Filter/Inflector.php';
00641 $inflector = new Zend_Filter_Inflector();
00642 $inflector->setTargetReference($this->_inflectorTarget)
00643 ->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower')))
00644 ->setStaticRuleReference('suffix', $this->_viewSuffix);
00645 $this->setInflector($inflector);
00646 }
00647
00648 return $this->_inflector;
00649 }
00650
00656 public function enableInflector()
00657 {
00658 $this->_inflectorEnabled = true;
00659 return $this;
00660 }
00661
00667 public function disableInflector()
00668 {
00669 $this->_inflectorEnabled = false;
00670 return $this;
00671 }
00672
00678 public function inflectorEnabled()
00679 {
00680 return $this->_inflectorEnabled;
00681 }
00682
00690 public function __set($key, $value)
00691 {
00692 $this->_container[$key] = $value;
00693 }
00694
00701 public function __get($key)
00702 {
00703 if (isset($this->_container[$key])) {
00704 return $this->_container[$key];
00705 }
00706
00707 return null;
00708 }
00709
00716 public function __isset($key)
00717 {
00718 return (isset($this->_container[$key]));
00719 }
00720
00727 public function __unset($key)
00728 {
00729 if (isset($this->_container[$key])) {
00730 unset($this->_container[$key]);
00731 }
00732 }
00733
00743 public function assign($spec, $value = null)
00744 {
00745 if (is_array($spec)) {
00746 $orig = $this->_container->getArrayCopy();
00747 $merged = array_merge($orig, $spec);
00748 $this->_container->exchangeArray($merged);
00749 return $this;
00750 }
00751
00752 if (is_string($spec)) {
00753 $this->_container[$spec] = $value;
00754 return $this;
00755 }
00756
00757 require_once 'Zend/Layout/Exception.php';
00758 throw new Zend_Layout_Exception('Invalid values passed to assign()');
00759 }
00760
00773 public function render($name = null)
00774 {
00775 if (null === $name) {
00776 $name = $this->getLayout();
00777 }
00778
00779 if ($this->inflectorEnabled() && (null !== ($inflector = $this->getInflector())))
00780 {
00781 $name = $this->_inflector->filter(array('script' => $name));
00782 }
00783
00784 $view = $this->getView();
00785
00786 if (null !== ($path = $this->getViewScriptPath())) {
00787 if (method_exists($view, 'addScriptPath')) {
00788 $view->addScriptPath($path);
00789 } else {
00790 $view->setScriptPath($path);
00791 }
00792 } elseif (null !== ($path = $this->getViewBasePath())) {
00793 $view->addBasePath($path, $this->_viewBasePrefix);
00794 }
00795
00796 return $view->render($name);
00797 }
00798 }