Completed
Pull Request — master (#18)
by Дмитрий
02:24
created

Annotations::trySetCachePath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
crap 3.3332
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 17
    public function init(): void
64
    {
65 17
        parent::init();
66 17
        $this->registerLoader();
67 17
        $this->enableCacheComponent();
68 17
    }
69
70
    /**
71
     * @return AnnotationCacheReader
72
     * @throws AnnotationException
73
     */
74 11
    public function getReader(): AnnotationCacheReader
75
    {
76 11
        $this->enableNewReader();
77 11
        return new AnnotationCacheReader(
78 11
            $this->reader,
79 11
            new AnnotationCache($this->cacheComponent),
80 11
            $this->debug
81
        );
82
    }
83
84
    /**
85
     * @return void
86
     */
87 17
    private function enableCacheComponent(): void
88
    {
89 17
        $this->cacheComponent = (is_string($this->cache)
90 17
                ? $this->tryInstantiateCache()
91 17
                : $this->cache) ?? $this->getDefaultCache();
92 17
        $this->configurationCache();
93 17
    }
94
95
    /**
96
     * @return object|CacheInterface|null
97
     */
98 17
    private function tryInstantiateCache()
99
    {
100
        try {
101 17
            return Instance::ensure($this->cache);
102 6
        } catch (Exception $e) {
103 6
            return null;
104
        }
105
    }
106
107
    /**
108
     * Return default cache
109
     */
110 6
    private function getDefaultCache()
111
    {
112 6
        $defaultCache = static::DEFAULT_CACHE;
113 6
        return new $defaultCache();
114
    }
115
116
    /**
117
     * @return void
118
     */
119 17
    private function configurationCache(): void
120
    {
121 17
        $this->trySetCachePath();
122 17
        $this->trySetCacheFileSuffix();
123 17
    }
124
125
    /**
126
     * @return void
127
     */
128 17
    private function trySetCachePath(): void
129
    {
130 17
        if (property_exists($this->cacheComponent, 'cachePath') && $this->path !== null) {
131
            $this->cacheComponent->cachePath = Yii::getAlias($this->path);
132
        }
133 17
    }
134
135
    /**
136
     * @return void
137
     */
138 17
    private function trySetCacheFileSuffix(): void
139
    {
140 17
        if (property_exists($this->cacheComponent, 'cacheFileSuffix')) {
141 8
            $this->cacheComponent->cacheFileSuffix = static::CACHE_PREFIX;
142
        }
143 17
    }
144
145
    /**
146
     *
147
     */
148 17
    private function registerLoader()
149
    {
150 17
        if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
151
            /** @scrutinizer ignore-deprecated */
152
            /** @noinspection PhpDeprecationInspection */
153 17
            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 17
    }
156
157
    /**
158
     * @return void
159
     * @throws AnnotationException
160
     */
161 11
    private function enableNewReader(): void
162
    {
163 11
        $this->reader = new AnnotationReader();
164 11
        foreach ($this->ignoreAnnotations as $annotation) {
165
            $this->reader::addGlobalIgnoredName($annotation);
166
        }
167 11
    }
168
}
169