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  * @see Zend_Tool_Framework_Provider_Pretendable
0030  */
0031 // require_once 'Zend/Tool/Framework/Provider/Pretendable.php';
0032 
0033 /**
0034  * @see Zend_Tool_Project_Profile_Iterator_ContextFilter
0035  */
0036 // require_once 'Zend/Tool/Project/Profile/Iterator/ContextFilter.php';
0037 
0038 /**
0039  * @see Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter
0040  */
0041 // require_once 'Zend/Tool/Project/Profile/Iterator/EnabledResourceFilter.php';
0042 
0043 /**
0044  * @category   Zend
0045  * @package    Zend_Tool
0046  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0047  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0048  */
0049 class Zend_Tool_Project_Provider_Module
0050     extends Zend_Tool_Project_Provider_Abstract
0051     implements Zend_Tool_Framework_Provider_Pretendable
0052 {
0053 
0054     public static function createResources(Zend_Tool_Project_Profile $profile, $moduleName, Zend_Tool_Project_Profile_Resource $targetModuleResource = null)
0055     {
0056 
0057         // find the appliction directory, it will serve as our module skeleton
0058         if ($targetModuleResource == null) {
0059             $targetModuleResource = $profile->search('applicationDirectory');
0060             $targetModuleEnabledResources = array(
0061                 'ControllersDirectory', 'ModelsDirectory', 'ViewsDirectory',
0062                 'ViewScriptsDirectory', 'ViewHelpersDirectory', 'ViewFiltersDirectory'
0063                 );
0064         }
0065 
0066         // find the actual modules directory we will use to house our module
0067         $modulesDirectory = $profile->search('modulesDirectory');
0068 
0069         // if there is a module directory already, except
0070         if ($modulesDirectory->search(array('moduleDirectory' => array('moduleName' => $moduleName)))) {
0071             throw new Zend_Tool_Project_Provider_Exception('A module named "' . $moduleName . '" already exists.');
0072         }
0073 
0074         // create the module directory
0075         $moduleDirectory = $modulesDirectory->createResource('moduleDirectory', array('moduleName' => $moduleName));
0076 
0077         // create a context filter so that we can pull out only what we need from the module skeleton
0078         $moduleContextFilterIterator = new Zend_Tool_Project_Profile_Iterator_ContextFilter(
0079             $targetModuleResource,
0080             array(
0081                 'denyNames' => array('ModulesDirectory', 'ViewControllerScriptsDirectory'),
0082                 'denyType'  => 'Zend_Tool_Project_Context_Filesystem_File'
0083                 )
0084             );
0085 
0086         // the iterator for the module skeleton
0087         $targetIterator = new RecursiveIteratorIterator($moduleContextFilterIterator, RecursiveIteratorIterator::SELF_FIRST);
0088 
0089         // initialize some loop state information
0090         $currentDepth = 0;
0091         $parentResources = array();
0092         $currentResource = $moduleDirectory;
0093 
0094         // loop through the target module skeleton
0095         foreach ($targetIterator as $targetSubResource) {
0096 
0097             $depthDifference = $targetIterator->getDepth() - $currentDepth;
0098             $currentDepth = $targetIterator->getDepth();
0099 
0100             if ($depthDifference === 1) {
0101                 // if we went down into a child, make note
0102                 array_push($parentResources, $currentResource);
0103                 // this will have always been set previously by another loop
0104                 $currentResource = $currentChildResource;
0105             } elseif ($depthDifference < 0) {
0106                 // if we went up to a parent, make note
0107                 $i = $depthDifference;
0108                 do {
0109                     // if we went out more than 1 parent, get to the correct parent
0110                     $currentResource = array_pop($parentResources);
0111                 } while ($i-- > 0);
0112             }
0113 
0114             // get parameters for the newly created module resource
0115             $params = $targetSubResource->getAttributes();
0116             $currentChildResource = $currentResource->createResource($targetSubResource->getName(), $params);
0117 
0118             // based of the provided list (Currently up top), enable specific resources
0119             if (isset($targetModuleEnabledResources)) {
0120                 $currentChildResource->setEnabled(in_array($targetSubResource->getName(), $targetModuleEnabledResources));
0121             } else {
0122                 $currentChildResource->setEnabled($targetSubResource->isEnabled());
0123             }
0124 
0125         }
0126 
0127         return $moduleDirectory;
0128     }
0129 
0130     /**
0131      * create()
0132      *
0133      * @param string $name
0134      */
0135     public function create($name) //, $moduleProfile = null)
0136     {
0137         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
0138 
0139         // determine if testing is enabled in the project
0140         // require_once 'Zend/Tool/Project/Provider/Test.php';
0141         //$testingEnabled = Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
0142         
0143         $resources = self::createResources($this->_loadedProfile, $name);
0144 
0145         $response = $this->_registry->getResponse();
0146 
0147         if ($this->_registry->getRequest()->isPretend()) {
0148             $response->appendContent('I would create the following module and artifacts:');
0149             foreach (new RecursiveIteratorIterator($resources, RecursiveIteratorIterator::SELF_FIRST) as $resource) {
0150                 if (is_callable(array($resource->getContext(), 'getPath'))) {
0151                     $response->appendContent($resource->getContext()->getPath());
0152                 }
0153             }
0154         } else {
0155             $response->appendContent('Creating the following module and artifacts:');
0156             $enabledFilter = new Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter($resources);
0157             foreach (new RecursiveIteratorIterator($enabledFilter, RecursiveIteratorIterator::SELF_FIRST) as $resource) {
0158                 $response->appendContent($resource->getContext()->getPath());
0159                 $resource->create();
0160             }
0161 
0162             $response->appendContent('Added a key for path module directory to the application.ini file');
0163             $appConfigFile = $this->_loadedProfile->search('ApplicationConfigFile');
0164             $appConfigFile->removeStringItem('resources.frontController.moduleDirectory', 'production');
0165             $appConfigFile->addStringItem('resources.frontController.moduleDirectory', 'APPLICATION_PATH "/modules"', 'production', false);
0166 
0167             if (strtolower($name) == 'default') {
0168                 $response->appendContent('Added a key for the default module to the application.ini file');
0169                 $appConfigFile->addStringItem('resources.frontController.params.prefixDefaultModule', '1', 'production');
0170             }
0171 
0172             $appConfigFile->create();
0173 
0174             // store changes to the profile
0175             $this->_storeProfile();
0176         }
0177 
0178     }
0179 
0180 }
0181