Aspect.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Di\Annotation;
  12. use Attribute;
  13. use InvalidArgumentException;
  14. #[Attribute(Attribute::TARGET_CLASS)]
  15. class Aspect extends AbstractAnnotation
  16. {
  17. public function __construct(public array $classes = [], public array $annotations = [], public ?int $priority = null)
  18. {
  19. }
  20. public function collectClass(string $className): void
  21. {
  22. parent::collectClass($className);
  23. $this->collect($className);
  24. }
  25. protected function collect(string $className)
  26. {
  27. [$instanceClasses, $instanceAnnotations, $instancePriority] = AspectLoader::load($className);
  28. // Classes
  29. $classes = $this->classes;
  30. $classes = $instanceClasses ? array_merge($classes, $instanceClasses) : $classes;
  31. // Annotations
  32. $annotations = $this->annotations;
  33. $annotations = $instanceAnnotations ? array_merge($annotations, $instanceAnnotations) : $annotations;
  34. // Priority
  35. $annotationPriority = $this->priority;
  36. $propertyPriority = $instancePriority ?: null;
  37. if (! is_null($annotationPriority) && ! is_null($propertyPriority) && $annotationPriority !== $propertyPriority) {
  38. throw new InvalidArgumentException('Cannot define two difference priority of Aspect.');
  39. }
  40. $priority = $annotationPriority ?? $propertyPriority;
  41. // Save the metadata to AspectCollector
  42. AspectCollector::setAround($className, $classes, $annotations, $priority);
  43. }
  44. }