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 Doctrine\Common\Annotations\DocParser; |
8
|
|
|
use Exception; |
9
|
|
|
use Yii; |
10
|
|
|
use yii\base\Component; |
11
|
|
|
use yii\caching\CacheInterface; |
12
|
|
|
use yii\caching\FileCache; |
13
|
|
|
use yii\di\Instance; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Annotations |
17
|
|
|
* @property CacheInterface|object $cacheComponent |
18
|
|
|
* @property FileCache $defaultCache |
19
|
|
|
*/ |
20
|
|
|
class Annotations extends Component implements AnnotationsInterface, ParserInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string|CacheInterface |
24
|
|
|
*/ |
25
|
|
|
public $cache = 'cache'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
public $debug = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $path; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AnnotationReader |
39
|
|
|
*/ |
40
|
|
|
private $reader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
public $ignoreAnnotations = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var CacheInterface |
49
|
|
|
*/ |
50
|
|
|
private $cacheComponent; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var FileCache |
54
|
|
|
*/ |
55
|
|
|
private const DEFAULT_CACHE = FileCache::class; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected const CACHE_PREFIX = '.annotations'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
*/ |
64
|
19 |
|
public function init(): void |
65
|
|
|
{ |
66
|
19 |
|
parent::init(); |
67
|
19 |
|
$this->registerLoader(); |
68
|
19 |
|
$this->enableCacheComponent(); |
69
|
19 |
|
} |
70
|
|
|
|
71
|
|
|
/** {@inheritDoc} */ |
72
|
11 |
|
public function getReader(): AnnotationCacheReader |
73
|
|
|
{ |
74
|
11 |
|
$this->enableNewReader(); |
75
|
11 |
|
return new AnnotationCacheReader( |
76
|
11 |
|
$this->reader, |
77
|
11 |
|
new AnnotationCache($this->cacheComponent), |
78
|
11 |
|
$this->debug |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
19 |
|
private function enableCacheComponent(): void |
86
|
|
|
{ |
87
|
19 |
|
$this->cacheComponent = (is_string($this->cache) |
88
|
19 |
|
? $this->tryInstantiateCache() |
89
|
19 |
|
: $this->cache) ?? $this->getDefaultCache(); |
90
|
19 |
|
$this->configurationCache(); |
91
|
19 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return object|CacheInterface|null |
95
|
|
|
*/ |
96
|
19 |
|
private function tryInstantiateCache() |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
19 |
|
return Instance::ensure($this->cache); |
100
|
8 |
|
} catch (Exception $e) { |
101
|
8 |
|
return null; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return default cache |
107
|
|
|
*/ |
108
|
8 |
|
private function getDefaultCache() |
109
|
|
|
{ |
110
|
8 |
|
$defaultCache = static::DEFAULT_CACHE; |
111
|
8 |
|
return new $defaultCache(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
19 |
|
private function configurationCache(): void |
118
|
|
|
{ |
119
|
19 |
|
$this->trySetCachePath(); |
120
|
19 |
|
$this->trySetCacheFileSuffix(); |
121
|
19 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
19 |
|
private function trySetCachePath(): void |
127
|
|
|
{ |
128
|
19 |
|
if (property_exists($this->cacheComponent, 'cachePath') && $this->path !== null) { |
129
|
|
|
$this->cacheComponent->cachePath = Yii::getAlias($this->path); |
130
|
|
|
} |
131
|
19 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
19 |
|
private function trySetCacheFileSuffix(): void |
137
|
|
|
{ |
138
|
19 |
|
if (property_exists($this->cacheComponent, 'cacheFileSuffix')) { |
139
|
10 |
|
$this->cacheComponent->cacheFileSuffix = static::CACHE_PREFIX; |
140
|
|
|
} |
141
|
19 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
*/ |
146
|
19 |
|
private function registerLoader() |
147
|
|
|
{ |
148
|
19 |
|
if (method_exists(AnnotationRegistry::class, 'registerLoader')) { |
149
|
|
|
/** @scrutinizer ignore-deprecated */ |
150
|
|
|
/** @noinspection PhpDeprecationInspection */ |
151
|
19 |
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
152
|
|
|
} |
153
|
19 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return void |
157
|
|
|
* @throws AnnotationException |
158
|
|
|
*/ |
159
|
11 |
|
private function enableNewReader(): void |
160
|
|
|
{ |
161
|
11 |
|
$this->reader = new AnnotationReader(); |
162
|
11 |
|
foreach ($this->ignoreAnnotations as $annotation) { |
163
|
|
|
$this->reader::addGlobalIgnoredName($annotation); |
164
|
|
|
} |
165
|
11 |
|
} |
166
|
|
|
|
167
|
|
|
/** {@inheritDoc} */ |
168
|
2 |
|
public function getParser(array $importAnnotations = []): DocParser |
169
|
|
|
{ |
170
|
2 |
|
$parser = new DocParser(); |
171
|
2 |
|
$parser->setImports($importAnnotations); |
172
|
2 |
|
$parser->setIgnoreNotImportedAnnotations(true); |
173
|
2 |
|
return $parser; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.