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 ;
18
20
*/
19
21
class Resetter implements ResetterInterface
20
22
{
21
- public const RESET_PATH = '/app/etc/reset.php ' ;
23
+ public const RESET_PATH = 'reset.json ' ;
24
+ private const RESET_STATE_METHOD = '_resetState ' ;
22
25
23
26
/** @var WeakMap instances to be reset after request */
24
27
private WeakMap $ resetAfterWeakMap ;
@@ -29,21 +32,6 @@ class Resetter implements ResetterInterface
29
32
/** @var WeakMapSorter|null Note: We use temporal coupling here because of chicken/egg during bootstrapping */
30
33
private ?WeakMapSorter $ weakMapSorter = null ;
31
34
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
- ];
47
35
48
36
/**
49
37
* @var array
@@ -56,15 +44,34 @@ class Resetter implements ResetterInterface
56
44
* @return void
57
45
* @phpcs:disable Magento2.Functions.DiscouragedFunction
58
46
*/
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 );
64
57
}
65
58
$ this ->resetAfterWeakMap = new WeakMap ;
66
59
}
67
60
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
+
68
75
/**
69
76
* Add instance to be reset later
70
77
*
@@ -73,7 +80,10 @@ public function __construct()
73
80
*/
74
81
public function addInstance (object $ instance ) : void
75
82
{
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
+ ) {
77
87
$ this ->resetAfterWeakMap [$ instance ] = true ;
78
88
}
79
89
}
@@ -125,6 +135,10 @@ public function setObjectManager(ObjectManagerInterface $objectManager) : void
125
135
*/
126
136
private function resetStateWithReflection (object $ instance )
127
137
{
138
+ if (\method_exists ($ instance , self ::RESET_STATE_METHOD )) {
139
+ $ instance ->{self ::RESET_STATE_METHOD }();
140
+ return ;
141
+ }
128
142
$ className = \get_class ($ instance );
129
143
$ reflectionClass = $ this ->reflectionCache [$ className ]
130
144
?? $ this ->reflectionCache [$ className ] = new \ReflectionClass ($ className );
0 commit comments