PriorityDefinition.php 1.1 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\Definition;
  12. class PriorityDefinition
  13. {
  14. /**
  15. * @var array<string, int>
  16. */
  17. protected array $dependencies = [];
  18. public function __construct(string $class, int $priority = 0)
  19. {
  20. $this->dependencies[$class] = $priority;
  21. }
  22. public function getDependencies(): array
  23. {
  24. return $this->dependencies;
  25. }
  26. public function merge(PriorityDefinition $factory): static
  27. {
  28. foreach ($factory->getDependencies() as $class => $priority) {
  29. if (isset($this->dependencies[$class]) && $priority <= $this->dependencies[$class]) {
  30. continue;
  31. }
  32. $this->dependencies[$class] = $priority;
  33. }
  34. return $this;
  35. }
  36. public function getDefinition(): string
  37. {
  38. $dependencies = array_flip($this->dependencies);
  39. ksort($dependencies);
  40. return array_pop($dependencies);
  41. }
  42. }