| Conditions | 4 |
| Paths | 5 |
| Total Lines | 32 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 22 | public static function create(\ReflectionClass $type, \Stringable|string|null $context): object |
||
| 23 | { |
||
| 24 | $interface = $type->getName(); |
||
| 25 | |||
| 26 | if (!\array_key_exists($interface, self::$cache)) { |
||
| 27 | /** @var class-string<TClass> $className */ |
||
| 28 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
||
| 29 | |||
| 30 | try { |
||
| 31 | $classString = ProxyClassRenderer::renderClass($type, $className); |
||
| 32 | |||
| 33 | eval($classString); |
||
| 34 | } catch (\Throwable $e) { |
||
| 35 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
||
| 36 | } |
||
| 37 | |||
| 38 | $instance = new $className(); |
||
| 39 | (static fn () => $instance::$__container_proxy_alias = $interface)->bindTo(null, $instance::class)(); |
||
| 40 | |||
| 41 | // Store in cache without context |
||
| 42 | self::$cache[$interface] = $instance; |
||
| 43 | } else { |
||
| 44 | /** @var TClass $instance */ |
||
| 45 | $instance = self::$cache[$interface]; |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($context !== null) { |
||
| 49 | $instance = clone $instance; |
||
| 50 | (static fn () => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $instance; |
||
| 54 | } |
||
| 56 |