@@ -29,6 +29,16 @@ abstract class AbstractFacade implements FacadeInterface
2929 /** @use ResolvesServiceLists<TService> */
3030 use ResolvesServiceLists;
3131
32+ /**
33+ * @var array<class-string<static>,TService>
34+ */
35+ private static array $ Instances = [];
36+
37+ /**
38+ * @var array<class-string<static>,int>
39+ */
40+ private static array $ ListenerIds = [];
41+
3242 /**
3343 * Get the facade's underlying class, or an array that maps its underlying
3444 * class to compatible implementations
@@ -40,16 +50,6 @@ abstract class AbstractFacade implements FacadeInterface
4050 */
4151 abstract protected static function getService ();
4252
43- /**
44- * @var array<class-string<static>,TService>
45- */
46- private static array $ Instances = [];
47-
48- /**
49- * @var array<class-string<static>,int>
50- */
51- private static array $ ListenerIds = [];
52-
5353 /**
5454 * @inheritDoc
5555 */
@@ -75,7 +75,7 @@ final public static function load(?object $instance = null): void
7575 */
7676 final public static function swap (object $ instance ): void
7777 {
78- self ::unload ();
78+ self ::doUnload ();
7979 self ::$ Instances [static ::class] = self ::doLoad ($ instance );
8080 }
8181
@@ -89,60 +89,17 @@ final public static function unloadAll(): void
8989 if ($ class === Event::class) {
9090 continue ;
9191 }
92- $ class ::unload ();
92+ $ class ::doUnload ();
9393 }
94- Event::unload ();
94+ Event::doUnload ();
9595 }
9696
9797 /**
9898 * @inheritDoc
9999 */
100100 final public static function unload (): void
101101 {
102- // @phpstan-ignore-next-line
103- if (static ::class === Event::class) {
104- $ loaded = array_keys (self ::$ Instances );
105- if ($ loaded && $ loaded !== [Event::class]) {
106- throw new LogicException (sprintf (
107- '%s cannot be unloaded before other facades ' ,
108- Event::class,
109- ));
110- }
111- }
112-
113- $ id = self ::$ ListenerIds [static ::class] ?? null ;
114- if ($ id !== null ) {
115- Event::removeListener ($ id );
116- unset(self ::$ ListenerIds [static ::class]);
117- }
118-
119- $ instance = self ::$ Instances [static ::class] ?? null ;
120- if (!$ instance ) {
121- return ;
122- }
123-
124- if (static ::class !== App::class) {
125- $ container = Container::maybeGetGlobalContainer ();
126- if ($ container ) {
127- $ serviceName = self ::getServiceName ();
128- if (
129- $ container ->hasInstance ($ serviceName ) &&
130- $ container ->get ($ serviceName ) === $ instance
131- ) {
132- $ container ->unbindInstance ($ serviceName );
133- }
134- }
135- }
136-
137- if ($ instance instanceof FacadeAwareInterface) {
138- $ instance = $ instance ->withoutFacade (static ::class, true );
139- }
140-
141- if ($ instance instanceof Unloadable) {
142- $ instance ->unload ();
143- }
144-
145- unset(self ::$ Instances [static ::class]);
102+ self ::doUnload ();
146103 }
147104
148105 /**
@@ -293,4 +250,52 @@ private static function createInstance(): object
293250 static ::class,
294251 ));
295252 }
253+
254+ private static function doUnload (): void
255+ {
256+ // @phpstan-ignore-next-line
257+ if (static ::class === Event::class) {
258+ $ loaded = array_keys (self ::$ Instances );
259+ if ($ loaded && $ loaded !== [Event::class]) {
260+ throw new LogicException (sprintf (
261+ '%s cannot be unloaded before other facades ' ,
262+ Event::class,
263+ ));
264+ }
265+ }
266+
267+ $ id = self ::$ ListenerIds [static ::class] ?? null ;
268+ if ($ id !== null ) {
269+ Event::removeListener ($ id );
270+ unset(self ::$ ListenerIds [static ::class]);
271+ }
272+
273+ $ instance = self ::$ Instances [static ::class] ?? null ;
274+ if (!$ instance ) {
275+ return ;
276+ }
277+
278+ if (static ::class !== App::class) {
279+ $ container = Container::maybeGetGlobalContainer ();
280+ if ($ container ) {
281+ $ serviceName = self ::getServiceName ();
282+ if (
283+ $ container ->hasInstance ($ serviceName ) &&
284+ $ container ->get ($ serviceName ) === $ instance
285+ ) {
286+ $ container ->unbindInstance ($ serviceName );
287+ }
288+ }
289+ }
290+
291+ if ($ instance instanceof FacadeAwareInterface) {
292+ $ instance = $ instance ->withoutFacade (static ::class, true );
293+ }
294+
295+ if ($ instance instanceof Unloadable) {
296+ $ instance ->unload ();
297+ }
298+
299+ unset(self ::$ Instances [static ::class]);
300+ }
296301}
0 commit comments