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 Spiral\Attributes\AnnotationReader; |
11
|
|
|
use Spiral\Attributes\AttributeReader; |
12
|
|
|
use Spiral\Attributes\Composite\SelectiveReader; |
13
|
|
|
use Spiral\Attributes\Exception\InitializationException; |
14
|
|
|
use Spiral\Attributes\Internal\Instantiator\Facade; |
15
|
|
|
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface; |
16
|
|
|
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator; |
17
|
|
|
use Spiral\Attributes\Psr16CachedReader; |
18
|
|
|
use Spiral\Attributes\ReaderInterface; |
19
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
20
|
|
|
use Spiral\Boot\EnvironmentInterface; |
21
|
|
|
use Spiral\Cache\CacheStorageProviderInterface; |
22
|
|
|
use Spiral\Config\ConfiguratorInterface; |
23
|
|
|
|
24
|
|
|
class AttributesBootloader extends Bootloader |
25
|
|
|
{ |
26
|
|
|
protected const SINGLETONS = [ |
27
|
|
|
ReaderInterface::class => [self::class, 'initReader'], |
28
|
|
|
InstantiatorInterface::class => [self::class, 'initInstantiator'], |
29
|
|
|
]; |
30
|
|
|
|
31
|
393 |
|
public function __construct( |
32
|
|
|
private readonly ConfiguratorInterface $config, |
33
|
|
|
) { |
34
|
393 |
|
} |
35
|
|
|
|
36
|
393 |
|
public function init(EnvironmentInterface $env): void |
37
|
|
|
{ |
38
|
393 |
|
$this->config->setDefaults( |
39
|
393 |
|
AttributesConfig::CONFIG, |
40
|
393 |
|
[ |
41
|
393 |
|
'annotations' => [ |
42
|
393 |
|
'support' => $env->get('SUPPORT_ANNOTATIONS', \interface_exists(DoctrineReaderInterface::class)), |
43
|
393 |
|
], |
44
|
393 |
|
'cache' => [ |
45
|
393 |
|
'storage' => $env->get('ATTRIBUTES_CACHE_STORAGE', null), |
46
|
393 |
|
'enabled' => $env->get('ATTRIBUTES_CACHE_ENABLED', false), |
47
|
393 |
|
], |
48
|
393 |
|
], |
49
|
393 |
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
393 |
|
private function initInstantiator(AttributesConfig $config): InstantiatorInterface |
53
|
|
|
{ |
54
|
393 |
|
if ($config->isAnnotationsReaderEnabled()) { |
55
|
391 |
|
return new Facade(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @psalm-suppress InternalClass */ |
59
|
2 |
|
return new NamedArgumentsInstantiator(); |
60
|
|
|
} |
61
|
|
|
|
62
|
393 |
|
private function initReader( |
63
|
|
|
ContainerInterface $container, |
64
|
|
|
InstantiatorInterface $instantiator, |
65
|
|
|
AttributesConfig $config, |
66
|
|
|
): ReaderInterface { |
67
|
393 |
|
$reader = new AttributeReader($instantiator); |
68
|
|
|
|
69
|
393 |
|
if ($config->isCacheEnabled()) { |
70
|
1 |
|
$provider = $container->get(CacheStorageProviderInterface::class); |
71
|
1 |
|
\assert($provider instanceof CacheStorageProviderInterface); |
72
|
|
|
|
73
|
1 |
|
$reader = new Psr16CachedReader($reader, $provider->storage($config->getCacheStorage())); |
74
|
|
|
} |
75
|
|
|
|
76
|
393 |
|
$supportAnnotations = $config->isAnnotationsReaderEnabled(); |
77
|
|
|
|
78
|
393 |
|
if ($supportAnnotations) { |
79
|
391 |
|
if (!\interface_exists(DoctrineReaderInterface::class)) { |
80
|
|
|
throw new InitializationException( |
|
|
|
|
81
|
|
|
'Doctrine annotations reader is not available, please install "doctrine/annotations" package', |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
391 |
|
$reader = new SelectiveReader([ |
86
|
391 |
|
$reader, |
87
|
391 |
|
new AnnotationReader(new DoctrineAnnotationReader()), |
|
|
|
|
88
|
391 |
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
393 |
|
return $reader; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|