| 1 | <?php |
||
| 40 | class Addendum implements LoggerAwareInterface |
||
| 41 | { |
||
| 42 | |||
| 43 | const DefaultInstanceId = 'addendum'; |
||
|
1 ignored issue
–
show
|
|||
| 44 | |||
| 45 | /** |
||
| 46 | * Runtime path |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | public $runtimePath = 'runtime'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Namespaces to check for annotations. |
||
| 53 | * By default global and addendum namespace is included. |
||
| 54 | * @var string[] |
||
| 55 | */ |
||
| 56 | public $namespaces = [ |
||
| 57 | '\\', |
||
| 58 | TargetAnnotation::Ns |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Translatable annotations |
||
| 63 | * TODO This should be moved to `maslosoft/addendum-i18n-extractor` |
||
| 64 | * @var string[] |
||
| 65 | */ |
||
| 66 | public $i18nAnnotations = [ |
||
| 67 | 'Label', |
||
| 68 | 'Description' |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Plugins collection |
||
| 73 | * @var AddendumPlugins|mixed[] |
||
| 74 | */ |
||
| 75 | public $plugins = [ |
||
| 76 | 'matcher' => [ |
||
| 77 | ClassLiteralMatcher::class => [ |
||
| 78 | UseResolverDecorator::class |
||
| 79 | ] |
||
| 80 | ] |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Current instance id |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $instanceId = self::DefaultInstanceId; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * DI |
||
| 91 | * @var EmbeDi |
||
| 92 | */ |
||
| 93 | protected $di = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Logger |
||
| 97 | * @var LoggerInterface |
||
| 98 | */ |
||
| 99 | private $_logger; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Cache for resolved annotations class names. |
||
| 103 | * Key is shor annotation name. |
||
| 104 | * @var string[] |
||
| 105 | */ |
||
| 106 | private static $_classnames = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * This holds information about all declared classes implementing AnnotatedInterface. |
||
| 110 | * @see AnnotatedInterface |
||
| 111 | * @var string[] |
||
| 112 | */ |
||
| 113 | private static $_annotations = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Reflection annotated class cache |
||
| 117 | * @var ReflectionAnnotatedClass[] |
||
| 118 | */ |
||
| 119 | private static $_localCache = []; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Version holder |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private static $_version = null; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Addendum instances |
||
| 129 | * @var Addendum[] |
||
| 130 | */ |
||
| 131 | private static $_a = []; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * |
||
| 135 | * @param string $instanceId |
||
| 136 | */ |
||
| 137 | 39 | public function __construct($instanceId = self::DefaultInstanceId) |
|
| 138 | { |
||
| 139 | 39 | $this->instanceId = $instanceId; |
|
| 140 | 39 | $this->plugins = new AddendumPlugins($this->plugins); |
|
| 141 | |||
| 142 | 39 | $this->di = new EmbeDi($instanceId); |
|
| 143 | 39 | $this->di->configure($this); |
|
| 144 | 39 | } |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Get flyweight addendum instance of `Addendum`. |
||
| 148 | * Only one instance will be created for each `$instanceId` |
||
| 149 | * |
||
| 150 | * @param string $instanceId |
||
| 151 | * @return Addendum |
||
| 152 | */ |
||
| 153 | 28 | public static function fly($instanceId = self::DefaultInstanceId) |
|
| 154 | { |
||
| 155 | 28 | if (empty(self::$_a[$instanceId])) |
|
| 156 | 28 | { |
|
| 157 | self::$_a[$instanceId] = (new Addendum($instanceId))->init(); |
||
| 158 | } |
||
| 159 | 28 | return self::$_a[$instanceId]; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get current addendum version. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getVersion() |
||
| 168 | { |
||
| 169 | if (null === self::$_version) |
||
| 170 | { |
||
| 171 | self::$_version = require __DIR__ . '/version.php'; |
||
| 172 | } |
||
| 173 | return self::$_version; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Initialize addendum and store configuration. |
||
| 178 | * This should be called upon first intstance creation. |
||
| 179 | * |
||
| 180 | * @return Addendum |
||
| 181 | */ |
||
| 182 | public function init() |
||
| 183 | { |
||
| 184 | if (!$this->di->isStored($this)) |
||
| 185 | { |
||
| 186 | (new Signal())->emit(new NamespacesSignal($this)); |
||
| 187 | } |
||
| 188 | $this->di->store($this); |
||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Chech if class could have annotations. |
||
| 194 | * **NOTE:** |
||
| 195 | * > This does not check check if class have annotations. It only checks if it implements `AnnotatedInterface` |
||
| 196 | * @param string|object $class |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 26 | public function hasAnnotations($class) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Use $class name or object to annotate class |
||
| 206 | * @param string|object $class |
||
| 207 | * @return ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|ReflectionAnnotatedClass |
||
| 208 | */ |
||
| 209 | 26 | public function annotate($class) |
|
| 210 | { |
||
| 211 | 26 | $className = is_object($class) ? get_class($class) : $class; |
|
| 212 | 26 | if (!$this->hasAnnotations($class)) |
|
| 213 | 26 | { |
|
| 214 | throw new ReflectionException(sprintf('To annotate class "%s", it must implement interface %s', $className, AnnotatedInterface::class)); |
||
| 215 | } |
||
| 216 | 26 | $reflection = new ReflectionAnnotatedClass($class, $this); |
|
| 217 | 21 | return $reflection; |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set logger |
||
| 222 | * |
||
| 223 | * @param LoggerInterface $logger |
||
| 224 | * @return Addendum |
||
| 225 | */ |
||
| 226 | public function setLogger(LoggerInterface $logger) |
||
| 227 | { |
||
| 228 | $this->_logger = $logger; |
||
| 229 | return $this; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get logger |
||
| 234 | * |
||
| 235 | * @return LoggerInterface logger |
||
| 236 | */ |
||
| 237 | public function getLogger() |
||
| 238 | { |
||
| 239 | if (null === $this->_logger) |
||
| 240 | { |
||
| 241 | $this->_logger = new NullLogger; |
||
| 242 | } |
||
| 243 | return $this->_logger; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Add annotations namespace. |
||
| 248 | * Every added namespace will be included in annotation name resolving for current instance. |
||
| 249 | * |
||
| 250 | * @param string $ns |
||
| 251 | * @renturn Addendum |
||
| 252 | */ |
||
| 253 | 3 | public function addNamespace($ns) |
|
| 254 | { |
||
| 255 | 3 | NameNormalizer::normalize($ns); |
|
| 256 | 3 | if (!in_array($ns, $this->namespaces)) |
|
| 257 | 3 | { |
|
| 258 | 2 | $this->namespaces[] = $ns; |
|
| 259 | 2 | array_unique($this->namespaces); |
|
| 260 | |||
| 261 | 2 | $this->di->store($this, [], true); |
|
| 262 | |||
| 263 | // Reconfigure flyweight instances if present |
||
| 264 | 2 | if (!empty(self::$_a[$this->instanceId])) |
|
| 265 | 2 | { |
|
| 266 | 2 | self::$_a[$this->instanceId]->di->configure(self::$_a[$this->instanceId]); |
|
| 267 | 2 | } |
|
| 268 | 2 | } |
|
| 269 | 3 | return $this; |
|
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Add many annotaion namespaces. |
||
| 274 | * This is same as `addNamespace`, in that difference that many namespaces at once can be added. |
||
| 275 | * |
||
| 276 | * It accepts array of namespaces as param: |
||
| 277 | * ```php |
||
| 278 | * $nss = [ |
||
| 279 | * 'Maslosoft\Addendum\Annotations', |
||
| 280 | * 'Maslosoft\Mangan\Annotations' |
||
| 281 | * ]; |
||
| 282 | * ``` |
||
| 283 | * |
||
| 284 | * @param string[] $nss |
||
| 285 | * @return Addendum |
||
| 286 | */ |
||
| 287 | 26 | public function addNamespaces($nss) |
|
| 288 | { |
||
| 289 | 26 | foreach ($nss as $ns) |
|
| 290 | { |
||
| 291 | 3 | $this->addNamespace($ns); |
|
| 292 | 26 | } |
|
| 293 | 26 | return $this; |
|
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Clear entire annotations cache. |
||
| 298 | */ |
||
| 299 | 3 | public static function cacheClear() |
|
| 300 | { |
||
| 301 | 3 | self::$_annotations = []; |
|
| 302 | 3 | self::$_classnames = []; |
|
| 303 | 3 | self::$_localCache = []; |
|
| 304 | 3 | Blacklister::reset(); |
|
| 305 | 3 | Builder::clearCache(); |
|
| 306 | 3 | (new MetaCache())->clear(); |
|
| 307 | 3 | } |
|
| 308 | |||
| 309 | /** |
||
| 310 | * TODO This should not be static |
||
| 311 | * @param \Reflector $reflection |
||
| 312 | * @return mixed[] |
||
| 313 | */ |
||
| 314 | 33 | public static function getDocComment(\Reflector $reflection) |
|
| 315 | { |
||
| 316 | // NOTE: Due to a nature of traits, raw doc parsing is always needed |
||
| 317 | // When using reflection's method `getDocComment` it will return |
||
| 318 | // doc comment like if it was pasted in code. Thus use statement |
||
| 319 | // from traits would be omited. See https://github.com/Maslosoft/Addendum/issues/24 |
||
| 320 | 33 | $docComment = new DocComment(); |
|
| 321 | 33 | if ($reflection instanceof ReflectionClass) |
|
| 322 | 33 | { |
|
| 323 | 33 | $commentString = $docComment->get($reflection)['class']; |
|
| 324 | 33 | } |
|
| 325 | else |
||
| 326 | { |
||
| 327 | 18 | $commentString = $docComment->get($reflection); |
|
| 328 | } |
||
| 329 | 33 | return $commentString; |
|
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set raw parsing mode |
||
| 334 | * @deprecated Since 4.0.4 this has no effect |
||
| 335 | * @param bool $enabled |
||
| 336 | */ |
||
| 337 | public static function setRawMode($enabled = true) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Resolve annotation class name to prefixed annotation class name |
||
| 344 | * |
||
| 345 | * @param string|bool $class |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | 33 | public static function resolveClassName($class) |
|
| 349 | { |
||
| 350 | 33 | if (false === $class) |
|
| 351 | 33 | { |
|
| 352 | 33 | return null; |
|
| 386 | |||
| 387 | 32 | private static function _getDeclaredAnnotations() |
|
| 402 | |||
| 403 | } |
||
| 404 |