|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Bootloader\Attributes; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader; |
|
8
|
|
|
use Doctrine\Common\Annotations\Reader as DoctrineReaderInterface; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
11
|
|
|
use Spiral\Attributes\AnnotationReader; |
|
12
|
|
|
use Spiral\Attributes\AttributeReader; |
|
13
|
|
|
use Spiral\Attributes\Composite\SelectiveReader; |
|
14
|
|
|
use Spiral\Attributes\Exception\InitializationException; |
|
15
|
|
|
use Spiral\Attributes\Internal\Instantiator\Facade; |
|
16
|
|
|
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface; |
|
17
|
|
|
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator; |
|
18
|
|
|
use Spiral\Attributes\Psr16CachedReader; |
|
19
|
|
|
use Spiral\Attributes\ReaderInterface; |
|
20
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
21
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
22
|
|
|
|
|
23
|
|
|
class AttributesBootloader extends Bootloader |
|
24
|
|
|
{ |
|
25
|
|
|
protected const SINGLETONS = [ |
|
26
|
|
|
ReaderInterface::class => [self::class, 'initReader'], |
|
27
|
|
|
InstantiatorInterface::class => [self::class, 'initInstantiator'], |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
356 |
|
public function __construct( |
|
31
|
|
|
private readonly ConfiguratorInterface $config, |
|
32
|
|
|
) { |
|
33
|
356 |
|
} |
|
34
|
|
|
|
|
35
|
356 |
|
public function init(): void |
|
36
|
|
|
{ |
|
37
|
356 |
|
$this->config->setDefaults( |
|
38
|
356 |
|
AttributesConfig::CONFIG, |
|
39
|
356 |
|
[ |
|
40
|
356 |
|
'annotations' => [ |
|
41
|
356 |
|
'support' => true, |
|
42
|
356 |
|
], |
|
43
|
356 |
|
], |
|
44
|
356 |
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
356 |
|
private function initInstantiator(AttributesConfig $config): InstantiatorInterface |
|
48
|
|
|
{ |
|
49
|
356 |
|
if ($config->isAnnotationsReaderEnabled()) { |
|
50
|
355 |
|
return new Facade(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @psalm-suppress InternalClass */ |
|
54
|
1 |
|
return new NamedArgumentsInstantiator(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
356 |
|
private function initReader( |
|
58
|
|
|
ContainerInterface $container, |
|
59
|
|
|
InstantiatorInterface $instantiator, |
|
60
|
|
|
AttributesConfig $config, |
|
61
|
|
|
): ReaderInterface { |
|
62
|
356 |
|
$reader = new AttributeReader($instantiator); |
|
63
|
|
|
|
|
64
|
356 |
|
if ($container->has(CacheInterface::class)) { |
|
65
|
291 |
|
$cache = $container->get(CacheInterface::class); |
|
66
|
291 |
|
\assert($cache instanceof CacheInterface); |
|
67
|
|
|
|
|
68
|
291 |
|
$reader = new Psr16CachedReader($reader, $cache); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
356 |
|
if ($config->isAnnotationsReaderEnabled()) { |
|
72
|
355 |
|
if (!\interface_exists(DoctrineReaderInterface::class)) { |
|
73
|
|
|
throw new InitializationException( |
|
74
|
|
|
'Doctrine annotations reader is not available, please install "doctrine/annotations" package', |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
355 |
|
$reader = new SelectiveReader([ |
|
79
|
355 |
|
$reader, |
|
80
|
355 |
|
new AnnotationReader(new DoctrineAnnotationReader()), |
|
81
|
355 |
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
356 |
|
return $reader; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|