Reader.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Module configuration file reader
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Module\Dir;
  9. use Magento\Framework\Config\FileIterator;
  10. use Magento\Framework\Config\FileIteratorFactory;
  11. use Magento\Framework\Filesystem;
  12. use Magento\Framework\Module\Dir;
  13. use Magento\Framework\Module\ModuleListInterface;
  14. /**
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Reader
  19. {
  20. /**
  21. * Module directories that were set explicitly
  22. *
  23. * @var array
  24. */
  25. protected $customModuleDirs = [];
  26. /**
  27. * Directory registry
  28. *
  29. * @var Dir
  30. */
  31. protected $moduleDirs;
  32. /**
  33. * Modules configuration provider
  34. *
  35. * @var ModuleListInterface
  36. */
  37. protected $modulesList;
  38. /**
  39. * @var FileIteratorFactory
  40. */
  41. protected $fileIteratorFactory;
  42. /**
  43. * @var Filesystem\Directory\ReadFactory
  44. */
  45. protected $readFactory;
  46. /**
  47. * Found configuration files grouped by configuration types (filename).
  48. *
  49. * @var array
  50. */
  51. private $fileIterators = [];
  52. /**
  53. * @param Dir $moduleDirs
  54. * @param ModuleListInterface $moduleList
  55. * @param FileIteratorFactory $fileIteratorFactory
  56. * @param Filesystem\Directory\ReadFactory $readFactory
  57. */
  58. public function __construct(
  59. Dir $moduleDirs,
  60. ModuleListInterface $moduleList,
  61. FileIteratorFactory $fileIteratorFactory,
  62. Filesystem\Directory\ReadFactory $readFactory
  63. ) {
  64. $this->moduleDirs = $moduleDirs;
  65. $this->modulesList = $moduleList;
  66. $this->fileIteratorFactory = $fileIteratorFactory;
  67. $this->readFactory = $readFactory;
  68. }
  69. /**
  70. * Go through all modules and find configuration files of active modules.
  71. *
  72. * @param string $filename
  73. * @return FileIterator
  74. */
  75. public function getConfigurationFiles($filename)
  76. {
  77. return $this->getFilesIterator($filename, Dir::MODULE_ETC_DIR);
  78. }
  79. /**
  80. * Go through all modules and find composer.json files of active modules.
  81. *
  82. * @return FileIterator
  83. */
  84. public function getComposerJsonFiles()
  85. {
  86. return $this->getFilesIterator('composer.json');
  87. }
  88. /**
  89. * Retrieve iterator for files with $filename from components located in component $subDir.
  90. *
  91. * @param string $filename
  92. * @param string $subDir
  93. *
  94. * @return FileIterator
  95. */
  96. private function getFilesIterator($filename, $subDir = '')
  97. {
  98. if (!isset($this->fileIterators[$subDir][$filename])) {
  99. $this->fileIterators[$subDir][$filename] = $this->fileIteratorFactory->create(
  100. $this->getFiles($filename, $subDir)
  101. );
  102. }
  103. return $this->fileIterators[$subDir][$filename];
  104. }
  105. /**
  106. * Go through all modules and find corresponding files of active modules
  107. *
  108. * @param string $filename
  109. * @param string $subDir
  110. * @return array
  111. */
  112. private function getFiles($filename, $subDir = '')
  113. {
  114. $result = [];
  115. foreach ($this->modulesList->getNames() as $moduleName) {
  116. $moduleSubDir = $this->getModuleDir($subDir, $moduleName);
  117. $file = $moduleSubDir . '/' . $filename;
  118. $directoryRead = $this->readFactory->create($moduleSubDir);
  119. $path = $directoryRead->getRelativePath($file);
  120. if ($directoryRead->isExist($path)) {
  121. $result[] = $file;
  122. }
  123. }
  124. return $result;
  125. }
  126. /**
  127. * Retrieve list of module action files
  128. *
  129. * @return array
  130. */
  131. public function getActionFiles()
  132. {
  133. $actions = [];
  134. foreach ($this->modulesList->getNames() as $moduleName) {
  135. $actionDir = $this->getModuleDir(Dir::MODULE_CONTROLLER_DIR, $moduleName);
  136. if (!file_exists($actionDir)) {
  137. continue;
  138. }
  139. $dirIterator = new \RecursiveDirectoryIterator($actionDir, \RecursiveDirectoryIterator::SKIP_DOTS);
  140. $recursiveIterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::LEAVES_ONLY);
  141. $namespace = str_replace('_', '\\', $moduleName);
  142. /** @var \SplFileInfo $actionFile */
  143. foreach ($recursiveIterator as $actionFile) {
  144. $actionName = str_replace('/', '\\', str_replace($actionDir, '', $actionFile->getPathname()));
  145. $action = $namespace . "\\" . Dir::MODULE_CONTROLLER_DIR . substr($actionName, 0, -4);
  146. $actions[strtolower($action)] = $action;
  147. }
  148. }
  149. return $actions;
  150. }
  151. /**
  152. * Get module directory by directory type
  153. *
  154. * @param string $type
  155. * @param string $moduleName
  156. * @return string
  157. */
  158. public function getModuleDir($type, $moduleName)
  159. {
  160. if (isset($this->customModuleDirs[$moduleName][$type])) {
  161. return $this->customModuleDirs[$moduleName][$type];
  162. }
  163. return $this->moduleDirs->getDir($moduleName, $type);
  164. }
  165. /**
  166. * Set path to the corresponding module directory
  167. *
  168. * @param string $moduleName
  169. * @param string $type directory type (etc, controllers, locale etc)
  170. * @param string $path
  171. * @return void
  172. */
  173. public function setModuleDir($moduleName, $type, $path)
  174. {
  175. $this->customModuleDirs[$moduleName][$type] = $path;
  176. $this->fileIterators = [];
  177. }
  178. }