Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Reader.php
Go to the documentation of this file.
1 <?php
9 
15 
20 class Reader
21 {
27  protected $customModuleDirs = [];
28 
34  protected $moduleDirs;
35 
41  protected $modulesList;
42 
47 
51  protected $readFactory;
52 
58  private $fileIterators = [];
59 
66  public function __construct(
68  ModuleListInterface $moduleList,
70  Filesystem\Directory\ReadFactory $readFactory
71  ) {
72  $this->moduleDirs = $moduleDirs;
73  $this->modulesList = $moduleList;
74  $this->fileIteratorFactory = $fileIteratorFactory;
75  $this->readFactory = $readFactory;
76  }
77 
84  public function getConfigurationFiles($filename)
85  {
86  return $this->getFilesIterator($filename, Dir::MODULE_ETC_DIR);
87  }
88 
94  public function getComposerJsonFiles()
95  {
96  return $this->getFilesIterator('composer.json');
97  }
98 
107  private function getFilesIterator($filename, $subDir = '')
108  {
109  if (!isset($this->fileIterators[$subDir][$filename])) {
110  $this->fileIterators[$subDir][$filename] = $this->fileIteratorFactory->create(
111  $this->getFiles($filename, $subDir)
112  );
113  }
114  return $this->fileIterators[$subDir][$filename];
115  }
116 
124  private function getFiles($filename, $subDir = '')
125  {
126  $result = [];
127  foreach ($this->modulesList->getNames() as $moduleName) {
128  $moduleSubDir = $this->getModuleDir($subDir, $moduleName);
129  $file = $moduleSubDir . '/' . $filename;
130  $directoryRead = $this->readFactory->create($moduleSubDir);
131  $path = $directoryRead->getRelativePath($file);
132  if ($directoryRead->isExist($path)) {
133  $result[] = $file;
134  }
135  }
136  return $result;
137  }
138 
144  public function getActionFiles()
145  {
146  $actions = [];
147  foreach ($this->modulesList->getNames() as $moduleName) {
148  $actionDir = $this->getModuleDir(Dir::MODULE_CONTROLLER_DIR, $moduleName);
149  if (!file_exists($actionDir)) {
150  continue;
151  }
152  $dirIterator = new \RecursiveDirectoryIterator($actionDir, \RecursiveDirectoryIterator::SKIP_DOTS);
153  $recursiveIterator = new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::LEAVES_ONLY);
154  $namespace = str_replace('_', '\\', $moduleName);
156  foreach ($recursiveIterator as $actionFile) {
157  $actionName = str_replace('/', '\\', str_replace($actionDir, '', $actionFile->getPathname()));
158  $action = $namespace . "\\" . Dir::MODULE_CONTROLLER_DIR . substr($actionName, 0, -4);
159  $actions[strtolower($action)] = $action;
160  }
161  }
162  return $actions;
163  }
164 
172  public function getModuleDir($type, $moduleName)
173  {
174  if (isset($this->customModuleDirs[$moduleName][$type])) {
175  return $this->customModuleDirs[$moduleName][$type];
176  }
177  return $this->moduleDirs->getDir($moduleName, $type);
178  }
179 
188  public function setModuleDir($moduleName, $type, $path)
189  {
190  $this->customModuleDirs[$moduleName][$type] = $path;
191  $this->fileIterators = [];
192  }
193 }
getModuleDir($type, $moduleName)
Definition: Reader.php:172
$type
Definition: item.phtml:13
__construct(Dir $moduleDirs, ModuleListInterface $moduleList, FileIteratorFactory $fileIteratorFactory, Filesystem\Directory\ReadFactory $readFactory)
Definition: Reader.php:66
setModuleDir($moduleName, $type, $path)
Definition: Reader.php:188