BindTrait.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
  11. use Symfony\Component\DependencyInjection\Argument\BoundArgument;
  12. use Symfony\Component\DependencyInjection\Loader\Configurator\DefaultsConfigurator;
  13. use Symfony\Component\DependencyInjection\Loader\Configurator\InstanceofConfigurator;
  14. trait BindTrait
  15. {
  16. /**
  17. * Sets bindings.
  18. *
  19. * Bindings map $named or FQCN arguments to values that should be
  20. * injected in the matching parameters (of the constructor, of methods
  21. * called and of controller actions).
  22. *
  23. * @param string $nameOrFqcn A parameter name with its "$" prefix, or an FQCN
  24. * @param mixed $valueOrRef The value or reference to bind
  25. *
  26. * @return $this
  27. */
  28. final public function bind(string $nameOrFqcn, $valueOrRef): self
  29. {
  30. $valueOrRef = static::processValue($valueOrRef, true);
  31. $bindings = $this->definition->getBindings();
  32. $type = $this instanceof DefaultsConfigurator ? BoundArgument::DEFAULTS_BINDING : ($this instanceof InstanceofConfigurator ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING);
  33. $bindings[$nameOrFqcn] = new BoundArgument($valueOrRef, true, $type, $this->path ?? null);
  34. $this->definition->setBindings($bindings);
  35. return $this;
  36. }
  37. }