Reference.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. use Psr\Container\ContainerInterface;
  13. class Reference implements DefinitionInterface, SelfResolvingDefinitionInterface
  14. {
  15. /**
  16. * Entry name.
  17. */
  18. private string $name = '';
  19. private bool $needProxy = false;
  20. /**
  21. * @param string $targetEntryName name of the target entry
  22. */
  23. public function __construct(private string $targetEntryName)
  24. {
  25. }
  26. /**
  27. * Definitions can be cast to string for debugging information.
  28. */
  29. public function __toString(): string
  30. {
  31. return sprintf('get(%s)', $this->targetEntryName);
  32. }
  33. /**
  34. * Returns the name of the entry in the container.
  35. */
  36. public function getName(): string
  37. {
  38. return $this->name;
  39. }
  40. /**
  41. * Set the name of the entry in the container.
  42. */
  43. public function setName(string $name)
  44. {
  45. $this->name = $name;
  46. }
  47. public function getTargetEntryName(): string
  48. {
  49. return $this->targetEntryName;
  50. }
  51. public function resolve(ContainerInterface $container)
  52. {
  53. return $container->get($this->getTargetEntryName());
  54. }
  55. public function isResolvable(ContainerInterface $container): bool
  56. {
  57. return $container->has($this->getTargetEntryName());
  58. }
  59. /**
  60. * Determine if the definition need to transfer to a proxy class.
  61. */
  62. public function isNeedProxy(): bool
  63. {
  64. return $this->needProxy;
  65. }
  66. public function setNeedProxy($needProxy): self
  67. {
  68. $this->needProxy = $needProxy;
  69. return $this;
  70. }
  71. }