Skip to content

Commit 368b8d2

Browse files
Indrani sonawaneIndrani sonawane
authored andcommitted
Merge remote-tracking branch '32354/fix-proxies-generation' into comm_247beta3
2 parents e423b68 + 3c8df01 commit 368b8d2

File tree

5 files changed

+112
-35
lines changed

5 files changed

+112
-35
lines changed

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

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* Copyright © Magento, Inc. All rights reserved.
46
* See COPYING.txt for license details.
@@ -42,42 +44,55 @@ public function collectEntities(array $files)
4244
$virtualTypeQuery = "//virtualType/@name";
4345

4446
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";
53-
/** @var \DOMNode $node */
54-
foreach ($xpath->query($query) as $node) {
55-
$output[] = $node->nodeValue;
47+
$virtualTypes[] = ltrim($virtualNode->nodeValue, '\\');
5648
}
5749

58-
$factoriesOutput = array_merge($factoriesOutput, $this->scanFactories($xpath));
50+
$output[] = $this->scanProxies($xpath);
51+
$factoriesOutput[] = $this->scanFactories($xpath);
5952
}
6053

61-
$output = array_unique($output);
62-
$factoriesOutput = array_unique($factoriesOutput);
54+
$output = array_unique(array_merge([], ...$output));
55+
$factoriesOutput = array_unique(array_merge([], ...$factoriesOutput));
6356
$factoriesOutput = array_diff($factoriesOutput, $virtualTypes);
6457
return array_merge($this->_filterEntities($output), $factoriesOutput);
6558
}
6659

6760
/**
68-
* Scan factories from all di.xml and retrieve non virtual one
61+
* Scan proxies from all di.xml
62+
*
63+
* @param \DOMXPath $xpath
64+
* @return array
65+
*/
66+
private function scanProxies(\DOMXPath $xpath): array
67+
{
68+
$result = [];
69+
$regex = '/^(\s+)?(.*)\\\(.*)Proxy(\s+)?$/';
70+
$query = "/config/preference[ php:functionString('preg_match', '{$regex}', @type) > 0]/@type | " .
71+
"//argument[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
72+
"//item[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
73+
"/config/virtualType[ php:functionString('preg_match', '{$regex}', @type) > 0]/@type";
74+
/** @var \DOMNode $node */
75+
foreach ($xpath->query($query) as $node) {
76+
$result[] = ltrim(trim($node->nodeValue), '\\');
77+
}
78+
return $result;
79+
}
80+
81+
/**
82+
* Scan factories from all di.xml and retrieve non-virtual one
6983
*
7084
* @param \DOMXPath $domXpath
7185
* @return array
7286
*/
73-
private function scanFactories(\DOMXPath $domXpath)
87+
private function scanFactories(\DOMXPath $domXpath): array
7488
{
7589
$output = [];
76-
$regex = '/^(.*)Factory$/';
90+
$regex = '/^(\s+)?(.*)Factory(\s+)?$/';
7791
$query = "//argument[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0] |" .
7892
"//item[@xsi:type='object' and php:functionString('preg_match', '{$regex}', text()) > 0]";
93+
7994
foreach ($domXpath->query($query) as $node) {
80-
$output[] = $node->nodeValue;
95+
$output[] = ltrim(trim($node->nodeValue), '\\');
8196
}
8297

8398
return $output;
@@ -94,13 +109,13 @@ protected function _filterEntities(array $output)
94109
$entitySuffix = '\\' . ucfirst(ProxyGenerator::ENTITY_TYPE);
95110
$filteredEntities = [];
96111
foreach ($output as $className) {
97-
$entityName = substr($className, -strlen($entitySuffix)) === $entitySuffix
112+
$entityName = str_ends_with($className, $entitySuffix)
98113
? substr($className, 0, -strlen($entitySuffix))
99114
: $className;
100115
$isClassExists = false;
101116
try {
102117
$isClassExists = class_exists($className);
103-
} catch (\RuntimeException $e) {
118+
} catch (\RuntimeException $e) { //@codingStandardsIgnoreLine
104119
}
105120
if (false === $isClassExists) {
106121
if (class_exists($entityName) || interface_exists($entityName)) {

setup/src/Magento/Setup/Test/Unit/Module/Di/Code/Reader/ClassesScannerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public function testGetList()
4343
$pathToScan = str_replace('\\', '/', realpath(__DIR__ . '/../../') . '/_files/app/code/Magento/SomeModule');
4444
$actual = $this->model->getList($pathToScan);
4545
$this->assertIsArray($actual);
46-
$this->assertCount(6, $actual);
46+
$this->assertCount(7, $actual);
4747
}
4848
}

setup/src/Magento/Setup/Test/Unit/Module/Di/Code/Scanner/XmlScannerTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,33 @@ class XmlScannerTest extends TestCase
1717
/**
1818
* @var XmlScanner
1919
*/
20-
protected $_model;
20+
private XmlScanner $model;
2121

2222
/**
2323
* @var MockObject
2424
*/
25-
protected $_logMock;
25+
private Log $logMock;
2626

2727
/**
2828
* @var array
2929
*/
30-
protected $_testFiles = [];
30+
private array $testFiles = [];
3131

3232
/**
3333
* @inheritdoc
3434
*/
3535
protected function setUp(): void
3636
{
37-
$this->_model = new XmlScanner(
38-
$this->_logMock = $this->createMock(Log::class)
39-
);
37+
$this->logMock = $this->createMock(Log::class);
38+
$this->model = new XmlScanner($this->logMock);
4039
$testDir = __DIR__ . '/../../' . '/_files';
41-
$this->_testFiles = [
40+
$this->testFiles = [
4241
$testDir . '/app/code/Magento/SomeModule/etc/adminhtml/system.xml',
4342
$testDir . '/app/code/Magento/SomeModule/etc/di.xml',
4443
$testDir . '/app/code/Magento/SomeModule/view/frontend/default.xml',
4544
];
45+
require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/Element.php';
46+
require_once __DIR__ . '/../../_files/app/code/Magento/SomeModule/NestedElement.php';
4647
}
4748

4849
/**
@@ -51,7 +52,7 @@ protected function setUp(): void
5152
public function testCollectEntities(): void
5253
{
5354
$className = 'Magento\Store\Model\Config\Invalidator\Proxy';
54-
$this->_logMock
55+
$this->logMock
5556
->method('add')
5657
->withConsecutive(
5758
[
@@ -61,16 +62,26 @@ public function testCollectEntities(): void
6162
],
6263
[
6364
4,
64-
'\Magento\SomeModule\Model\Element\Proxy',
65-
'Invalid proxy class for ' . substr('\Magento\SomeModule\Model\Element\Proxy', 0, -5)
65+
'Magento\SomeModule\Model\Element\Proxy',
66+
'Invalid proxy class for ' . substr('Magento\SomeModule\Model\Element\Proxy', 0, -5)
6667
],
6768
[
6869
4,
69-
'\Magento\SomeModule\Model\Nested\Element\Proxy',
70-
'Invalid proxy class for ' . substr('\Magento\SomeModule\Model\Nested\Element\Proxy', 0, -5)
71-
]
70+
'Magento\SomeModule\Model\Element2\Proxy',
71+
'Invalid proxy class for ' . substr('Magento\SomeModule\Model\Element2\Proxy', 0, -5)
72+
],
73+
[
74+
4,
75+
'Magento\SomeModule\Model\Nested\Element\Proxy',
76+
'Invalid proxy class for ' . substr('Magento\SomeModule\Model\Nested\Element\Proxy', 0, -5)
77+
],
78+
[
79+
4,
80+
'Magento\SomeModule\Model\Nested\Element2\Proxy',
81+
'Invalid proxy class for ' . substr('Magento\SomeModule\Model\Nested\Element2\Proxy', 0, -5)
82+
],
7283
);
73-
$actual = $this->_model->collectEntities($this->_testFiles);
84+
$actual = $this->model->collectEntities($this->testFiles);
7485
$expected = [];
7586
$this->assertEquals($expected, $actual);
7687
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SomeModule;
9+
10+
class NestedElement
11+
{
12+
}

setup/src/Magento/Setup/Test/Unit/Module/Di/_files/app/code/Magento/SomeModule/etc/di.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,48 @@
1313
</virtualType>
1414
<type name="Magento\SomeModule\Model\Test">
1515
<arguments>
16+
<!-- non-existing class proxy - inline -->
1617
<argument name="proxy" xsi:type="object">\Magento\SomeModule\Model\Element\Proxy</argument>
18+
<argument name="proxy2" xsi:type="object">Magento\SomeModule\Model\Element\Proxy</argument>
19+
<!-- non-existing class proxy - multi-line -->
20+
<argument name="proxy3" xsi:type="object">
21+
\Magento\SomeModule\Model\Element2\Proxy
22+
</argument>
23+
<argument name="proxy4" xsi:type="object">
24+
Magento\SomeModule\Model\Element2\Proxy
25+
</argument>
26+
<!-- existing class proxy - multi-line -->
27+
<argument name="proxy5" xsi:type="object">
28+
\Magento\SomeModule\Element\Proxy
29+
</argument>
30+
<argument name="proxy6" xsi:type="object">
31+
Magento\SomeModule\Element\Proxy
32+
</argument>
33+
<!-- existing class proxy - inline -->
34+
<argument name="proxy7" xsi:type="object">\Magento\SomeModule\Element\Proxy</argument>
35+
<argument name="proxy8" xsi:type="object">Magento\SomeModule\Element\Proxy</argument>
36+
1737
<argument name="array" xsi:type="array">
38+
<!-- non-existing class proxy - inline -->
1839
<item name="proxy" xsi:type="object">\Magento\SomeModule\Model\Nested\Element\Proxy</item>
40+
<item name="proxy2" xsi:type="object">Magento\SomeModule\Model\Nested\Element\Proxy</item>
41+
<!-- non-existing class proxy - multi-line -->
42+
<item name="proxy3" xsi:type="object">
43+
\Magento\SomeModule\Model\Nested\Element2\Proxy
44+
</item>
45+
<item name="proxy4" xsi:type="object">
46+
\Magento\SomeModule\Model\Nested\Element2\Proxy
47+
</item>
48+
<!-- existing class proxy - multi-line -->
49+
<item name="proxy5" xsi:type="object">
50+
\Magento\SomeModule\NestedElement\Proxy
51+
</item>
52+
<item name="proxy6" xsi:type="object">
53+
Magento\SomeModule\NestedElement\Proxy
54+
</item>
55+
<!-- existing class proxy - inline -->
56+
<item name="proxy7" xsi:type="object">\Magento\SomeModule\NestedElement\Proxy</item>
57+
<item name="proxy8" xsi:type="object">Magento\SomeModule\NestedElement\Proxy</item>
1958
</argument>
2059
</arguments>
2160
</type>

0 commit comments

Comments
 (0)