|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Biurad opensource projects. |
| 7 | + * |
| 8 | + * PHP version 7.2 and above required |
| 9 | + * |
| 10 | + * @author Divine Niiquaye Ibok <divineibok@gmail.com> |
| 11 | + * @copyright 2019 Biurad Group (https://biurad.com/) |
| 12 | + * @license https://opensource.org/licenses/BSD-3-Clause License |
| 13 | + * |
| 14 | + * For the full copyright and license information, please view the LICENSE |
| 15 | + * file that was distributed with this source code. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Biurad\Annotations; |
| 19 | + |
| 20 | +use Doctrine\Common\Annotations\Reader as AnnotationReader; |
| 21 | +use Doctrine\Common\Annotations\SimpleAnnotationReader; |
| 22 | +use FilesystemIterator; |
| 23 | +use RecursiveDirectoryIterator; |
| 24 | +use RecursiveIteratorIterator; |
| 25 | +use ReflectionClass; |
| 26 | +use ReflectionMethod; |
| 27 | +use ReflectionProperty; |
| 28 | +use Reflector; |
| 29 | +use RegexIterator; |
| 30 | + |
| 31 | +class AnnotationLoader implements LoaderInterface |
| 32 | +{ |
| 33 | + /** @var null|AnnotationReader */ |
| 34 | + private $annotation; |
| 35 | + |
| 36 | + /** @var ListenerInterface[] */ |
| 37 | + private $listeners; |
| 38 | + |
| 39 | + /** @var string[] */ |
| 40 | + private $resources = []; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param null|AnnotationReader $reader |
| 44 | + */ |
| 45 | + public function __construct(?AnnotationReader $reader = null) |
| 46 | + { |
| 47 | + $this->annotation = $reader; |
| 48 | + |
| 49 | + if (null === $reader && \interface_exists(AnnotationReader::class)) { |
| 50 | + $this->annotation = new SimpleAnnotationReader(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + public function attachListener(ListenerInterface ...$listeners): void |
| 58 | + { |
| 59 | + foreach ($listeners as $listener) { |
| 60 | + if ($this->annotation instanceof SimpleAnnotationReader) { |
| 61 | + $this->annotation->addNamespace($listener->getAnnotation()); |
| 62 | + } |
| 63 | + |
| 64 | + $this->listeners[] = $listener; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + public function attach(string ...$resources): void |
| 72 | + { |
| 73 | + foreach ($resources as $resource) { |
| 74 | + $this->resources[] = $resource; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritdoc} |
| 80 | + */ |
| 81 | + public function load(): iterable |
| 82 | + { |
| 83 | + $annotations = []; |
| 84 | + |
| 85 | + foreach ($this->resources as $resource) { |
| 86 | + foreach ($this->listeners as $listener) { |
| 87 | + if (\class_exists($resource) || \is_dir($resource)) { |
| 88 | + $annotations += $this->findAnnotations($resource, $listener); |
| 89 | + |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (!\file_exists($resource) || \is_dir($resource)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + (function () use ($resource): void { |
| 98 | + require $resource; |
| 99 | + })->call($listener->getBinding()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + foreach ($this->listeners as $listener) { |
| 104 | + if (null !== $found = $listener->onAnnotation($annotations)) { |
| 105 | + yield $found; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Finds annotations in the given resource |
| 112 | + * |
| 113 | + * @param string $resource |
| 114 | + * |
| 115 | + * @return array<string,array<string,mixed>> |
| 116 | + */ |
| 117 | + private function findAnnotations(string $resource, ListenerInterface $listener): array |
| 118 | + { |
| 119 | + $classes = $annotations = []; |
| 120 | + |
| 121 | + if (\is_dir($resource)) { |
| 122 | + $classes = \array_merge($this->findClasses($resource), $classes); |
| 123 | + } elseif (\class_exists($resource)) { |
| 124 | + $classes[] = $resource; |
| 125 | + } |
| 126 | + |
| 127 | + foreach ($classes as $class) { |
| 128 | + $classReflection = new ReflectionClass($class); |
| 129 | + $className = $classReflection->getName(); |
| 130 | + |
| 131 | + if ($classReflection->isAbstract()) { |
| 132 | + throw new InvalidAnnotationException(\sprintf( |
| 133 | + 'Annotations from class "%s" cannot be read as it is abstract.', |
| 134 | + $classReflection->getName() |
| 135 | + )); |
| 136 | + } |
| 137 | + |
| 138 | + foreach ($this->getAnnotations($classReflection, $listener) as $annotation) { |
| 139 | + $annotations[$className]['class'] = $annotation; |
| 140 | + } |
| 141 | + |
| 142 | + $reflections = \array_merge($classReflection->getMethods(), $classReflection->getProperties()); |
| 143 | + |
| 144 | + foreach ($reflections as $reflection) { |
| 145 | + if ($reflection instanceof ReflectionMethod && $reflection->isAbstract()) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + foreach ($this->getAnnotations($reflection, $listener) as $annotation) { |
| 150 | + if ($reflection instanceof ReflectionMethod) { |
| 151 | + $annotations[$className]['method'][] = [$reflection, $annotation]; |
| 152 | + |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + $annotations[$className]['property'][] = [$reflection, $annotation]; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + \gc_mem_caches(); |
| 162 | + |
| 163 | + return $annotations; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @param ReflectionClass|ReflectionMethod|ReflectionProperty $reflection |
| 168 | + * |
| 169 | + * @return iterable<object> |
| 170 | + */ |
| 171 | + private function getAnnotations(Reflector $reflection, ListenerInterface $listener): iterable |
| 172 | + { |
| 173 | + $annotationClass = $listener->getAnnotation(); |
| 174 | + $annotations = []; |
| 175 | + |
| 176 | + if (\PHP_VERSION_ID >= 80000) { |
| 177 | + foreach ($reflection->getAttributes($annotationClass) as $attribute) { |
| 178 | + yield $attribute->newInstance(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + if (null === $this->annotation) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + switch (true) { |
| 187 | + case $reflection instanceof ReflectionClass: |
| 188 | + $annotations = $this->annotation->getClassAnnotations($reflection); |
| 189 | + |
| 190 | + break; |
| 191 | + |
| 192 | + case $reflection instanceof ReflectionMethod: |
| 193 | + $annotations = $this->annotation->getMethodAnnotations($reflection); |
| 194 | + |
| 195 | + break; |
| 196 | + |
| 197 | + case $reflection instanceof ReflectionProperty: |
| 198 | + $annotations = $this->annotation->getPropertyAnnotations($reflection); |
| 199 | + |
| 200 | + break; |
| 201 | + } |
| 202 | + |
| 203 | + foreach ($annotations as $annotation) { |
| 204 | + if ($annotation instanceof $annotationClass) { |
| 205 | + yield $annotation; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Finds classes in the given resource directory |
| 212 | + * |
| 213 | + * @param string $resource |
| 214 | + * |
| 215 | + * @return string[] |
| 216 | + */ |
| 217 | + private function findClasses(string $resource): array |
| 218 | + { |
| 219 | + $files = $this->findFiles($resource); |
| 220 | + $declared = \get_declared_classes(); |
| 221 | + |
| 222 | + foreach ($files as $file) { |
| 223 | + require_once $file; |
| 224 | + } |
| 225 | + |
| 226 | + return \array_diff(\get_declared_classes(), $declared); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Finds files in the given resource |
| 231 | + * |
| 232 | + * @param string $resource |
| 233 | + * |
| 234 | + * @return string[] |
| 235 | + */ |
| 236 | + private function findFiles(string $resource): array |
| 237 | + { |
| 238 | + $flags = FilesystemIterator::CURRENT_AS_PATHNAME; |
| 239 | + |
| 240 | + $directory = new RecursiveDirectoryIterator($resource, $flags); |
| 241 | + $iterator = new RecursiveIteratorIterator($directory); |
| 242 | + $files = new RegexIterator($iterator, '/\.php$/'); |
| 243 | + |
| 244 | + return \iterator_to_array($files); |
| 245 | + } |
| 246 | +} |
0 commit comments