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 Yii; |
8
|
|
|
use yii\base\Component; |
9
|
|
|
use yii\base\InvalidConfigException; |
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
|
|
|
* @throws InvalidConfigException |
63
|
|
|
*/ |
64
|
|
|
public function init(): void |
65
|
|
|
{ |
66
|
|
|
parent::init(); |
67
|
|
|
$this->registerLoader(); |
68
|
|
|
$this->enableCacheComponent(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return AnnotationCacheReader |
73
|
|
|
* @throws AnnotationException |
74
|
|
|
*/ |
75
|
|
|
public function getReader(): AnnotationCacheReader |
76
|
|
|
{ |
77
|
|
|
$this->enableNewReader(); |
78
|
|
|
return new AnnotationCacheReader( |
79
|
|
|
$this->reader, |
80
|
|
|
new AnnotationCache($this->cacheComponent), |
81
|
|
|
$this->debug |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return void |
87
|
|
|
* @throws InvalidConfigException |
88
|
|
|
*/ |
89
|
|
|
private function enableCacheComponent(): void |
90
|
|
|
{ |
91
|
|
|
$this->cacheComponent = (is_string($this->cache) |
92
|
|
|
? Instance::ensure($this->cache) |
93
|
|
|
: $this->cache) ?? $this->getDefaultCache(); |
94
|
|
|
$this->configurationCache(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return default cache |
99
|
|
|
*/ |
100
|
|
|
private function getDefaultCache() |
101
|
|
|
{ |
102
|
|
|
return self::DEFAULT_CACHE; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
private function configurationCache(): void |
109
|
|
|
{ |
110
|
|
|
$this->trySetCachePath(); |
111
|
|
|
$this->trySetCacheFileSuffix(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
private function trySetCachePath(): void |
118
|
|
|
{ |
119
|
|
|
if (property_exists($this->cacheComponent, 'cachePath') && $this->path !== null) { |
120
|
|
|
$this->cacheComponent->cachePath = Yii::getAlias($this->path); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
private function trySetCacheFileSuffix(): void |
128
|
|
|
{ |
129
|
|
|
if (property_exists($this->cacheComponent, 'cacheFileSuffix')) { |
130
|
|
|
$this->cacheComponent->cacheFileSuffix = static::CACHE_PREFIX; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
*/ |
137
|
|
|
private function registerLoader() |
138
|
|
|
{ |
139
|
|
|
if (method_exists(AnnotationRegistry::class, 'registerLoader')) { |
140
|
|
|
/** @noinspection PhpDeprecationInspection */ |
141
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return void |
147
|
|
|
* @throws AnnotationException |
148
|
|
|
*/ |
149
|
|
|
private function enableNewReader(): void |
150
|
|
|
{ |
151
|
|
|
$this->reader = new AnnotationReader(); |
152
|
|
|
foreach ($this->ignoreAnnotations as $annotation) { |
153
|
|
|
$this->reader::addGlobalIgnoredName($annotation); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.