Layout.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Provider_Abstract
  24. */
  25. require_once 'Zend/Tool/Project/Provider/Abstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Tool
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Tool_Project_Provider_Layout extends Zend_Tool_Project_Provider_Abstract implements Zend_Tool_Framework_Provider_Pretendable
  33. {
  34. /**
  35. * @var string Layout path
  36. */
  37. protected $_layoutPath = 'APPLICATION_PATH "/layouts/scripts/"';
  38. public static function createResource(Zend_Tool_Project_Profile $profile, $layoutName = 'layout')
  39. {
  40. $applicationDirectory = $profile->search('applicationDirectory');
  41. $layoutDirectory = $applicationDirectory->search('layoutsDirectory');
  42. if ($layoutDirectory == false) {
  43. $layoutDirectory = $applicationDirectory->createResource('layoutsDirectory');
  44. }
  45. $layoutScriptsDirectory = $layoutDirectory->search('layoutScriptsDirectory');
  46. if ($layoutScriptsDirectory == false) {
  47. $layoutScriptsDirectory = $layoutDirectory->createResource('layoutScriptsDirectory');
  48. }
  49. $layoutScriptFile = $layoutScriptsDirectory->search('layoutScriptFile', array('layoutName' => 'layout'));
  50. if ($layoutScriptFile == false) {
  51. $layoutScriptFile = $layoutScriptsDirectory->createResource('layoutScriptFile', array('layoutName' => 'layout'));
  52. }
  53. return $layoutScriptFile;
  54. }
  55. public function enable()
  56. {
  57. $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  58. $applicationConfigResource = $profile->search('ApplicationConfigFile');
  59. if (!$applicationConfigResource) {
  60. throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.');
  61. }
  62. $zc = $applicationConfigResource->getAsZendConfig();
  63. if (isset($zc->resources) && isset($zc->resources->layout)) {
  64. $this->_registry->getResponse()->appendContent('A layout resource already exists in this project\'s application configuration file.');
  65. return;
  66. }
  67. if ($this->_registry->getRequest()->isPretend()) {
  68. $this->_registry->getResponse()->appendContent('Would add "resources.layout.layoutPath" key to the application config file.');
  69. } else {
  70. $applicationConfigResource->addStringItem('resources.layout.layoutPath', $this->_layoutPath, 'production', false);
  71. $applicationConfigResource->create();
  72. $this->_registry->getResponse()->appendContent('A layout entry has been added to the application config file.');
  73. $layoutScriptFile = self::createResource($profile);
  74. if (!$layoutScriptFile->exists()) {
  75. $layoutScriptFile->create();
  76. $this->_registry->getResponse()->appendContent(
  77. 'A default layout has been created at '
  78. . $layoutScriptFile->getPath()
  79. );
  80. }
  81. $this->_storeProfile();
  82. }
  83. }
  84. public function disable()
  85. {
  86. $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  87. $applicationConfigResource = $this->_getApplicationConfigResource($profile);
  88. $zc = $applicationConfigResource->getAsZendConfig();
  89. if (isset($zc->resources) && !isset($zc->resources->layout)) {
  90. $this->_registry->getResponse()->appendContent('No layout configuration exists in application config file.');
  91. return;
  92. }
  93. if ($this->_registry->getRequest()->isPretend()) {
  94. $this->_registry->getResponse()->appendContent('Would remove "resources.layout.layoutPath" key from the application config file.');
  95. } else {
  96. // Remove the resources.layout.layoutPath directive from application config
  97. $applicationConfigResource->removeStringItem('resources.layout.layoutPath', $this->_layoutPath, 'production', false);
  98. $applicationConfigResource->create();
  99. // Tell the user about the good work we've done
  100. $this->_registry->getResponse()->appendContent('Layout entry has been removed from the application config file.');
  101. $this->_storeProfile();
  102. }
  103. }
  104. protected function _getApplicationConfigResource(Zend_Tool_Project_Profile $profile)
  105. {
  106. $applicationConfigResource = $profile->search('ApplicationConfigFile');
  107. if (!$applicationConfigResource) {
  108. throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.');
  109. }
  110. return $applicationConfigResource;
  111. }
  112. }