Completed
Branch ci (96427e)
by Дмитрий
01:33
created

Annotations::tryInstantiateCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace yii\annotations;
3
4
use Doctrine\Common\Annotations\AnnotationException;
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Exception;
8
use Yii;
9
use yii\base\Component;
10
use yii\caching\CacheInterface;
11
use yii\caching\FileCache;
12
use yii\di\Instance;
13
14
/**
15
 * Class Annotations
16
 * @property CacheInterface|object $cacheComponent
17
 * @property FileCache $defaultCache
18
 */
19
class Annotations extends Component implements AnnotationsInterface
20
{
21
    /**
22
     * @var string|CacheInterface
23
     */
24
    public $cache = 'cache';
25
26
    /**
27
     * @var bool
28
     */
29
    public $debug = false;
30
31
    /**
32
     * @var string
33
     */
34
    public $path;
35
36
    /**
37
     * @var AnnotationReader
38
     */
39
    private $reader;
40
41
    /**
42
     * @var array
43
     */
44
    public $ignoreAnnotations = [];
45
46
    /**
47
     * @var CacheInterface
48
     */
49
    private $cacheComponent;
50
51
    /**
52
     * @var FileCache
53
     */
54
    private const DEFAULT_CACHE = FileCache::class;
55
56
    /**
57
     * @var string
58
     */
59
    protected const CACHE_PREFIX = '.annotations';
60
61
    /**
62
     */
63 2
    public function init(): void
64
    {
65 2
        parent::init();
66 2
        $this->registerLoader();
67 2
        $this->enableCacheComponent();
68 2
    }
69
70
    /**
71
     * @return AnnotationCacheReader
72
     * @throws AnnotationException
73
     */
74 1
    public function getReader(): AnnotationCacheReader
75
    {
76 1
        $this->enableNewReader();
77 1
        return new AnnotationCacheReader(
78 1
            $this->reader,
79 1
            new AnnotationCache($this->cacheComponent),
80 1
            $this->debug
81
        );
82
    }
83
84
    /**
85
     * @return void
86
     */
87 2
    private function enableCacheComponent(): void
88
    {
89 2
        $this->cacheComponent = (is_string($this->cache)
90 2
                ? $this->tryInstantiateCache()
91 2
                : $this->cache) ?? $this->getDefaultCache();
92 2
        $this->configurationCache();
93 2
    }
94
95
    /**
96
     * @return object|CacheInterface|null
97
     */
98 2
    private function tryInstantiateCache()
99
    {
100
        try {
101 2
            return Instance::ensure($this->cache);
102 2
        } catch (Exception $e) {
103 2
            return null;
104
        }
105
    }
106
107
    /**
108
     * Return default cache
109
     */
110 2
    private function getDefaultCache()
111
    {
112 2
        $defaultCache = static::DEFAULT_CACHE;
113 2
        return new $defaultCache();
114
    }
115
116
    /**
117
     * @return void
118
     */
119 2
    private function configurationCache(): void
120
    {
121 2
        $this->trySetCachePath();
122 2
        $this->trySetCacheFileSuffix();
123 2
    }
124
125
    /**
126
     * @return void
127
     */
128 2
    private function trySetCachePath(): void
129
    {
130 2
        if (property_exists($this->cacheComponent, 'cachePath') && $this->path !== null) {
131
            $this->cacheComponent->cachePath = Yii::getAlias($this->path);
132
        }
133 2
    }
134
135
    /**
136
     * @return void
137
     */
138 2
    private function trySetCacheFileSuffix(): void
139
    {
140 2
        if (property_exists($this->cacheComponent, 'cacheFileSuffix')) {
141 2
            $this->cacheComponent->cacheFileSuffix = static::CACHE_PREFIX;
142
        }
143 2
    }
144
145
    /**
146
     *
147
     */
148 2
    private function registerLoader()
149
    {
150 2
        if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
151
            /** @scrutinizer ignore-deprecated */
152
            /** @noinspection PhpDeprecationInspection */
153 2
            AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

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

153
            /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('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...
154
        }
155 2
    }
156
157
    /**
158
     * @return void
159
     * @throws AnnotationException
160
     */
161 1
    private function enableNewReader(): void
162
    {
163 1
        $this->reader = new AnnotationReader();
164 1
        foreach ($this->ignoreAnnotations as $annotation) {
165
            $this->reader::addGlobalIgnoredName($annotation);
166
        }
167 1
    }
168
}
169