Module.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-2009 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. * @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. * @category Zend
  36. * @package Zend_Tool
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Tool_Project_Provider_Module
  41. extends Zend_Tool_Project_Provider_Abstract
  42. implements Zend_Tool_Framework_Provider_Pretendable
  43. {
  44. public static function createResources(Zend_Tool_Project_Profile $profile, $moduleName, Zend_Tool_Project_Profile_Resource $targetModuleResource = null)
  45. {
  46. // find the appliction directory, it will serve as our module skeleton
  47. if ($targetModuleResource == null) {
  48. $targetModuleResource = $profile->search('applicationDirectory');
  49. $targetModuleEnabledResources = array(
  50. 'ControllersDirectory', 'ModelsDirectory', 'ViewsDirectory',
  51. 'ViewScriptsDirectory', 'ViewHelpersDirectory', 'ViewFiltersDirectory'
  52. );
  53. }
  54. // find the actual modules directory we will use to house our module
  55. $modulesDirectory = $profile->search('modulesDirectory');
  56. // if there is a module directory already, except
  57. if ($modulesDirectory->search(array('moduleDirectory' => array('moduleName' => $moduleName)))) {
  58. throw new Zend_Tool_Project_Provider_Exception('A module named "' . $moduleName . '" already exists.');
  59. }
  60. // create the module directory
  61. $moduleDirectory = $modulesDirectory->createResource('moduleDirectory', array('moduleName' => $moduleName));
  62. // create a context filter so that we can pull out only what we need from the module skeleton
  63. $moduleContextFilterIterator = new Zend_Tool_Project_Profile_Iterator_ContextFilter(
  64. $targetModuleResource,
  65. array(
  66. 'denyNames' => array('ModulesDirectory', 'ViewControllerScriptsDirectory'),
  67. 'denyType' => 'Zend_Tool_Project_Context_Filesystem_File'
  68. )
  69. );
  70. // the iterator for the module skeleton
  71. $targetIterator = new RecursiveIteratorIterator($moduleContextFilterIterator, RecursiveIteratorIterator::SELF_FIRST);
  72. // initialize some loop state information
  73. $currentDepth = 0;
  74. $parentResources = array();
  75. $currentResource = $moduleDirectory;
  76. // loop through the target module skeleton
  77. foreach ($targetIterator as $targetSubResource) {
  78. $depthDifference = $targetIterator->getDepth() - $currentDepth;
  79. $currentDepth = $targetIterator->getDepth();
  80. if ($depthDifference === 1) {
  81. // if we went down into a child, make note
  82. array_push($parentResources, $currentResource);
  83. // this will have always been set previously by another loop
  84. $currentResource = $currentChildResource;
  85. } elseif ($depthDifference < 0) {
  86. // if we went up to a parent, make note
  87. $i = $depthDifference;
  88. do {
  89. // if we went out more than 1 parent, get to the correct parent
  90. $currentResource = array_pop($parentResources);
  91. } while ($i-- > 0);
  92. }
  93. // get parameters for the newly created module resource
  94. $params = $targetSubResource->getAttributes();
  95. $currentChildResource = $currentResource->createResource($targetSubResource->getName(), $params);
  96. // based of the provided list (Currently up top), enable specific resources
  97. if (isset($targetModuleEnabledResources)) {
  98. $currentChildResource->setEnabled(in_array($targetSubResource->getName(), $targetModuleEnabledResources));
  99. } else {
  100. $currentChildResource->setEnabled($targetSubResource->isEnabled());
  101. }
  102. }
  103. return $moduleDirectory;
  104. }
  105. /**
  106. * create()
  107. *
  108. * @param string $name
  109. */
  110. public function create($name) //, $moduleProfile = null)
  111. {
  112. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  113. $resources = self::createResources($this->_loadedProfile, $name);
  114. $response = $this->_registry->getResponse();
  115. if ($this->_registry->getRequest()->isPretend()) {
  116. $response->appendContent('I would create the following module and artifacts:');
  117. foreach (new RecursiveIteratorIterator($resources, RecursiveIteratorIterator::SELF_FIRST) as $resource) {
  118. if (is_callable(array($resource->getContext(), 'getPath'))) {
  119. $response->appendContent($resource->getContext()->getPath());
  120. }
  121. }
  122. } else {
  123. $response->appendContent('Creating the following module and artifacts:');
  124. $enabledFilter = new Zend_Tool_Project_Profile_Iterator_EnabledResource($resources);
  125. foreach (new RecursiveIteratorIterator($enabledFilter, RecursiveIteratorIterator::SELF_FIRST) as $resource) {
  126. $response->appendContent($resource->getContext()->getPath());
  127. $resource->create();
  128. }
  129. // store changes to the profile
  130. $this->_storeProfile();
  131. }
  132. }
  133. }