8
8
namespace Magento \Framework \ObjectManager \Resetter ;
9
9
10
10
use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \Component \ComponentRegistrar ;
12
+ use Magento \Framework \Component \ComponentRegistrarInterface ;
11
13
use Magento \Framework \ObjectManager \ResetAfterRequestInterface ;
12
14
use Magento \Framework \ObjectManagerInterface ;
13
15
use WeakMap ;
17
19
*/
18
20
class Resetter implements ResetterInterface
19
21
{
20
- public const RESET_PATH = '/app/etc/reset.php ' ;
22
+ public const RESET_PATH = 'reset.json ' ;
23
+ private const RESET_STATE_METHOD = '_resetState ' ;
21
24
22
25
/** @var WeakMap instances to be reset after request */
23
26
private WeakMap $ resetAfterWeakMap ;
@@ -28,21 +31,6 @@ class Resetter implements ResetterInterface
28
31
/** @var WeakMapSorter|null Note: We use temporal coupling here because of chicken/egg during bootstrapping */
29
32
private ?WeakMapSorter $ weakMapSorter = null ;
30
33
31
- /**
32
- * @var array
33
- *
34
- */
35
- private array $ classList = [
36
- //phpcs:disable Magento2.PHP.LiteralNamespaces
37
- 'Magento\Framework\GraphQl\Query\Fields ' => true ,
38
- 'Magento\Store\Model\Store ' => [
39
- "_baseUrlCache " => [],
40
- "_configCache " => null ,
41
- "_configCacheBaseNodes " => [],
42
- "_dirCache " => [],
43
- "_urlCache " => []
44
- ]
45
- ];
46
34
47
35
/**
48
36
* @var array
@@ -55,15 +43,34 @@ class Resetter implements ResetterInterface
55
43
* @return void
56
44
* @phpcs:disable Magento2.Functions.DiscouragedFunction
57
45
*/
58
- public function __construct ()
59
- {
60
- if (\file_exists (BP . self ::RESET_PATH )) {
61
- // phpcs:ignore Magento2.Security.IncludeFile.FoundIncludeFile
62
- $ this ->classList = array_replace ($ this ->classList , (require BP . self ::RESET_PATH ));
46
+ public function __construct (
47
+ private ComponentRegistrarInterface $ componentRegistrar ,
48
+ private array $ classList = [],
49
+ ) {
50
+ foreach ($ this ->getPaths () as $ resetPath ) {
51
+ if (!\file_exists ($ resetPath )) {
52
+ continue ;
53
+ }
54
+ $ resetData = \json_decode (\file_get_contents ($ resetPath ), true );
55
+ $ this ->classList = array_replace ($ this ->classList , $ resetData );
63
56
}
64
57
$ this ->resetAfterWeakMap = new WeakMap ;
65
58
}
66
59
60
+ /**
61
+ * Get paths for reset json
62
+ *
63
+ * @return \Generator<string>
64
+ */
65
+ private function getPaths (): \Generator
66
+ {
67
+ yield BP . '/app/etc/ ' . self ::RESET_PATH ;
68
+ foreach ($ this ->componentRegistrar ->getPaths (ComponentRegistrar::MODULE ) as $ modulePath ) {
69
+ yield $ modulePath . '/etc/ ' . self ::RESET_PATH ;
70
+ }
71
+ }
72
+
73
+
67
74
/**
68
75
* Add instance to be reset later
69
76
*
@@ -72,7 +79,10 @@ public function __construct()
72
79
*/
73
80
public function addInstance (object $ instance ) : void
74
81
{
75
- if ($ instance instanceof ResetAfterRequestInterface || isset ($ this ->classList [\get_class ($ instance )])) {
82
+ if ($ instance instanceof ResetAfterRequestInterface
83
+ || \method_exists ($ instance , self ::RESET_STATE_METHOD )
84
+ || isset ($ this ->classList [\get_class ($ instance )])
85
+ ) {
76
86
$ this ->resetAfterWeakMap [$ instance ] = true ;
77
87
}
78
88
}
@@ -124,6 +134,10 @@ public function setObjectManager(ObjectManagerInterface $objectManager) : void
124
134
*/
125
135
private function resetStateWithReflection (object $ instance )
126
136
{
137
+ if (\method_exists ($ instance , self ::RESET_STATE_METHOD )) {
138
+ $ instance ->{self ::RESET_STATE_METHOD }();
139
+ return ;
140
+ }
127
141
$ className = \get_class ($ instance );
128
142
$ reflectionClass = $ this ->reflectionCache [$ className ]
129
143
?? $ this ->reflectionCache [$ className ] = new \ReflectionClass ($ className );
0 commit comments