PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Tool/Project/Provider/Module.php

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