Skip to content

Commit 591fa43

Browse files
committed
Merge remote-tracking branch 'performance/ACPT-1666-a' into ACPT-1666
2 parents 1c280f1 + 3a0c13d commit 591fa43

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

lib/internal/Magento/Framework/ObjectManager/Resetter/Resetter.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\Framework\ObjectManager\Resetter;
99

1010
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Component\ComponentRegistrar;
12+
use Magento\Framework\Component\ComponentRegistrarInterface;
1113
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
1214
use Magento\Framework\ObjectManagerInterface;
1315
use WeakMap;
@@ -18,7 +20,8 @@
1820
*/
1921
class Resetter implements ResetterInterface
2022
{
21-
public const RESET_PATH = '/app/etc/reset.php';
23+
public const RESET_PATH = 'reset.json';
24+
private const RESET_STATE_METHOD = '_resetState';
2225

2326
/** @var WeakMap instances to be reset after request */
2427
private WeakMap $resetAfterWeakMap;
@@ -29,21 +32,6 @@ class Resetter implements ResetterInterface
2932
/** @var WeakMapSorter|null Note: We use temporal coupling here because of chicken/egg during bootstrapping */
3033
private ?WeakMapSorter $weakMapSorter = null;
3134

32-
/**
33-
* @var array
34-
*
35-
*/
36-
private array $classList = [
37-
//phpcs:disable Magento2.PHP.LiteralNamespaces
38-
'Magento\Framework\GraphQl\Query\Fields' => true,
39-
'Magento\Store\Model\Store' => [
40-
"_baseUrlCache" => [],
41-
"_configCache" => null,
42-
"_configCacheBaseNodes" => [],
43-
"_dirCache" => [],
44-
"_urlCache" => []
45-
]
46-
];
4735

4836
/**
4937
* @var array
@@ -56,15 +44,34 @@ class Resetter implements ResetterInterface
5644
* @return void
5745
* @phpcs:disable Magento2.Functions.DiscouragedFunction
5846
*/
59-
public function __construct()
60-
{
61-
if (\file_exists(BP . self::RESET_PATH)) {
62-
// phpcs:ignore Magento2.Security.IncludeFile.FoundIncludeFile
63-
$this->classList = array_replace($this->classList, (require BP . self::RESET_PATH));
47+
public function __construct(
48+
private ComponentRegistrarInterface $componentRegistrar,
49+
private array $classList = [],
50+
) {
51+
foreach ($this->getPaths() as $resetPath) {
52+
if (!\file_exists($resetPath)) {
53+
continue;
54+
}
55+
$resetData = \json_decode(\file_get_contents($resetPath), true);
56+
$this->classList = array_replace($this->classList, $resetData);
6457
}
6558
$this->resetAfterWeakMap = new WeakMap;
6659
}
6760

61+
/**
62+
* Get paths for reset json
63+
*
64+
* @return \Generator<string>
65+
*/
66+
private function getPaths(): \Generator
67+
{
68+
yield BP . '/app/etc/' . self::RESET_PATH;
69+
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $modulePath) {
70+
yield $modulePath . '/etc/' . self::RESET_PATH;
71+
}
72+
}
73+
74+
6875
/**
6976
* Add instance to be reset later
7077
*
@@ -73,7 +80,10 @@ public function __construct()
7380
*/
7481
public function addInstance(object $instance) : void
7582
{
76-
if ($instance instanceof ResetAfterRequestInterface || isset($this->classList[\get_class($instance)])) {
83+
if ($instance instanceof ResetAfterRequestInterface
84+
|| \method_exists($instance, self::RESET_STATE_METHOD)
85+
|| isset($this->classList[\get_class($instance)])
86+
) {
7787
$this->resetAfterWeakMap[$instance] = true;
7888
}
7989
}
@@ -125,6 +135,10 @@ public function setObjectManager(ObjectManagerInterface $objectManager) : void
125135
*/
126136
private function resetStateWithReflection(object $instance)
127137
{
138+
if (\method_exists($instance, self::RESET_STATE_METHOD)) {
139+
$instance->{self::RESET_STATE_METHOD}();
140+
return;
141+
}
128142
$className = \get_class($instance);
129143
$reflectionClass = $this->reflectionCache[$className]
130144
?? $this->reflectionCache[$className] = new \ReflectionClass($className);

lib/internal/Magento/Framework/ObjectManager/Resetter/ResetterFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Framework\ObjectManager\Resetter;
99

10+
use Magento\Framework\ObjectManagerInterface;
11+
1012
/**
1113
* Factory that creates Resetter based on environment variable.
1214
*/
@@ -17,15 +19,18 @@ class ResetterFactory
1719
*/
1820
private static string $resetterClassName = Resetter::class;
1921

22+
public function __construct(private ObjectManagerInterface $objectManager)
23+
{
24+
}
25+
2026
/**
21-
* Create resseter factory
27+
* Create resseter instance
2228
*
2329
* @return ResetterInterface
24-
* @phpcs:disable Magento2.Functions.StaticFunction
2530
*/
26-
public static function create() : ResetterInterface
31+
public function create() : ResetterInterface
2732
{
28-
return new static::$resetterClassName;
33+
return $this->objectManager->create(static::$resetterClassName);
2934
}
3035

3136
/**

lib/internal/Magento/Framework/TestFramework/ApplicationStateComparator/Resetter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\TestFramework\ApplicationStateComparator;
99

10+
use Magento\Framework\Component\ComponentRegistrarInterface;
1011
use Magento\Framework\ObjectManager\Resetter\Resetter as OriginalResetter;
1112
use Magento\Framework\ObjectManager\Resetter\WeakMapSorter;
1213
use Magento\Framework\ObjectManagerInterface;
@@ -36,11 +37,11 @@ class Resetter extends OriginalResetter
3637
*
3738
* @return void
3839
*/
39-
public function __construct()
40+
public function __construct(ComponentRegistrarInterface $componentRegistrar, array $classList = [])
4041
{
4142
$this->collectedWeakMap = new WeakMap;
4243
$this->skipListAndFilterList = new SkipListAndFilterList;
43-
parent::__construct();
44+
parent::__construct($componentRegistrar, $classList);
4445
}
4546

4647
/**

0 commit comments

Comments
 (0)