Container.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Pimple;
  12. use Hyperf\Contract\ContainerInterface;
  13. use Hyperf\Pimple\Exception\InvalidDefinitionException;
  14. use Hyperf\Pimple\Exception\NotFoundException;
  15. use Hyperf\Pimple\Exception\NotSupportException;
  16. use Pimple;
  17. use Psr\Container\ContainerInterface as PsrContainerInterface;
  18. use ReflectionClass;
  19. use ReflectionException;
  20. use ReflectionMethod;
  21. use ReflectionParameter;
  22. class Container implements ContainerInterface
  23. {
  24. /**
  25. * @var Pimple\Container
  26. */
  27. protected $pimple;
  28. /**
  29. * @var ReflectionClass[]
  30. */
  31. protected $reflection = [];
  32. public function __construct(Pimple\Container $pimple)
  33. {
  34. $this->pimple = $pimple;
  35. $this->pimple[ContainerInterface::class] = $this;
  36. $this->pimple[PsrContainerInterface::class] = $this;
  37. }
  38. public function get($id)
  39. {
  40. if ($this->has($id)) {
  41. return $this->pimple[$id];
  42. }
  43. return $this->pimple[$id] = $this->make($id);
  44. }
  45. public function has($id): bool
  46. {
  47. return isset($this->pimple[$id]);
  48. }
  49. public function make(string $name, array $parameters = [])
  50. {
  51. if (! class_exists($name)) {
  52. throw new NotFoundException("Entry {$name} is not found.");
  53. }
  54. $ref = $this->reflection[$name] ?? new ReflectionClass($name);
  55. $constructor = $ref->getConstructor();
  56. $args = [];
  57. if ($constructor && $constructor->isPublic()) {
  58. $args = $this->resolveParameters($constructor, $parameters);
  59. }
  60. $instance = new $name(...$args);
  61. $this->reflection[$name] = $ref;
  62. return $instance;
  63. }
  64. public function set(string $name, $entry): void
  65. {
  66. $this->pimple[$name] = $entry;
  67. }
  68. public function define(string $name, $definition): void
  69. {
  70. throw new NotSupportException('Method define is not support.');
  71. }
  72. public function unbind(string $name): void
  73. {
  74. $this->pimple[$name] = null;
  75. }
  76. protected function resolveParameters(ReflectionMethod $method, $parameters = [])
  77. {
  78. $args = [];
  79. foreach ($method->getParameters() as $index => $parameter) {
  80. if (array_key_exists($parameter->getName(), $parameters)) {
  81. $value = $parameters[$parameter->getName()];
  82. } elseif (array_key_exists($index, $parameters)) {
  83. $value = $parameters[$index];
  84. } elseif ($parameter->getType() && $this->has($parameter->getType()->getName())) {
  85. $value = $this->get($parameter->getType()->getName());
  86. } else {
  87. if ($parameter->isDefaultValueAvailable() || $parameter->isOptional()) {
  88. $value = $this->getParameterDefaultValue($parameter, $method);
  89. } else {
  90. throw new InvalidDefinitionException(sprintf(
  91. 'Parameter $%s of %s has no value defined or guessable',
  92. $parameter->getName(),
  93. $this->getFunctionName($method)
  94. ));
  95. }
  96. }
  97. $args[] = $value;
  98. }
  99. return $args;
  100. }
  101. protected function getParameterDefaultValue(ReflectionParameter $parameter, ReflectionMethod $function)
  102. {
  103. try {
  104. return $parameter->getDefaultValue();
  105. } catch (ReflectionException $e) {
  106. throw new InvalidDefinitionException(sprintf(
  107. 'The parameter "%s" of %s has no type defined or guessable. It has a default value, '
  108. . 'but the default value can\'t be read through Reflection because it is a PHP internal class.',
  109. $parameter->getName(),
  110. $this->getFunctionName($function)
  111. ));
  112. }
  113. }
  114. private function getFunctionName(ReflectionMethod $method): string
  115. {
  116. return $method->getName() . '()';
  117. }
  118. }