Passed
Pull Request — master (#963)
by butschster
09:56
created

AttributesBootloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 0
Metric Value
wmc 8
eloc 25
dl 0
loc 64
ccs 29
cts 32
cp 0.9063
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initInstantiator() 0 8 2
A __construct() 0 3 1
A init() 0 7 1
A initReader() 0 30 4
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\Boot\EnvironmentInterface;
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 368
    public function __construct(
32
        private readonly ConfiguratorInterface $config,
33
    ) {
34 368
    }
35
36 368
    public function init(EnvironmentInterface $env): void
37
    {
38 368
        $this->config->setDefaults(
39 368
            AttributesConfig::CONFIG,
40 368
            [
41 368
                'annotations' => [
42 368
                    'support' => $env->get('SUPPORT_ANNOTATIONS', true),
43 368
                ],
44 368
            ],
45 368
        );
46
    }
47
48 368
    private function initInstantiator(AttributesConfig $config): InstantiatorInterface
49
    {
50 368
        if ($config->isAnnotationsReaderEnabled()) {
51 366
            return new Facade();
52
        }
53
54
        /** @psalm-suppress InternalClass */
55 2
        return new NamedArgumentsInstantiator();
56
    }
57
58 368
    private function initReader(
59
        ContainerInterface $container,
60
        InstantiatorInterface $instantiator,
61
        AttributesConfig $config,
62
    ): ReaderInterface {
63 368
        $reader = new AttributeReader($instantiator);
64
65 368
        if ($container->has(CacheInterface::class)) {
66 295
            $cache = $container->get(CacheInterface::class);
67 295
            \assert($cache instanceof CacheInterface);
68
69 295
            $reader = new Psr16CachedReader($reader, $cache);
70
        }
71
72 368
        $supportAnnotations = $config->isAnnotationsReaderEnabled();
73
74 368
        if ($supportAnnotations) {
75 366
            if (!\interface_exists(DoctrineReaderInterface::class)) {
76
                throw new InitializationException(
77
                    'Doctrine annotations reader is not available, please install "doctrine/annotations" package',
78
                );
79
            }
80
81 366
            $reader = new SelectiveReader([
82 366
                $reader,
83 366
                new AnnotationReader(new DoctrineAnnotationReader()),
84 366
            ]);
85
        }
86
87 368
        return $reader;
88
    }
89
}
90