1 | <?php |
||
45 | class Addendum implements LoggerAwareInterface |
||
46 | { |
||
47 | |||
48 | const DefaultInstanceId = 'addendum'; |
||
49 | |||
50 | /** |
||
51 | * Runtime path |
||
52 | * @var string |
||
53 | */ |
||
54 | public $runtimePath = 'runtime'; |
||
55 | |||
56 | /** |
||
57 | * Whether to check modification time of file to invalidate cache. |
||
58 | * Set this to true for development, and false for production. |
||
59 | * @var bool |
||
60 | */ |
||
61 | public $checkMTime = false; |
||
62 | |||
63 | /** |
||
64 | * Namespaces to check for annotations. |
||
65 | * By default global and addendum namespace is included. |
||
66 | * @var string[] |
||
67 | */ |
||
68 | public $namespaces = [ |
||
69 | '\\', |
||
70 | TargetAnnotation::Ns |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * @var bool[] |
||
75 | * @internal Do not use, this for performance only |
||
76 | */ |
||
77 | public $nameKeys = []; |
||
78 | |||
79 | /** |
||
80 | * Translatable annotations |
||
81 | * TODO This should be moved to `maslosoft/addendum-i18n-extractor` |
||
82 | * @var string[] |
||
83 | */ |
||
84 | public $i18nAnnotations = [ |
||
85 | 'Label', |
||
86 | 'Description' |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * Plugins collection |
||
91 | * @var AddendumPlugins|mixed[] |
||
92 | */ |
||
93 | public $plugins = [ |
||
94 | 'matcher' => [ |
||
95 | ClassLiteralMatcher::class => [ |
||
96 | UseResolverDecorator::class |
||
97 | ], |
||
98 | StaticConstantMatcher::class => [ |
||
99 | UseResolverDecorator::class |
||
100 | ], |
||
101 | AnnotationsMatcher::class => [ |
||
102 | DefencerDecorator::class |
||
103 | ] |
||
104 | ] |
||
105 | ]; |
||
106 | |||
107 | /** |
||
108 | * Current instance id |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $instanceId = self::DefaultInstanceId; |
||
112 | |||
113 | /** |
||
114 | * DI |
||
115 | * @var EmbeDi |
||
116 | */ |
||
117 | protected $di = null; |
||
118 | |||
119 | /** |
||
120 | * Logger |
||
121 | * @var LoggerInterface |
||
122 | */ |
||
123 | private $loggerInstance; |
||
124 | |||
125 | /** |
||
126 | * Cache for resolved annotations class names. |
||
127 | * Key is shor annotation name. |
||
128 | * @var string[] |
||
129 | */ |
||
130 | private static $classNames = []; |
||
131 | |||
132 | /** |
||
133 | * This holds information about all declared classes implementing AnnotatedInterface. |
||
134 | * @see AnnotatedInterface |
||
135 | * @var string[] |
||
136 | */ |
||
137 | private static $annotations = []; |
||
138 | |||
139 | /** |
||
140 | * Version holder |
||
141 | * @var string |
||
142 | */ |
||
143 | private static $versionNumber = null; |
||
144 | |||
145 | /** |
||
146 | * Addendum instances |
||
147 | * @var Addendum[] |
||
148 | */ |
||
149 | private static $addendums = []; |
||
150 | |||
151 | /** |
||
152 | * |
||
153 | * @param string $instanceId |
||
154 | */ |
||
155 | 58 | public function __construct($instanceId = self::DefaultInstanceId) |
|
163 | |||
164 | /** |
||
165 | * Get flyweight addendum instance of `Addendum`. |
||
166 | * Only one instance will be created for each `$instanceId` |
||
167 | * |
||
168 | * @param string $instanceId |
||
169 | * @return Addendum |
||
170 | */ |
||
171 | 54 | public static function fly($instanceId = self::DefaultInstanceId) |
|
172 | { |
||
173 | 54 | if (empty(self::$addendums[$instanceId])) |
|
174 | { |
||
175 | self::$addendums[$instanceId] = (new Addendum($instanceId))->init(); |
||
176 | } |
||
177 | 54 | return self::$addendums[$instanceId]; |
|
178 | } |
||
179 | |||
180 | 51 | public function getInstanceId() |
|
184 | |||
185 | /** |
||
186 | * Get current addendum version. |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getVersion() |
||
198 | |||
199 | /** |
||
200 | * Initialize addendum and store configuration. |
||
201 | * This should be called upon first intstance creation. |
||
202 | * |
||
203 | * @return Addendum |
||
204 | */ |
||
205 | public function init() |
||
206 | { |
||
207 | if (!$this->di->isStored($this)) |
||
208 | { |
||
209 | (new Signal())->emit(new NamespacesSignal($this)); |
||
210 | } |
||
211 | $this->di->store($this); |
||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Check if class could have annotations. |
||
217 | * **NOTE:** |
||
218 | * > This does not check check if class have annotations. It only checks if it implements `AnnotatedInterface` |
||
219 | * @param string|object $class |
||
220 | * @return bool |
||
221 | */ |
||
222 | 32 | public function hasAnnotations($class) |
|
226 | |||
227 | /** |
||
228 | * Use $class name or object to annotate class |
||
229 | * @param string|object $class |
||
230 | * @return ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|ReflectionAnnotatedClass |
||
231 | */ |
||
232 | 32 | public function annotate($class) |
|
242 | |||
243 | /** |
||
244 | * Set logger |
||
245 | * |
||
246 | * @param LoggerInterface $logger |
||
247 | * @return Addendum |
||
248 | */ |
||
249 | public function setLogger(LoggerInterface $logger) |
||
254 | |||
255 | /** |
||
256 | * Get logger |
||
257 | * |
||
258 | * @return LoggerInterface logger |
||
259 | */ |
||
260 | 46 | public function getLogger() |
|
268 | |||
269 | /** |
||
270 | * Add annotations namespace. |
||
271 | * Every added namespace will be included in annotation name resolving for current instance. |
||
272 | * |
||
273 | * @param string $ns |
||
274 | * @renturn Addendum |
||
275 | */ |
||
276 | 3 | public function addNamespace($ns) |
|
302 | |||
303 | /** |
||
304 | * Add many annotation namespaces. |
||
305 | * This is same as `addNamespace`, in that difference that many namespaces at once can be added. |
||
306 | * |
||
307 | * It accepts array of namespaces as param: |
||
308 | * ```php |
||
309 | * $nss = [ |
||
310 | * 'Maslosoft\Addendum\Annotations', |
||
311 | * 'Maslosoft\Mangan\Annotations' |
||
312 | * ]; |
||
313 | * ``` |
||
314 | * |
||
315 | * @param string[] $nss |
||
316 | * @return Addendum |
||
317 | */ |
||
318 | 32 | public function addNamespaces($nss) |
|
326 | |||
327 | /** |
||
328 | * Clear entire annotations cache. |
||
329 | */ |
||
330 | 3 | public static function cacheClear() |
|
338 | |||
339 | /** |
||
340 | * TODO This should not be static |
||
341 | * @param Reflector $reflection |
||
342 | * @return mixed[] |
||
343 | */ |
||
344 | 44 | public static function getDocComment(Reflector $reflection) |
|
361 | |||
362 | /** |
||
363 | * This method has no effect |
||
364 | * @deprecated Since 4.0.4 this has no effect |
||
365 | * @param bool $enabled |
||
366 | */ |
||
367 | public static function setRawMode($enabled = true) |
||
371 | |||
372 | /** |
||
373 | * Resolve annotation class name to prefixed annotation class name |
||
374 | * |
||
375 | * @param string|bool $class |
||
376 | * @return string |
||
377 | */ |
||
378 | 51 | public static function resolveClassName($class) |
|
416 | |||
417 | 50 | private static function getDeclaredAnnotations() |
|
432 | |||
433 | } |
||
434 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.