Skip to content

Commit 2191ab4

Browse files
committed
Rearrange ContainerInterface and AbstractFacade members for clarity
1 parent a2a1dff commit 2191ab4

File tree

2 files changed

+92
-87
lines changed

2 files changed

+92
-87
lines changed

src/Toolkit/Core/AbstractFacade.php

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Util/Container/ContainerInterface.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,25 @@ public function getName(string $id): string;
124124
public function has(string $id): bool;
125125

126126
/**
127-
* Check if a service provider is registered with the container
127+
* Check if a shared service is bound to the container
128128
*
129129
* @param class-string $id
130130
*/
131-
public function hasProvider(string $id): bool;
131+
public function hasSingleton(string $id): bool;
132132

133133
/**
134-
* Check if a shared service is bound to the container
134+
* Check if a service resolves to a shared instance
135135
*
136136
* @param class-string $id
137137
*/
138-
public function hasSingleton(string $id): bool;
138+
public function hasInstance(string $id): bool;
139139

140140
/**
141-
* Check if a service resolves to a shared instance
141+
* Check if a service provider is registered with the container
142142
*
143143
* @param class-string $id
144144
*/
145-
public function hasInstance(string $id): bool;
145+
public function hasProvider(string $id): bool;
146146

147147
/**
148148
* Bind a service to the container
@@ -222,6 +222,30 @@ public function singletonIf(
222222
array $args = []
223223
): self;
224224

225+
/**
226+
* Bind a shared instance to the container
227+
*
228+
* @template TService of object
229+
* @template T of TService
230+
*
231+
* @param class-string<TService> $id
232+
* @param T $instance
233+
* @return $this
234+
*/
235+
public function instance(string $id, $instance): self;
236+
237+
/**
238+
* Bind a shared instance to the container if it isn't already bound
239+
*
240+
* @template TService of object
241+
* @template T of TService
242+
*
243+
* @param class-string<TService> $id
244+
* @param T $instance
245+
* @return $this
246+
*/
247+
public function instanceIf(string $id, $instance): self;
248+
225249
/**
226250
* Register a contextual binding with the container
227251
*
@@ -301,30 +325,6 @@ public function providers(
301325
*/
302326
public function getProviders(): array;
303327

304-
/**
305-
* Bind a shared instance to the container
306-
*
307-
* @template TService of object
308-
* @template T of TService
309-
*
310-
* @param class-string<TService> $id
311-
* @param T $instance
312-
* @return $this
313-
*/
314-
public function instance(string $id, $instance): self;
315-
316-
/**
317-
* Bind a shared instance to the container if it isn't already bound
318-
*
319-
* @template TService of object
320-
* @template T of TService
321-
*
322-
* @param class-string<TService> $id
323-
* @param T $instance
324-
* @return $this
325-
*/
326-
public function instanceIf(string $id, $instance): self;
327-
328328
/**
329329
* Remove a binding from the container
330330
*

0 commit comments

Comments
 (0)