-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReverseContainer.php
96 lines (83 loc) · 3.04 KB
/
ReverseContainer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Drupal\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
/**
* Retrieves service IDs from the container for public services.
*
* Heavily inspired by \Symfony\Component\DependencyInjection\ReverseContainer.
*/
final class ReverseContainer {
/**
* A closure on the container that can search for services.
*
* @var \Closure
*/
private \Closure $getServiceId;
/**
* A static map of services to a hash.
*
* @var array
*/
private static array $recordedServices = [];
/**
* Constructs a ReverseContainer object.
*
* @param \Drupal\Component\DependencyInjection\Container|\Symfony\Component\DependencyInjection\Container $serviceContainer
* The service container.
*/
public function __construct(private readonly Container|SymfonyContainer $serviceContainer) {
$this->getServiceId = \Closure::bind(function ($service): ?string {
return array_search($service, $this->services, TRUE) ?: NULL;
}, $serviceContainer, $serviceContainer);
}
/**
* Returns the ID of the passed object when it exists as a service.
*
* To be reversible, services need to be public.
*
* @param object $service
* The service to find the ID for.
*/
public function getId(object $service): ?string {
if ($this->serviceContainer === $service || $service instanceof SymfonyContainerInterface) {
return 'service_container';
}
$hash = $this->generateServiceIdHash($service);
$id = self::$recordedServices[$hash] ?? ($this->getServiceId)($service);
if ($id !== NULL && $this->serviceContainer->has($id)) {
self::$recordedServices[$hash] = $id;
return $id;
}
return NULL;
}
/**
* Records a map of the container's services.
*
* This method is used so that stale services can be serialized after a
* container has been re-initialized.
*/
public function recordContainer(): void {
$service_recorder = \Closure::bind(function () : array {
return $this->services;
}, $this->serviceContainer, $this->serviceContainer);
self::$recordedServices = array_merge(self::$recordedServices, array_flip(array_map([$this, 'generateServiceIdHash'], $service_recorder())));
}
/**
* Generates an identifier for a service based on the object class and hash.
*
* @param object $object
* The object to generate an identifier for.
*
* @return string
* The object's class and hash concatenated together.
*/
private function generateServiceIdHash(object $object): string {
// Include class name as an additional namespace for the hash since
// spl_object_hash's return can be recycled. This still is not a 100%
// guarantee to be unique but makes collisions incredibly difficult and even
// then the interface would be preserved.
// @see https://php.net/spl_object_hash#refsect1-function.spl-object-hash-notes
return get_class($object) . spl_object_hash($object);
}
}