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

/zf2/vendor/Zend/Tool/Project/Provider/Module.php

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