MethodInjection.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 MethodInjection implements DefinitionInterface
  13. {
  14. public function __construct(private string $methodName, private array $parameters = [])
  15. {
  16. }
  17. public function __toString(): string
  18. {
  19. return sprintf('method(%s)', $this->methodName);
  20. }
  21. public function getName(): string
  22. {
  23. return '';
  24. }
  25. public function setName(string $name)
  26. {
  27. // The name does not matter for method injections, so do nothing.
  28. }
  29. public function getParameters(): array
  30. {
  31. return $this->parameters;
  32. }
  33. public function merge(self $definition)
  34. {
  35. // In case of conflicts, the current definition prevails.
  36. $this->parameters = $this->parameters + $definition->parameters;
  37. }
  38. /**
  39. * Reset the target should be resolved.
  40. * If it is the FactoryDefinition, then the target means $factory property,
  41. * If it is the ObjectDefinition, then the target means $className property.
  42. */
  43. public function setTarget(string $value)
  44. {
  45. $this->methodName = $value;
  46. }
  47. /**
  48. * Determine if the definition need to transfer to a proxy class.
  49. */
  50. public function isNeedProxy(): bool
  51. {
  52. // Method injection does not has proxy.
  53. return false;
  54. }
  55. }