@@ -41,6 +41,9 @@ final class Proxy extends ProxyList
4141 /** @var array<string, Instance> */
4242 private array $ instances = [];
4343
44+ /** @var array<string, string> */
45+ private array $ shared = [];
46+
4447 /** @var array<string, string|false> */
4548 private array $ detectedClasses = [];
4649
@@ -89,20 +92,27 @@ public function has(string $interface): bool
8992 return array_key_exists ($ interface , self ::KNOWN_INTERFACE );
9093 }
9194
92- public function create (object $ object , string $ interface , bool $ locked = false ): object
95+ public function create (object $ object , string $ interface , bool $ share = false ): object
9396 {
9497 if ($ this ->has ($ interface ) === self ::HASNT_PROXYABLE_INTERFACE ) {
9598 throw NonExistentInterface::create ($ interface );
9699 }
97100
101+ if (array_key_exists ($ interface , $ this ->shared )) {
102+ return $ this ->instances [$ this ->shared [$ interface ]];
103+ }
104+
98105 /**
99106 * @psalm-suppress EmptyArrayAccess
100107 */
101108 $ class = self ::KNOWN_INTERFACE [$ interface ];
102- $ instance = new Instance ($ object , $ interface , $ locked );
109+ $ instance = new Instance ($ object , $ interface , $ share );
103110 $ hash = $ instance ->class () . '___ ' . spl_object_hash ($ object );
104111
105112 $ this ->instances [$ hash ] = $ instance ;
113+ if ($ share ) {
114+ $ this ->shared [$ interface ] = $ hash ;
115+ }
106116
107117 if ($ this ->counterCreate instanceof Counters) {
108118 $ this ->counterCreate ->counter (new Label ('class ' , $ this ->instances [$ hash ]->class ()), new Label ('interface ' , $ interface ))->incr ();
0 commit comments