PageRenderTime 118ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/protected/vendors/Zend/Layout.php

https://bitbucket.org/negge/tlklan2
PHP | 798 lines | 357 code | 81 blank | 360 comment | 32 complexity | b1e7ce92cd5c1cfdb15207fc7fdb5f6b MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, GPL-3.0
  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_Layout
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Layout.php 24593 2012-01-05 20:35:02Z matthew $
  20. */
  21. /**
  22. * Provide Layout support for MVC applications
  23. *
  24. * @category Zend
  25. * @package Zend_Layout
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Layout
  30. {
  31. /**
  32. * Placeholder container for layout variables
  33. * @var Zend_View_Helper_Placeholder_Container
  34. */
  35. protected $_container;
  36. /**
  37. * Key used to store content from 'default' named response segment
  38. * @var string
  39. */
  40. protected $_contentKey = 'content';
  41. /**
  42. * Are layouts enabled?
  43. * @var bool
  44. */
  45. protected $_enabled = true;
  46. /**
  47. * Helper class
  48. * @var string
  49. */
  50. protected $_helperClass = 'Zend_Layout_Controller_Action_Helper_Layout';
  51. /**
  52. * Inflector used to resolve layout script
  53. * @var Zend_Filter_Inflector
  54. */
  55. protected $_inflector;
  56. /**
  57. * Flag: is inflector enabled?
  58. * @var bool
  59. */
  60. protected $_inflectorEnabled = true;
  61. /**
  62. * Inflector target
  63. * @var string
  64. */
  65. protected $_inflectorTarget = ':script.:suffix';
  66. /**
  67. * Layout view
  68. * @var string
  69. */
  70. protected $_layout = 'layout';
  71. /**
  72. * Layout view script path
  73. * @var string
  74. */
  75. protected $_viewScriptPath = null;
  76. protected $_viewBasePath = null;
  77. protected $_viewBasePrefix = 'Layout_View';
  78. /**
  79. * Flag: is MVC integration enabled?
  80. * @var bool
  81. */
  82. protected $_mvcEnabled = true;
  83. /**
  84. * Instance registered with MVC, if any
  85. * @var Zend_Layout
  86. */
  87. protected static $_mvcInstance;
  88. /**
  89. * Flag: is MVC successful action only flag set?
  90. * @var bool
  91. */
  92. protected $_mvcSuccessfulActionOnly = true;
  93. /**
  94. * Plugin class
  95. * @var string
  96. */
  97. protected $_pluginClass = 'Zend_Layout_Controller_Plugin_Layout';
  98. /**
  99. * @var Zend_View_Interface
  100. */
  101. protected $_view;
  102. /**
  103. * View script suffix for layout script
  104. * @var string
  105. */
  106. protected $_viewSuffix = 'phtml';
  107. /**
  108. * Constructor
  109. *
  110. * Accepts either:
  111. * - A string path to layouts
  112. * - An array of options
  113. * - A Zend_Config object with options
  114. *
  115. * Layout script path, either as argument or as key in options, is
  116. * required.
  117. *
  118. * If mvcEnabled flag is false from options, simply sets layout script path.
  119. * Otherwise, also instantiates and registers action helper and controller
  120. * plugin.
  121. *
  122. * @param string|array|Zend_Config $options
  123. * @return void
  124. */
  125. public function __construct($options = null, $initMvc = false)
  126. {
  127. if (null !== $options) {
  128. if (is_string($options)) {
  129. $this->setLayoutPath($options);
  130. } elseif (is_array($options)) {
  131. $this->setOptions($options);
  132. } elseif ($options instanceof Zend_Config) {
  133. $this->setConfig($options);
  134. } else {
  135. require_once 'Zend/Layout/Exception.php';
  136. throw new Zend_Layout_Exception('Invalid option provided to constructor');
  137. }
  138. }
  139. $this->_initVarContainer();
  140. if ($initMvc) {
  141. $this->_setMvcEnabled(true);
  142. $this->_initMvc();
  143. } else {
  144. $this->_setMvcEnabled(false);
  145. }
  146. }
  147. /**
  148. * Static method for initialization with MVC support
  149. *
  150. * @param string|array|Zend_Config $options
  151. * @return Zend_Layout
  152. */
  153. public static function startMvc($options = null)
  154. {
  155. if (null === self::$_mvcInstance) {
  156. self::$_mvcInstance = new self($options, true);
  157. }
  158. if (is_string($options)) {
  159. self::$_mvcInstance->setLayoutPath($options);
  160. } elseif (is_array($options) || $options instanceof Zend_Config) {
  161. self::$_mvcInstance->setOptions($options);
  162. }
  163. return self::$_mvcInstance;
  164. }
  165. /**
  166. * Retrieve MVC instance of Zend_Layout object
  167. *
  168. * @return Zend_Layout|null
  169. */
  170. public static function getMvcInstance()
  171. {
  172. return self::$_mvcInstance;
  173. }
  174. /**
  175. * Reset MVC instance
  176. *
  177. * Unregisters plugins and helpers, and destroys MVC layout instance.
  178. *
  179. * @return void
  180. */
  181. public static function resetMvcInstance()
  182. {
  183. if (null !== self::$_mvcInstance) {
  184. $layout = self::$_mvcInstance;
  185. $pluginClass = $layout->getPluginClass();
  186. $front = Zend_Controller_Front::getInstance();
  187. if ($front->hasPlugin($pluginClass)) {
  188. $front->unregisterPlugin($pluginClass);
  189. }
  190. if (Zend_Controller_Action_HelperBroker::hasHelper('layout')) {
  191. Zend_Controller_Action_HelperBroker::removeHelper('layout');
  192. }
  193. unset($layout);
  194. self::$_mvcInstance = null;
  195. }
  196. }
  197. /**
  198. * Set options en masse
  199. *
  200. * @param array|Zend_Config $options
  201. * @return void
  202. */
  203. public function setOptions($options)
  204. {
  205. if ($options instanceof Zend_Config) {
  206. $options = $options->toArray();
  207. } elseif (!is_array($options)) {
  208. require_once 'Zend/Layout/Exception.php';
  209. throw new Zend_Layout_Exception('setOptions() expects either an array or a Zend_Config object');
  210. }
  211. foreach ($options as $key => $value) {
  212. $method = 'set' . ucfirst($key);
  213. if (method_exists($this, $method)) {
  214. $this->$method($value);
  215. }
  216. }
  217. }
  218. /**
  219. * Initialize MVC integration
  220. *
  221. * @return void
  222. */
  223. protected function _initMvc()
  224. {
  225. $this->_initPlugin();
  226. $this->_initHelper();
  227. }
  228. /**
  229. * Initialize front controller plugin
  230. *
  231. * @return void
  232. */
  233. protected function _initPlugin()
  234. {
  235. $pluginClass = $this->getPluginClass();
  236. require_once 'Zend/Controller/Front.php';
  237. $front = Zend_Controller_Front::getInstance();
  238. if (!$front->hasPlugin($pluginClass)) {
  239. if (!class_exists($pluginClass)) {
  240. require_once 'Zend/Loader.php';
  241. Zend_Loader::loadClass($pluginClass);
  242. }
  243. $front->registerPlugin(
  244. // register to run last | BUT before the ErrorHandler (if its available)
  245. new $pluginClass($this),
  246. 99
  247. );
  248. }
  249. }
  250. /**
  251. * Initialize action helper
  252. *
  253. * @return void
  254. */
  255. protected function _initHelper()
  256. {
  257. $helperClass = $this->getHelperClass();
  258. require_once 'Zend/Controller/Action/HelperBroker.php';
  259. if (!Zend_Controller_Action_HelperBroker::hasHelper('layout')) {
  260. if (!class_exists($helperClass)) {
  261. require_once 'Zend/Loader.php';
  262. Zend_Loader::loadClass($helperClass);
  263. }
  264. Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-90, new $helperClass($this));
  265. }
  266. }
  267. /**
  268. * Set options from a config object
  269. *
  270. * @param Zend_Config $config
  271. * @return Zend_Layout
  272. */
  273. public function setConfig(Zend_Config $config)
  274. {
  275. $this->setOptions($config->toArray());
  276. return $this;
  277. }
  278. /**
  279. * Initialize placeholder container for layout vars
  280. *
  281. * @return Zend_View_Helper_Placeholder_Container
  282. */
  283. protected function _initVarContainer()
  284. {
  285. if (null === $this->_container) {
  286. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  287. $this->_container = Zend_View_Helper_Placeholder_Registry::getRegistry()->getContainer(__CLASS__);
  288. }
  289. return $this->_container;
  290. }
  291. /**
  292. * Set layout script to use
  293. *
  294. * Note: enables layout by default, can be disabled
  295. *
  296. * @param string $name
  297. * @param boolean $enabled
  298. * @return Zend_Layout
  299. */
  300. public function setLayout($name, $enabled = true)
  301. {
  302. $this->_layout = (string) $name;
  303. if ($enabled) {
  304. $this->enableLayout();
  305. }
  306. return $this;
  307. }
  308. /**
  309. * Get current layout script
  310. *
  311. * @return string
  312. */
  313. public function getLayout()
  314. {
  315. return $this->_layout;
  316. }
  317. /**
  318. * Disable layout
  319. *
  320. * @return Zend_Layout
  321. */
  322. public function disableLayout()
  323. {
  324. $this->_enabled = false;
  325. return $this;
  326. }
  327. /**
  328. * Enable layout
  329. *
  330. * @return Zend_Layout
  331. */
  332. public function enableLayout()
  333. {
  334. $this->_enabled = true;
  335. return $this;
  336. }
  337. /**
  338. * Is layout enabled?
  339. *
  340. * @return bool
  341. */
  342. public function isEnabled()
  343. {
  344. return $this->_enabled;
  345. }
  346. public function setViewBasePath($path, $prefix = 'Layout_View')
  347. {
  348. $this->_viewBasePath = $path;
  349. $this->_viewBasePrefix = $prefix;
  350. return $this;
  351. }
  352. public function getViewBasePath()
  353. {
  354. return $this->_viewBasePath;
  355. }
  356. public function setViewScriptPath($path)
  357. {
  358. $this->_viewScriptPath = $path;
  359. return $this;
  360. }
  361. public function getViewScriptPath()
  362. {
  363. return $this->_viewScriptPath;
  364. }
  365. /**
  366. * Set layout script path
  367. *
  368. * @param string $path
  369. * @return Zend_Layout
  370. */
  371. public function setLayoutPath($path)
  372. {
  373. return $this->setViewScriptPath($path);
  374. }
  375. /**
  376. * Get current layout script path
  377. *
  378. * @return string
  379. */
  380. public function getLayoutPath()
  381. {
  382. return $this->getViewScriptPath();
  383. }
  384. /**
  385. * Set content key
  386. *
  387. * Key in namespace container denoting default content
  388. *
  389. * @param string $contentKey
  390. * @return Zend_Layout
  391. */
  392. public function setContentKey($contentKey)
  393. {
  394. $this->_contentKey = (string) $contentKey;
  395. return $this;
  396. }
  397. /**
  398. * Retrieve content key
  399. *
  400. * @return string
  401. */
  402. public function getContentKey()
  403. {
  404. return $this->_contentKey;
  405. }
  406. /**
  407. * Set MVC enabled flag
  408. *
  409. * @param bool $mvcEnabled
  410. * @return Zend_Layout
  411. */
  412. protected function _setMvcEnabled($mvcEnabled)
  413. {
  414. $this->_mvcEnabled = ($mvcEnabled) ? true : false;
  415. return $this;
  416. }
  417. /**
  418. * Retrieve MVC enabled flag
  419. *
  420. * @return bool
  421. */
  422. public function getMvcEnabled()
  423. {
  424. return $this->_mvcEnabled;
  425. }
  426. /**
  427. * Set MVC Successful Action Only flag
  428. *
  429. * @param bool $successfulActionOnly
  430. * @return Zend_Layout
  431. */
  432. public function setMvcSuccessfulActionOnly($successfulActionOnly)
  433. {
  434. $this->_mvcSuccessfulActionOnly = ($successfulActionOnly) ? true : false;
  435. return $this;
  436. }
  437. /**
  438. * Get MVC Successful Action Only Flag
  439. *
  440. * @return bool
  441. */
  442. public function getMvcSuccessfulActionOnly()
  443. {
  444. return $this->_mvcSuccessfulActionOnly;
  445. }
  446. /**
  447. * Set view object
  448. *
  449. * @param Zend_View_Interface $view
  450. * @return Zend_Layout
  451. */
  452. public function setView(Zend_View_Interface $view)
  453. {
  454. $this->_view = $view;
  455. return $this;
  456. }
  457. /**
  458. * Retrieve helper class
  459. *
  460. * @return string
  461. */
  462. public function getHelperClass()
  463. {
  464. return $this->_helperClass;
  465. }
  466. /**
  467. * Set helper class
  468. *
  469. * @param string $helperClass
  470. * @return Zend_Layout
  471. */
  472. public function setHelperClass($helperClass)
  473. {
  474. $this->_helperClass = (string) $helperClass;
  475. return $this;
  476. }
  477. /**
  478. * Retrieve plugin class
  479. *
  480. * @return string
  481. */
  482. public function getPluginClass()
  483. {
  484. return $this->_pluginClass;
  485. }
  486. /**
  487. * Set plugin class
  488. *
  489. * @param string $pluginClass
  490. * @return Zend_Layout
  491. */
  492. public function setPluginClass($pluginClass)
  493. {
  494. $this->_pluginClass = (string) $pluginClass;
  495. return $this;
  496. }
  497. /**
  498. * Get current view object
  499. *
  500. * If no view object currently set, retrieves it from the ViewRenderer.
  501. *
  502. * @todo Set inflector from view renderer at same time
  503. * @return Zend_View_Interface
  504. */
  505. public function getView()
  506. {
  507. if (null === $this->_view) {
  508. require_once 'Zend/Controller/Action/HelperBroker.php';
  509. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  510. if (null === $viewRenderer->view) {
  511. $viewRenderer->initView();
  512. }
  513. $this->setView($viewRenderer->view);
  514. }
  515. return $this->_view;
  516. }
  517. /**
  518. * Set layout view script suffix
  519. *
  520. * @param string $viewSuffix
  521. * @return Zend_Layout
  522. */
  523. public function setViewSuffix($viewSuffix)
  524. {
  525. $this->_viewSuffix = (string) $viewSuffix;
  526. return $this;
  527. }
  528. /**
  529. * Retrieve layout view script suffix
  530. *
  531. * @return string
  532. */
  533. public function getViewSuffix()
  534. {
  535. return $this->_viewSuffix;
  536. }
  537. /**
  538. * Retrieve inflector target
  539. *
  540. * @return string
  541. */
  542. public function getInflectorTarget()
  543. {
  544. return $this->_inflectorTarget;
  545. }
  546. /**
  547. * Set inflector target
  548. *
  549. * @param string $inflectorTarget
  550. * @return Zend_Layout
  551. */
  552. public function setInflectorTarget($inflectorTarget)
  553. {
  554. $this->_inflectorTarget = (string) $inflectorTarget;
  555. return $this;
  556. }
  557. /**
  558. * Set inflector to use when resolving layout names
  559. *
  560. * @param Zend_Filter_Inflector $inflector
  561. * @return Zend_Layout
  562. */
  563. public function setInflector(Zend_Filter_Inflector $inflector)
  564. {
  565. $this->_inflector = $inflector;
  566. return $this;
  567. }
  568. /**
  569. * Retrieve inflector
  570. *
  571. * @return Zend_Filter_Inflector
  572. */
  573. public function getInflector()
  574. {
  575. if (null === $this->_inflector) {
  576. require_once 'Zend/Filter/Inflector.php';
  577. $inflector = new Zend_Filter_Inflector();
  578. $inflector->setTargetReference($this->_inflectorTarget)
  579. ->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower')))
  580. ->setStaticRuleReference('suffix', $this->_viewSuffix);
  581. $this->setInflector($inflector);
  582. }
  583. return $this->_inflector;
  584. }
  585. /**
  586. * Enable inflector
  587. *
  588. * @return Zend_Layout
  589. */
  590. public function enableInflector()
  591. {
  592. $this->_inflectorEnabled = true;
  593. return $this;
  594. }
  595. /**
  596. * Disable inflector
  597. *
  598. * @return Zend_Layout
  599. */
  600. public function disableInflector()
  601. {
  602. $this->_inflectorEnabled = false;
  603. return $this;
  604. }
  605. /**
  606. * Return status of inflector enabled flag
  607. *
  608. * @return bool
  609. */
  610. public function inflectorEnabled()
  611. {
  612. return $this->_inflectorEnabled;
  613. }
  614. /**
  615. * Set layout variable
  616. *
  617. * @param string $key
  618. * @param mixed $value
  619. * @return void
  620. */
  621. public function __set($key, $value)
  622. {
  623. $this->_container[$key] = $value;
  624. }
  625. /**
  626. * Get layout variable
  627. *
  628. * @param string $key
  629. * @return mixed
  630. */
  631. public function __get($key)
  632. {
  633. if (isset($this->_container[$key])) {
  634. return $this->_container[$key];
  635. }
  636. return null;
  637. }
  638. /**
  639. * Is a layout variable set?
  640. *
  641. * @param string $key
  642. * @return bool
  643. */
  644. public function __isset($key)
  645. {
  646. return (isset($this->_container[$key]));
  647. }
  648. /**
  649. * Unset a layout variable?
  650. *
  651. * @param string $key
  652. * @return void
  653. */
  654. public function __unset($key)
  655. {
  656. if (isset($this->_container[$key])) {
  657. unset($this->_container[$key]);
  658. }
  659. }
  660. /**
  661. * Assign one or more layout variables
  662. *
  663. * @param mixed $spec Assoc array or string key; if assoc array, sets each
  664. * key as a layout variable
  665. * @param mixed $value Value if $spec is a key
  666. * @return Zend_Layout
  667. * @throws Zend_Layout_Exception if non-array/string value passed to $spec
  668. */
  669. public function assign($spec, $value = null)
  670. {
  671. if (is_array($spec)) {
  672. $orig = $this->_container->getArrayCopy();
  673. $merged = array_merge($orig, $spec);
  674. $this->_container->exchangeArray($merged);
  675. return $this;
  676. }
  677. if (is_string($spec)) {
  678. $this->_container[$spec] = $value;
  679. return $this;
  680. }
  681. require_once 'Zend/Layout/Exception.php';
  682. throw new Zend_Layout_Exception('Invalid values passed to assign()');
  683. }
  684. /**
  685. * Render layout
  686. *
  687. * Sets internal script path as last path on script path stack, assigns
  688. * layout variables to view, determines layout name using inflector, and
  689. * renders layout view script.
  690. *
  691. * $name will be passed to the inflector as the key 'script'.
  692. *
  693. * @param mixed $name
  694. * @return mixed
  695. */
  696. public function render($name = null)
  697. {
  698. if (null === $name) {
  699. $name = $this->getLayout();
  700. }
  701. if ($this->inflectorEnabled() && (null !== ($inflector = $this->getInflector())))
  702. {
  703. $name = $this->_inflector->filter(array('script' => $name));
  704. }
  705. $view = $this->getView();
  706. if (null !== ($path = $this->getViewScriptPath())) {
  707. if (method_exists($view, 'addScriptPath')) {
  708. $view->addScriptPath($path);
  709. } else {
  710. $view->setScriptPath($path);
  711. }
  712. } elseif (null !== ($path = $this->getViewBasePath())) {
  713. $view->addBasePath($path, $this->_viewBasePrefix);
  714. }
  715. return $view->render($name);
  716. }
  717. }