Test Failed
Push — master ( a85b44...a25a4c )
by Divine Niiquaye
11:17
created

AnnotationServiceProvider::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Provider;
19
20
use Biurad\Annotations\AnnotationLoader;
21
use Biurad\Annotations\ListenerInterface;
22
use Composer\Autoload\ClassLoader;
23
use Doctrine\Common\Annotations\AnnotationReader;
24
use Doctrine\Common\Annotations\AnnotationRegistry;
25
use Doctrine\Common\Annotations\CachedReader;
26
use Doctrine\Common\Annotations\Reader;
27
use Rade\API\BootableProviderInterface;
28
use Rade\Application;
29
use Rade\DI\Container;
30
use Rade\DI\ServiceProviderInterface;
31
use Spiral\Attributes\AnnotationReader as DoctrineReader;
32
use Spiral\Attributes\AttributeReader;
33
use Spiral\Attributes\Composite\MergeReader;
34
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
35
use Symfony\Component\Config\Definition\ConfigurationInterface;
36
37
class AnnotationServiceProvider implements ConfigurationInterface, ServiceProviderInterface, BootableProviderInterface
38
{
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getName(): string
43
    {
44
        return 'annotation';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getConfigTreeBuilder(): TreeBuilder
51
    {
52
        $treeBuilder = new TreeBuilder($this->getName());
53
54
        $treeBuilder->getRootNode()
55
            ->children()
56
                ->arrayNode('resources')
57
                    ->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
58
                    ->prototype('scalar')->end()
59
                    ->defaultValue([])
60
                ->end()
61
                ->booleanNode('debug')->end()
62
                ->arrayNode('ignores')
63
                    ->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
64
                    ->prototype('scalar')->end()
65
                    ->defaultValue(['persistent', 'serializationVersion', 'inject'])
66
                ->end()
67
                ->scalarNode('cache')->end()
68
            ->end()
69
        ;
70
71
        return $treeBuilder;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function register(Container $app): void
78
    {
79
        $composer = new ClassLoader();
80
        $composer->setPsr4('App\\', $app->parameters['project_dir']);
81
        $composer->register();
82
83
        $config = $app->parameters['annotation'] ?? [];
84
85
        if (!isset($config['debug'])) {
86
            $config['debug'] = $app->parameters['debug'];
87
        }
88
89
        if (null !== $doctrine  = \class_exists(AnnotationReader::class)) {
90
            $app['annotation.doctrine'] = static function (Container $app) use ($config): Reader {
91
                $annotation = new AnnotationReader();
92
93
                foreach ($config['ignores'] as $excluded) {
94
                    $annotation::addGlobalIgnoredName($excluded);
95
                }
96
97
                if (isset($app['cache.doctrine'])) {
98
                    return new CachedReader($annotation, $app['cache.doctrine'], $config['debug']);
99
                }
100
101
                return $annotation;
102
            };
103
        }
104
105
        $app['annotation'] = static function (Container $app) use ($doctrine, $config): AnnotationLoader {
106
            $reader = new AttributeReader();
107
108
            if (null !== $doctrine) {
109
                $reader = new MergeReader([$reader, new DoctrineReader($app['annotation.doctrine'])]);
110
            }
111
112
            $annotation = new AnnotationLoader($reader);
113
            $annotation->attach(...$config['resources']);
114
115
            return $annotation;
116
        };
117
118
        // doctrine/annotations ^1.0 compatibility.
119
        if (\method_exists(AnnotationRegistry::class, 'registerLoader')) {
120
            AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

120
            /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
121
        }
122
123
        unset($app->parameters['annotation']);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function boot(Application $app)
130
    {
131
        $listeners = $app->get(ListenerInterface::class);
132
133
        if (!\is_array($listeners)) {
134
            $listeners = [$listeners];
135
        }
136
137
        $app['annotation']->attachListener(...$listeners);
138
139
        //Build annotations ...
140
        $app['annotation']->build();
141
    }
142
}
143