Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
XmlScanner.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class XmlScanner implements ScannerInterface
11 {
15  protected $_log;
16 
20  public function __construct(\Magento\Setup\Module\Di\Compiler\Log\Log $log)
21  {
22  $this->_log = $log;
23  }
24 
31  public function collectEntities(array $files)
32  {
33  $virtualTypes = [];
34  $output = [];
35  $factoriesOutput = [];
36  foreach ($files as $file) {
37  $dom = new \DOMDocument();
38  $dom->load($file);
39  $xpath = new \DOMXPath($dom);
40  $xpath->registerNamespace("php", "http://php.net/xpath");
41  $xpath->registerPhpFunctions('preg_match');
42  $virtualTypeQuery = "//virtualType/@name";
43 
44  foreach ($xpath->query($virtualTypeQuery) as $virtualNode) {
45  $virtualTypes[] = $virtualNode->nodeValue;
46  }
47 
48  $regex = '/^(.*)\\\(.*)Proxy$/';
49  $query = "/config/preference[ php:functionString('preg_match', '{$regex}', @type) > 0]/@type | " .
50  "//argument[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
51  "//item[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
52  "/config/virtualType[ php:functionString('preg_match', '{$regex}', @type) > 0]/@type";
54  foreach ($xpath->query($query) as $node) {
55  $output[] = $node->nodeValue;
56  }
57 
58  $factoriesOutput = array_merge($factoriesOutput, $this->scanFactories($xpath));
59  }
60 
61  $output = array_unique($output);
62  $factoriesOutput = array_unique($factoriesOutput);
63  $factoriesOutput = array_diff($factoriesOutput, $virtualTypes);
64  return array_merge($this->_filterEntities($output), $factoriesOutput);
65  }
66 
73  private function scanFactories(\DOMXPath $domXpath)
74  {
75  $output = [];
76  $regex = '/^(.*)Factory$/';
77  $query = "//argument[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
78  "//item[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0]";
79  foreach ($domXpath->query($query) as $node) {
80  $output[] = $node->nodeValue;
81  }
82 
83  return $output;
84  }
85 
92  protected function _filterEntities(array $output)
93  {
94  $entitySuffix = '\\' . ucfirst(ProxyGenerator::ENTITY_TYPE);
95  $filteredEntities = [];
96  foreach ($output as $className) {
97  $entityName = substr($className, -strlen($entitySuffix)) === $entitySuffix
98  ? substr($className, 0, -strlen($entitySuffix))
99  : $className;
100  $isClassExists = false;
101  try {
102  $isClassExists = class_exists($className);
103  } catch (\RuntimeException $e) {
104  }
105  if (false === $isClassExists) {
106  if (class_exists($entityName) || interface_exists($entityName)) {
107  $filteredEntities[] = $className;
108  } else {
109  $this->_log->add(
110  \Magento\Setup\Module\Di\Compiler\Log\Log::CONFIGURATION_ERROR,
111  $className,
112  'Invalid proxy class for ' . substr($className, 0, -5)
113  );
114  }
115  }
116  }
117  return $filteredEntities;
118  }
119 }
__construct(\Magento\Setup\Module\Di\Compiler\Log\Log $log)
Definition: XmlScanner.php:20
foreach($appDirs as $dir) $files
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31