ServiceManager.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\ServiceGovernance;
  12. class ServiceManager
  13. {
  14. protected array $services = [];
  15. /**
  16. * Register a service to the manager.
  17. */
  18. public function register(string $name, string $path, array $metadata): void
  19. {
  20. if (isset($metadata['protocol'])) {
  21. $this->services[$name][$path][$metadata['protocol']] = $metadata;
  22. } else {
  23. $this->services[$name][$path]['default'] = $metadata;
  24. }
  25. }
  26. /**
  27. * Deregister a service from the manager.
  28. */
  29. public function deregister(string $name, ?string $path = null): void
  30. {
  31. if ($path) {
  32. unset($this->services[$name][$path]);
  33. } else {
  34. unset($this->services[$name]);
  35. }
  36. }
  37. /**
  38. * List all services.
  39. */
  40. public function all(): array
  41. {
  42. return $this->services;
  43. }
  44. }