1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Boot\BootloadManager; |
6
|
|
|
|
7
|
|
|
use Spiral\Boot\Attribute\AbstractMethod; |
8
|
|
|
use Spiral\Boot\Attribute\BindMethod; |
9
|
|
|
use Spiral\Boot\Attribute\InjectorMethod; |
10
|
|
|
use Spiral\Boot\Attribute\SingletonMethod; |
11
|
|
|
use Spiral\Boot\Bootloader\BootloaderInterface; |
12
|
|
|
use Spiral\Core\Attribute\Singleton; |
13
|
|
|
use Spiral\Core\Container; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @internal |
17
|
|
|
* @template TAttribute of AbstractMethod |
18
|
|
|
* @template TBootloader of BootloaderInterface |
19
|
|
|
* @implements AttributeResolverInterface<TAttribute, TBootloader> |
20
|
|
|
* @implements AttributeResolverRegistryInterface<TAttribute> |
21
|
|
|
*/ |
22
|
|
|
#[Singleton] |
23
|
|
|
final class AttributeResolver implements AttributeResolverInterface, AttributeResolverRegistryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array<class-string<TAttribute>, AttributeResolverInterface> |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
private array $resolvers = []; |
29
|
|
|
|
30
|
19 |
|
public function __construct(Container $container) |
31
|
|
|
{ |
32
|
|
|
/** @psalm-suppress InvalidArgument */ |
33
|
19 |
|
$this->register(SingletonMethod::class, $container->get(AttributeResolver\SingletonMethodResolver::class)); |
34
|
|
|
/** @psalm-suppress InvalidArgument */ |
35
|
19 |
|
$this->register(BindMethod::class, $container->get(AttributeResolver\BindMethodResolver::class)); |
36
|
|
|
/** @psalm-suppress InvalidArgument */ |
37
|
19 |
|
$this->register(InjectorMethod::class, $container->get(AttributeResolver\InjectorMethodResolver::class)); |
38
|
|
|
} |
39
|
|
|
|
40
|
19 |
|
public function register(string $attribute, AttributeResolverInterface $resolver): void |
41
|
|
|
{ |
42
|
19 |
|
$this->resolvers[$attribute] = $resolver; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return class-string<TAttribute>[] |
|
|
|
|
47
|
|
|
*/ |
48
|
18 |
|
public function getResolvers(): array |
49
|
|
|
{ |
50
|
18 |
|
return \array_keys($this->resolvers); |
51
|
|
|
} |
52
|
|
|
|
53
|
12 |
|
public function resolve(object $attribute, object $service, \ReflectionMethod $method): void |
54
|
|
|
{ |
55
|
12 |
|
$attributeClass = $attribute::class; |
56
|
12 |
|
if (!isset($this->resolvers[$attributeClass])) { |
57
|
|
|
throw new \RuntimeException("No resolver for attribute {$attributeClass}"); |
58
|
|
|
} |
59
|
|
|
|
60
|
12 |
|
$this->resolvers[$attributeClass]->resolve($attribute, $service, $method); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|