ParentTrait.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\ChildDefinition;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. trait ParentTrait
  14. {
  15. /**
  16. * Sets the Definition to inherit from.
  17. *
  18. * @return $this
  19. *
  20. * @throws InvalidArgumentException when parent cannot be set
  21. */
  22. final public function parent(string $parent): self
  23. {
  24. if (!$this->allowParent) {
  25. throw new InvalidArgumentException(sprintf('A parent cannot be defined when either "_instanceof" or "_defaults" are also defined for service prototype "%s".', $this->id));
  26. }
  27. if ($this->definition instanceof ChildDefinition) {
  28. $this->definition->setParent($parent);
  29. } else {
  30. // cast Definition to ChildDefinition
  31. $definition = serialize($this->definition);
  32. $definition = substr_replace($definition, '53', 2, 2);
  33. $definition = substr_replace($definition, 'Child', 44, 0);
  34. $definition = unserialize($definition);
  35. $this->definition = $definition->setParent($parent);
  36. }
  37. return $this;
  38. }
  39. }