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

AttributesBootloader::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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