Skip to content

Commit c52ae13

Browse files
author
Sergii Kovalenko
committed
MAGETWO-88507: Allow Magento to generate factories from di.xml
1 parent dbfd16c commit c52ae13

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

setup/src/Magento/Setup/Module/Di/Code/Scanner/XmlScanner.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ public function __construct(\Magento\Setup\Module\Di\Compiler\Log\Log $log)
3030
*/
3131
public function collectEntities(array $files)
3232
{
33+
$virtualTypes = [];
3334
$output = [];
35+
$factoriesOutput = [];
3436
foreach ($files as $file) {
3537
$dom = new \DOMDocument();
3638
$dom->load($file);
3739
$xpath = new \DOMXPath($dom);
3840
$xpath->registerNamespace("php", "http://php.net/xpath");
3941
$xpath->registerPhpFunctions('preg_match');
42+
$virtualTypeQuery = "//virtualType/@name";
43+
44+
foreach ($xpath->query($virtualTypeQuery) as $virtualNode) {
45+
$virtualTypes[] = $virtualNode->nodeValue;
46+
}
47+
4048
$regex = '/^(.*)\\\(.*)Proxy$/';
4149
$query = "/config/preference[ php:functionString('preg_match', '{$regex}', @type) > 0]/@type | " .
4250
"//argument[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
@@ -46,9 +54,35 @@ public function collectEntities(array $files)
4654
foreach ($xpath->query($query) as $node) {
4755
$output[] = $node->nodeValue;
4856
}
57+
58+
$factoriesOutput = array_merge($factoriesOutput, $this->scanNonVirtualFactories($virtualTypes, $xpath));
4959
}
60+
5061
$output = array_unique($output);
51-
return $this->_filterEntities($output);
62+
$factoriesOutput = array_unique($factoriesOutput);
63+
return array_merge($this->_filterEntities($output), $factoriesOutput);
64+
}
65+
66+
/**
67+
* Scan factories from all di.xml and retrieve non virtual one
68+
*
69+
* @param array $virtualTypes
70+
* @param \DOMXPath $domXpath
71+
* @return array
72+
*/
73+
private function scanNonVirtualFactories(array $virtualTypes, \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+
if (!in_array($node->nodeValue, $virtualTypes)) {
81+
$output[] = $node->nodeValue;
82+
}
83+
}
84+
85+
return $output;
5286
}
5387

5488
/**

0 commit comments

Comments
 (0)