|
7 | 7 | use ArrayIterator; |
8 | 8 | use Closure; |
9 | 9 | use ReflectionFunction; |
| 10 | +use Tempest\Container\Exceptions\DecoratorDidNotImplementInterface; |
10 | 11 | use Tempest\Container\Exceptions\DependencyCouldNotBeAutowired; |
11 | 12 | use Tempest\Container\Exceptions\DependencyCouldNotBeInstantiated; |
12 | 13 | use Tempest\Container\Exceptions\InvokedCallableWasInvalid; |
@@ -35,6 +36,9 @@ public function __construct( |
35 | 36 |
|
36 | 37 | /** @var ArrayIterator<array-key, class-string> $dynamicInitializers */ |
37 | 38 | private ArrayIterator $dynamicInitializers = new ArrayIterator(), |
| 39 | + |
| 40 | + /** @var ArrayIterator<array-key, class-string[]> $decorators */ |
| 41 | + private ArrayIterator $decorators = new ArrayIterator(), |
38 | 42 | private ?DependencyChain $chain = null, |
39 | 43 | ) {} |
40 | 44 |
|
@@ -66,6 +70,13 @@ public function setDynamicInitializers(array $dynamicInitializers): self |
66 | 70 | return $this; |
67 | 71 | } |
68 | 72 |
|
| 73 | + public function setDecorators(array $decorators): self |
| 74 | + { |
| 75 | + $this->decorators = new ArrayIterator($decorators); |
| 76 | + |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
69 | 80 | public function getDefinitions(): array |
70 | 81 | { |
71 | 82 | return $this->definitions->getArrayCopy(); |
@@ -99,6 +110,11 @@ public function getDynamicInitializers(): array |
99 | 110 | return $this->dynamicInitializers->getArrayCopy(); |
100 | 111 | } |
101 | 112 |
|
| 113 | + public function getDecorators(): array |
| 114 | + { |
| 115 | + return $this->decorators->getArrayCopy(); |
| 116 | + } |
| 117 | + |
102 | 118 | public function register(string $className, callable $definition): self |
103 | 119 | { |
104 | 120 | $this->definitions[$className] = $definition; |
@@ -299,7 +315,28 @@ public function removeInitializer(ClassReflector|string $initializerClass): Cont |
299 | 315 | return $this; |
300 | 316 | } |
301 | 317 |
|
| 318 | + public function addDecorator(ClassReflector|string $decoratorClass, ClassReflector|string $decoratedClass): Container |
| 319 | + { |
| 320 | + $decoratorClass = is_string($decoratorClass) ? $decoratorClass : $decoratorClass->getName(); |
| 321 | + $decoratedClass = is_string($decoratedClass) ? $decoratedClass : $decoratedClass->getName(); |
| 322 | + |
| 323 | + $this->decorators[$decoratedClass][] = $decoratorClass; |
| 324 | + |
| 325 | + return $this; |
| 326 | + } |
| 327 | + |
302 | 328 | private function resolve(string $className, null|string|UnitEnum $tag = null, mixed ...$params): ?object |
| 329 | + { |
| 330 | + $instance = $this->resolveDependency($className, $tag, ...$params); |
| 331 | + |
| 332 | + if ($this->decorators[$className] ?? null) { |
| 333 | + $instance = $this->resolveDecorator($className, $instance, $tag, ...$params); |
| 334 | + } |
| 335 | + |
| 336 | + return $instance; |
| 337 | + } |
| 338 | + |
| 339 | + private function resolveDependency(string $className, null|string|UnitEnum $tag = null, mixed ...$params): ?object |
303 | 340 | { |
304 | 341 | $class = new ClassReflector($className); |
305 | 342 |
|
@@ -608,4 +645,35 @@ private function resolveTaggedName(string $className, null|string|UnitEnum $tag) |
608 | 645 | ? "{$className}#{$tag}" |
609 | 646 | : $className; |
610 | 647 | } |
| 648 | + |
| 649 | + private function resolveDecorator(string $className, mixed $instance, null|string|UnitEnum $tag = null, mixed ...$params): ?object |
| 650 | + { |
| 651 | + foreach ($this->decorators[$className] ?? [] as $decoratorClass) { |
| 652 | + $decoratorClassReflector = new ClassReflector($decoratorClass); |
| 653 | + $constructor = $decoratorClassReflector->getConstructor(); |
| 654 | + $parameters = $constructor?->getParameters(); |
| 655 | + |
| 656 | + // we look for parameter holding decorated instance |
| 657 | + foreach ($parameters ?? [] as $parameter) { |
| 658 | + if ($parameter->getType()->matches($className) === false) { |
| 659 | + continue; |
| 660 | + } |
| 661 | + |
| 662 | + // we bind the decorated instance to the parameter, so container won't try to resolve it (it would end up as circular dependency) |
| 663 | + $params[$parameter->getName()] = $instance; |
| 664 | + |
| 665 | + break; |
| 666 | + } |
| 667 | + |
| 668 | + $decorator = $this->resolveDependency($decoratorClass, $tag, ...$params); |
| 669 | + |
| 670 | + if (! $decorator instanceof $className) { |
| 671 | + throw new DecoratorDidNotImplementInterface($className, $decoratorClass); |
| 672 | + } |
| 673 | + |
| 674 | + $instance = $decorator; |
| 675 | + } |
| 676 | + |
| 677 | + return $instance; |
| 678 | + } |
611 | 679 | } |
0 commit comments