4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
14 use Symfony\Component\DependencyInjection\ChildDefinition;
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18 * @method $this parent(string $parent)
23 * Sets the Definition to inherit from.
25 * @param string $parent
29 * @throws InvalidArgumentException when parent cannot be set
31 final protected function setParent($parent)
33 if (!$this->allowParent) {
34 throw new InvalidArgumentException(sprintf('A parent cannot be defined when either "_instanceof" or "_defaults" are also defined for service prototype "%s".', $this->id));
37 if ($this->definition instanceof ChildDefinition) {
38 $this->definition->setParent($parent);
39 } elseif ($this->definition->isAutoconfigured()) {
40 throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try disabling autoconfiguration for the service.', $this->id));
41 } elseif ($this->definition->getBindings()) {
42 throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also "bind" arguments.', $this->id));
44 // cast Definition to ChildDefinition
45 $definition = serialize($this->definition);
46 $definition = substr_replace($definition, '53', 2, 2);
47 $definition = substr_replace($definition, 'Child', 44, 0);
48 $definition = unserialize($definition);
50 $this->definition = $definition->setParent($parent);