web/lib/Zend/Tool/Project/Provider/Module.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Module.php 23419 2010-11-20 21:37:46Z ramon $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Project_Provider_Abstract
       
    25  */
       
    26 require_once 'Zend/Tool/Project/Provider/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Tool_Framework_Provider_Pretendable
       
    30  */
       
    31 require_once 'Zend/Tool/Framework/Provider/Pretendable.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Tool_Project_Profile_Iterator_ContextFilter
       
    35  */
       
    36 require_once 'Zend/Tool/Project/Profile/Iterator/ContextFilter.php';
       
    37 
       
    38 /**
       
    39  * @see Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter
       
    40  */
       
    41 require_once 'Zend/Tool/Project/Profile/Iterator/EnabledResourceFilter.php';
       
    42 
       
    43 /**
       
    44  * @category   Zend
       
    45  * @package    Zend_Tool
       
    46  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    47  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    48  */
       
    49 class Zend_Tool_Project_Provider_Module
       
    50     extends Zend_Tool_Project_Provider_Abstract
       
    51     implements Zend_Tool_Framework_Provider_Pretendable
       
    52 {
       
    53 
       
    54     public static function createResources(Zend_Tool_Project_Profile $profile, $moduleName, Zend_Tool_Project_Profile_Resource $targetModuleResource = null)
       
    55     {
       
    56 
       
    57         // find the appliction directory, it will serve as our module skeleton
       
    58         if ($targetModuleResource == null) {
       
    59             $targetModuleResource = $profile->search('applicationDirectory');
       
    60             $targetModuleEnabledResources = array(
       
    61                 'ControllersDirectory', 'ModelsDirectory', 'ViewsDirectory',
       
    62                 'ViewScriptsDirectory', 'ViewHelpersDirectory', 'ViewFiltersDirectory'
       
    63                 );
       
    64         }
       
    65 
       
    66         // find the actual modules directory we will use to house our module
       
    67         $modulesDirectory = $profile->search('modulesDirectory');
       
    68 
       
    69         // if there is a module directory already, except
       
    70         if ($modulesDirectory->search(array('moduleDirectory' => array('moduleName' => $moduleName)))) {
       
    71             throw new Zend_Tool_Project_Provider_Exception('A module named "' . $moduleName . '" already exists.');
       
    72         }
       
    73 
       
    74         // create the module directory
       
    75         $moduleDirectory = $modulesDirectory->createResource('moduleDirectory', array('moduleName' => $moduleName));
       
    76 
       
    77         // create a context filter so that we can pull out only what we need from the module skeleton
       
    78         $moduleContextFilterIterator = new Zend_Tool_Project_Profile_Iterator_ContextFilter(
       
    79             $targetModuleResource,
       
    80             array(
       
    81                 'denyNames' => array('ModulesDirectory', 'ViewControllerScriptsDirectory'),
       
    82                 'denyType'  => 'Zend_Tool_Project_Context_Filesystem_File'
       
    83                 )
       
    84             );
       
    85 
       
    86         // the iterator for the module skeleton
       
    87         $targetIterator = new RecursiveIteratorIterator($moduleContextFilterIterator, RecursiveIteratorIterator::SELF_FIRST);
       
    88 
       
    89         // initialize some loop state information
       
    90         $currentDepth = 0;
       
    91         $parentResources = array();
       
    92         $currentResource = $moduleDirectory;
       
    93 
       
    94         // loop through the target module skeleton
       
    95         foreach ($targetIterator as $targetSubResource) {
       
    96 
       
    97             $depthDifference = $targetIterator->getDepth() - $currentDepth;
       
    98             $currentDepth = $targetIterator->getDepth();
       
    99 
       
   100             if ($depthDifference === 1) {
       
   101                 // if we went down into a child, make note
       
   102                 array_push($parentResources, $currentResource);
       
   103                 // this will have always been set previously by another loop
       
   104                 $currentResource = $currentChildResource;
       
   105             } elseif ($depthDifference < 0) {
       
   106                 // if we went up to a parent, make note
       
   107                 $i = $depthDifference;
       
   108                 do {
       
   109                     // if we went out more than 1 parent, get to the correct parent
       
   110                     $currentResource = array_pop($parentResources);
       
   111                 } while ($i-- > 0);
       
   112             }
       
   113 
       
   114             // get parameters for the newly created module resource
       
   115             $params = $targetSubResource->getAttributes();
       
   116             $currentChildResource = $currentResource->createResource($targetSubResource->getName(), $params);
       
   117 
       
   118             // based of the provided list (Currently up top), enable specific resources
       
   119             if (isset($targetModuleEnabledResources)) {
       
   120                 $currentChildResource->setEnabled(in_array($targetSubResource->getName(), $targetModuleEnabledResources));
       
   121             } else {
       
   122                 $currentChildResource->setEnabled($targetSubResource->isEnabled());
       
   123             }
       
   124 
       
   125         }
       
   126 
       
   127         return $moduleDirectory;
       
   128     }
       
   129 
       
   130     /**
       
   131      * create()
       
   132      *
       
   133      * @param string $name
       
   134      */
       
   135     public function create($name) //, $moduleProfile = null)
       
   136     {
       
   137         $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
       
   138 
       
   139         $resources = self::createResources($this->_loadedProfile, $name);
       
   140 
       
   141         $response = $this->_registry->getResponse();
       
   142 
       
   143         if ($this->_registry->getRequest()->isPretend()) {
       
   144             $response->appendContent('I would create the following module and artifacts:');
       
   145             foreach (new RecursiveIteratorIterator($resources, RecursiveIteratorIterator::SELF_FIRST) as $resource) {
       
   146                 if (is_callable(array($resource->getContext(), 'getPath'))) {
       
   147                     $response->appendContent($resource->getContext()->getPath());
       
   148                 }
       
   149             }
       
   150         } else {
       
   151             $response->appendContent('Creating the following module and artifacts:');
       
   152             $enabledFilter = new Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter($resources);
       
   153             foreach (new RecursiveIteratorIterator($enabledFilter, RecursiveIteratorIterator::SELF_FIRST) as $resource) {
       
   154                 $response->appendContent($resource->getContext()->getPath());
       
   155                 $resource->create();
       
   156             }
       
   157 
       
   158             $response->appendContent('Added a key for path module directory to the application.ini file');
       
   159             $appConfigFile = $this->_loadedProfile->search('ApplicationConfigFile');
       
   160             $appConfigFile->removeStringItem('resources.frontController.moduleDirectory', 'production');
       
   161             $appConfigFile->addStringItem('resources.frontController.moduleDirectory', 'APPLICATION_PATH "/modules"', 'production', false);
       
   162 
       
   163             if (strtolower($name) == 'default') {
       
   164                 $response->appendContent('Added a key for the default module to the application.ini file');
       
   165                 $appConfigFile->addStringItem('resources.frontController.params.prefixDefaultModule', '1', 'production');
       
   166             }
       
   167 
       
   168             $appConfigFile->create();
       
   169 
       
   170             // store changes to the profile
       
   171             $this->_storeProfile();
       
   172         }
       
   173 
       
   174     }
       
   175 
       
   176 }
       
   177