Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Loader.php
Go to the documentation of this file.
1 <?php
8 
14 
20 class Loader
21 {
27  private $converter;
28 
34  private $parser;
35 
41  private $moduleRegistry;
42 
48  private $filesystemDriver;
49 
58  public function __construct(
59  Dom $converter,
60  Parser $parser,
61  ComponentRegistrarInterface $moduleRegistry,
62  DriverInterface $filesystemDriver
63  ) {
64  $this->converter = $converter;
65  $this->parser = $parser;
66  $this->parser->initErrorHandler();
67  $this->moduleRegistry = $moduleRegistry;
68  $this->filesystemDriver = $filesystemDriver;
69  }
70 
78  public function load(array $exclude = [])
79  {
80  $result = [];
81  foreach ($this->getModuleConfigs() as list($file, $contents)) {
82  try {
83  $this->parser->loadXML($contents);
84  } catch (\Magento\Framework\Exception\LocalizedException $e) {
85  throw new \Magento\Framework\Exception\LocalizedException(
86  new \Magento\Framework\Phrase(
87  'Invalid Document: %1%2 Error: %3',
88  [$file, PHP_EOL, $e->getMessage()]
89  ),
90  $e
91  );
92  }
93 
94  $data = $this->converter->convert($this->parser->getDom());
95  $name = key($data);
96  if (!in_array($name, $exclude)) {
98  }
99  }
100  return $this->sortBySequence($result);
101  }
102 
115  private function getModuleConfigs()
116  {
117  $modulePaths = $this->moduleRegistry->getPaths(ComponentRegistrar::MODULE);
118  foreach ($modulePaths as $modulePath) {
119  $filePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, "$modulePath/etc/module.xml");
120  yield [$filePath, $this->filesystemDriver->fileGetContents($filePath)];
121  }
122  }
123 
131  private function sortBySequence($origList)
132  {
133  ksort($origList);
134  $expanded = [];
135  foreach ($origList as $moduleName => $value) {
136  $expanded[] = [
137  'name' => $moduleName,
138  'sequence' => $this->expandSequence($origList, $moduleName),
139  ];
140  }
141 
142  // Use "bubble sorting" because usort does not check each pair of elements and in this case it is important
143  $total = count($expanded);
144  for ($i = 0; $i < $total - 1; $i++) {
145  for ($j = $i; $j < $total; $j++) {
146  if (in_array($expanded[$j]['name'], $expanded[$i]['sequence'])) {
147  $temp = $expanded[$i];
148  $expanded[$i] = $expanded[$j];
149  $expanded[$j] = $temp;
150  }
151  }
152  }
153 
154  $result = [];
155  foreach ($expanded as $pair) {
156  $result[$pair['name']] = $origList[$pair['name']];
157  }
158 
159  return $result;
160  }
161 
171  private function expandSequence($list, $name, $accumulated = [])
172  {
173  $accumulated[] = $name;
174  $result = $list[$name]['sequence'];
175  foreach ($result as $relatedName) {
176  if (in_array($relatedName, $accumulated)) {
177  throw new \Exception("Circular sequence reference from '{$name}' to '{$relatedName}'.");
178  }
179  if (!isset($list[$relatedName])) {
180  continue;
181  }
182  $relatedResult = $this->expandSequence($list, $relatedName, $accumulated);
183  $result = array_unique(array_merge($result, $relatedResult));
184  }
185  return $result;
186  }
187 }
$contents
Definition: website.php:14
$value
Definition: gender.phtml:16
__construct(Dom $converter, Parser $parser, ComponentRegistrarInterface $moduleRegistry, DriverInterface $filesystemDriver)
Definition: Loader.php:58
$i
Definition: gallery.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14