Test Failed
Pull Request — master (#940)
by butschster
08:32
created

AttributesBootloader::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
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 353
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator;
18
use Spiral\Attributes\Psr16CachedReader;
19 353
use Spiral\Attributes\ReaderInterface;
20 353
use Spiral\Boot\Bootloader\Bootloader;
21 353
use Spiral\Config\ConfiguratorInterface;
22 353
23
class AttributesBootloader extends Bootloader
24 353
{
25
    protected const SINGLETONS = [
26
        ReaderInterface::class => [self::class, 'initReader'],
27
        InstantiatorInterface::class => [self::class, 'initInstantiator'],
28
    ];
29 353
30
    public function __construct(
31
        private readonly ConfiguratorInterface $config,
32
    ) {
33
    }
34
35
    public function init(): void
36 353
    {
37 353
        $this->config->setDefaults(
38 353
            AttributesConfig::CONFIG,
39
            [
40
                'annotations' => [
41
                    'support' => true,
42
                ],
43
            ],
44
        );
45
    }
46
47
    private function initInstantiator(AttributesConfig $config): InstantiatorInterface
48
    {
49
        if ($config->isAnnotationsReaderEnabled()) {
50
            return new Facade();
51
        }
52
53
        /** @psalm-suppress InternalClass */
54
        return new NamedArgumentsInstantiator();
55
    }
56
57
    private function initReader(
58
        ContainerInterface $container,
59
        InstantiatorInterface $instantiator,
60
        AttributesConfig $config,
61
    ): ReaderInterface {
62
        $reader = new AttributeReader($instantiator);
63
64
        if ($container->has(CacheInterface::class)) {
65
            $cache = $container->get(CacheInterface::class);
66
            \assert($cache instanceof CacheInterface);
67
68
            $reader = new Psr16CachedReader($reader, $cache);
69
        }
70
71
        if ($config->isAnnotationsReaderEnabled()) {
72
            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
            $reader = new SelectiveReader([
79
                $reader,
80
                new AnnotationReader(new DoctrineAnnotationReader()),
81
            ]);
82
        }
83
84
        return $reader;
85
    }
86
}
87