|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Micro framework package. |
| 7 | + * |
| 8 | + * (c) Stanislau Komar <kost@micro-php.net> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Micro\Component\DependencyInjection; |
| 15 | + |
| 16 | +use Micro\Component\DependencyInjection\Exception\ServiceRegistrationException; |
| 17 | +use Micro\Component\DependencyInjection\Proxy\ProxyBuilderInterface; |
| 18 | +use Micro\Component\DependencyInjection\Proxy\ProxyClassNameGeneratorInterface; |
| 19 | +use Psr\Container\ContainerInterface as PsrContainerInterface; |
| 20 | + |
| 21 | +/** @psalm-suppress UnusedClass */ |
| 22 | +final class ContainerCompiled extends Container implements ContainerRegistryCompiledInterface |
| 23 | +{ |
| 24 | + private bool $isCompiled; |
| 25 | + |
| 26 | + private PsrContainerInterface $decorated; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + private readonly ProxyClassNameGeneratorInterface $classNameGenerator, |
| 30 | + private readonly ProxyBuilderInterface $proxyBuilder, |
| 31 | + ?PsrContainerInterface $decorated = null, |
| 32 | + ) { |
| 33 | + $this->isCompiled = false; |
| 34 | + $this->decorated = $decorated ?? new Container(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @template T of object |
| 39 | + * |
| 40 | + * @param class-string<T> $id |
| 41 | + * |
| 42 | + * @psalm-suppress MoreSpecificImplementedParamType |
| 43 | + * |
| 44 | + * @return T |
| 45 | + */ |
| 46 | + #[\Override] |
| 47 | + public function get(string $id): object |
| 48 | + { |
| 49 | + $proxyClass = $this->classNameGenerator->createProxyClassName($id); |
| 50 | + if (!class_exists($proxyClass)) { |
| 51 | + /* @psalm-suppress MixedReturnStatement */ |
| 52 | + return $this->decorated->get($id); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @var T $instance |
| 57 | + * |
| 58 | + * @psalm-suppress MixedMethodCall |
| 59 | + */ |
| 60 | + $instance = new $proxyClass($this); |
| 61 | + |
| 62 | + return $instance; |
| 63 | + } |
| 64 | + |
| 65 | + #[\Override] |
| 66 | + public function has(string $id): bool |
| 67 | + { |
| 68 | + return $this->decorated->has($id); |
| 69 | + } |
| 70 | + |
| 71 | + #[\Override] |
| 72 | + public function register(string $id, callable $service, bool $force = false): void |
| 73 | + { |
| 74 | + if (!$this->decorated instanceof ContainerRegistryInterface) { |
| 75 | + throw new ServiceRegistrationException('Container can not decorate service because container does not implement '.ContainerRegistryInterface::class); |
| 76 | + } |
| 77 | + |
| 78 | + if ($this->isCompiled) { |
| 79 | + throw new ServiceRegistrationException('Can not register service because container already compiled.'); |
| 80 | + } |
| 81 | + |
| 82 | + if (interface_exists($id) || class_exists($id)) { |
| 83 | + $this->proxyBuilder->add($id); |
| 84 | + } |
| 85 | + |
| 86 | + /** @psalm-suppress ArgumentTypeCoercion */ |
| 87 | + $this->decorated->register($id, $service, $force); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @psalm-suppress InvalidPropertyAssignmentValue |
| 92 | + */ |
| 93 | + #[\Override] |
| 94 | + public function decorate(string $id, callable $service, int $priority = 0): void |
| 95 | + { |
| 96 | + if (!$this->decorated instanceof ContainerDecoratorInterface) { |
| 97 | + throw new ServiceRegistrationException('Container can not decorate service because container does not implement '.ContainerDecoratorInterface::class); |
| 98 | + } |
| 99 | + |
| 100 | + if ($this->isCompiled) { |
| 101 | + throw new ServiceRegistrationException('Can not register service because container already compiled.'); |
| 102 | + } |
| 103 | + |
| 104 | + if (interface_exists($id) || class_exists($id)) { |
| 105 | + $this->proxyBuilder->add($id); |
| 106 | + } |
| 107 | + |
| 108 | + /** @psalm-suppress ArgumentTypeCoercion */ |
| 109 | + $this->decorated->decorate($id, $service, $priority); |
| 110 | + } |
| 111 | + |
| 112 | + #[\Override] |
| 113 | + public function compile(): void |
| 114 | + { |
| 115 | + $this->proxyBuilder->build(); |
| 116 | + |
| 117 | + $this->isCompiled = true; |
| 118 | + } |
| 119 | +} |
0 commit comments