ContainerInterface.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Contract;
  12. use Psr\Container\ContainerExceptionInterface;
  13. use Psr\Container\ContainerInterface as PsrContainerInterface;
  14. use Psr\Container\NotFoundExceptionInterface;
  15. use Throwable;
  16. interface ContainerInterface extends PsrContainerInterface
  17. {
  18. /**
  19. * Build an entry of the container by its name.
  20. * This method behave like get() except resolves the entry again every time.
  21. * For example if the entry is a class then a new instance will be created each time.
  22. * This method makes the container behave like a factory.
  23. *
  24. * @template TClass
  25. *
  26. * @param class-string<TClass>|string $name entry name or a class name
  27. * @param array $parameters Optional parameters to use to build the entry. Use this to force specific parameters to specific values. Parameters not defined in this array will be resolved using the container.
  28. *
  29. * @return ($name is class-string<TClass> ? TClass : mixed)
  30. *
  31. * @throws ContainerExceptionInterface&Throwable the name parameter must be of type string
  32. * @throws NotFoundExceptionInterface&Throwable no entry found for the given name
  33. */
  34. public function make(string $name, array $parameters = []);
  35. /**
  36. * Bind an arbitrary resolved entry to an identifier.
  37. * Useful for testing 'get'.
  38. *
  39. * @param mixed $entry
  40. */
  41. public function set(string $name, $entry): void;
  42. /**
  43. * Unbind an arbitrary resolved entry.
  44. */
  45. public function unbind(string $name): void;
  46. /**
  47. * Bind an arbitrary definition to an identifier.
  48. * Useful for testing 'make'.
  49. *
  50. * @param array|callable|string $definition
  51. */
  52. public function define(string $name, $definition): void;
  53. }