| 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) |
|
| 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 | 27 | public static function fly($instanceId = self::DefaultInstanceId) |
|
| 154 | { |
||
| 155 | 27 | if (empty(self::$_a[$instanceId])) |
|
| 156 | 27 | { |
|
| 157 | 1 | self::$_a[$instanceId] = (new Addendum($instanceId))->init(); |
|
| 158 | 1 | } |
|
| 159 | 27 | return self::$_a[$instanceId]; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get current addendum version. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getVersion() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Initialize addendum and store configuration. |
||
| 178 | * This should be called upon first intstance creation. |
||
| 179 | * |
||
| 180 | * @return Addendum |
||
| 181 | */ |
||
| 182 | 1 | public function init() |
|
| 183 | { |
||
| 184 | 1 | if (!$this->di->isStored($this)) |
|
| 185 | 1 | { |
|
| 186 | (new Signal())->emit(new NamespacesSignal($this)); |
||
| 187 | } |
||
| 188 | 1 | $this->di->store($this); |
|
| 189 | 1 | 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 | 25 | 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) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Set logger |
||
| 222 | * |
||
| 223 | * @param LoggerInterface $logger |
||
| 224 | * @return Addendum |
||
| 225 | */ |
||
| 226 | public function setLogger(LoggerInterface $logger) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get logger |
||
| 234 | * |
||
| 235 | * @return LoggerInterface logger |
||
| 236 | */ |
||
| 237 | public function getLogger() |
||
| 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) |
|
| 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 | 25 | public function addNamespaces($nss) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Clear entire annotations cache. |
||
| 298 | */ |
||
| 299 | 3 | public static function cacheClear() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * TODO This should not be static |
||
| 311 | * @param \Reflector $reflection |
||
| 312 | * @return mixed[] |
||
| 313 | */ |
||
| 314 | 28 | public static function getDocComment(\Reflector $reflection) |
|
| 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 | 32 | public static function resolveClassName($class) |
|
| 386 | |||
| 387 | 15 | private static function _getDeclaredAnnotations() |
|
| 402 | |||
| 403 | } |
||
| 404 |