File indexing completed on 2024-06-23 05:55:52

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tool
0017  * @subpackage Framework
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Tool_Project_Provider_Abstract
0025  */
0026 // require_once 'Zend/Tool/Project/Provider/Abstract.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Tool
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Tool_Project_Provider_Layout extends Zend_Tool_Project_Provider_Abstract implements Zend_Tool_Framework_Provider_Pretendable
0035 {
0036        /**
0037         * @var string Layout path
0038         */
0039        protected $_layoutPath = 'APPLICATION_PATH "/layouts/scripts/"';
0040 
0041     public static function createResource(Zend_Tool_Project_Profile $profile, $layoutName = 'layout')
0042     {
0043         $applicationDirectory = $profile->search('applicationDirectory');
0044         $layoutDirectory = $applicationDirectory->search('layoutsDirectory');
0045 
0046         if ($layoutDirectory == false) {
0047             $layoutDirectory = $applicationDirectory->createResource('layoutsDirectory');
0048         }
0049 
0050         $layoutScriptsDirectory = $layoutDirectory->search('layoutScriptsDirectory');
0051 
0052         if ($layoutScriptsDirectory == false) {
0053             $layoutScriptsDirectory = $layoutDirectory->createResource('layoutScriptsDirectory');
0054         }
0055 
0056         $layoutScriptFile = $layoutScriptsDirectory->search('layoutScriptFile', array('layoutName' => 'layout'));
0057 
0058         if ($layoutScriptFile == false) {
0059             $layoutScriptFile = $layoutScriptsDirectory->createResource('layoutScriptFile', array('layoutName' => 'layout'));
0060         }
0061 
0062         return $layoutScriptFile;
0063     }
0064 
0065     public function enable()
0066     {
0067         $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
0068 
0069         $applicationConfigResource = $profile->search('ApplicationConfigFile');
0070 
0071         if (!$applicationConfigResource) {
0072             throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.');
0073         }
0074 
0075         $zc = $applicationConfigResource->getAsZendConfig();
0076 
0077         if (isset($zc->resources) && isset($zc->resources->layout)) {
0078             $this->_registry->getResponse()->appendContent('A layout resource already exists in this project\'s application configuration file.');
0079             return;
0080         }
0081 
0082         if ($this->_registry->getRequest()->isPretend()) {
0083             $this->_registry->getResponse()->appendContent('Would add "resources.layout.layoutPath" key to the application config file.');
0084         } else {
0085             $applicationConfigResource->addStringItem('resources.layout.layoutPath', $this->_layoutPath, 'production', false);
0086             $applicationConfigResource->create();
0087 
0088             $this->_registry->getResponse()->appendContent('A layout entry has been added to the application config file.');
0089             
0090             $layoutScriptFile = self::createResource($profile);
0091             if (!$layoutScriptFile->exists()) {
0092                 $layoutScriptFile->create();
0093                 $this->_registry->getResponse()->appendContent(
0094                     'A default layout has been created at '
0095                     . $layoutScriptFile->getPath()
0096                     );
0097 
0098             }
0099 
0100             $this->_storeProfile();
0101         }
0102     }
0103 
0104     public function disable()
0105     {
0106         $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
0107 
0108         $applicationConfigResource = $this->_getApplicationConfigResource($profile);
0109         $zc = $applicationConfigResource->getAsZendConfig();
0110 
0111         if (isset($zc->resources) && !isset($zc->resources->layout)) {
0112             $this->_registry->getResponse()->appendContent('No layout configuration exists in application config file.');
0113             return;
0114         }
0115 
0116         if ($this->_registry->getRequest()->isPretend()) {
0117             $this->_registry->getResponse()->appendContent('Would remove "resources.layout.layoutPath" key from the application config file.');
0118         } else {
0119 
0120             // Remove the resources.layout.layoutPath directive from application config
0121             $applicationConfigResource->removeStringItem('resources.layout.layoutPath', $this->_layoutPath, 'production', false);
0122             $applicationConfigResource->create();
0123 
0124             // Tell the user about the good work we've done
0125             $this->_registry->getResponse()->appendContent('Layout entry has been removed from the application config file.');
0126 
0127             $this->_storeProfile();
0128         }
0129      }
0130 
0131     protected function _getApplicationConfigResource(Zend_Tool_Project_Profile $profile)
0132     {
0133         $applicationConfigResource = $profile->search('ApplicationConfigFile');
0134         if (!$applicationConfigResource) {
0135             throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.');
0136         }
0137 
0138         return $applicationConfigResource;
0139     }
0140 }