Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits; Chris@14: Chris@14: use Symfony\Component\DependencyInjection\ChildDefinition; Chris@14: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@14: Chris@14: /** Chris@14: * @method $this parent(string $parent) Chris@14: */ Chris@14: trait ParentTrait Chris@14: { Chris@14: /** Chris@14: * Sets the Definition to inherit from. Chris@14: * Chris@14: * @param string $parent Chris@14: * Chris@14: * @return $this Chris@14: * Chris@14: * @throws InvalidArgumentException when parent cannot be set Chris@14: */ Chris@14: final protected function setParent($parent) Chris@14: { Chris@14: if (!$this->allowParent) { Chris@14: throw new InvalidArgumentException(sprintf('A parent cannot be defined when either "_instanceof" or "_defaults" are also defined for service prototype "%s".', $this->id)); Chris@14: } Chris@14: Chris@14: if ($this->definition instanceof ChildDefinition) { Chris@14: $this->definition->setParent($parent); Chris@14: } elseif ($this->definition->isAutoconfigured()) { Chris@14: throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try disabling autoconfiguration for the service.', $this->id)); Chris@14: } elseif ($this->definition->getBindings()) { Chris@14: throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also "bind" arguments.', $this->id)); Chris@14: } else { Chris@14: // cast Definition to ChildDefinition Chris@14: $definition = serialize($this->definition); Chris@14: $definition = substr_replace($definition, '53', 2, 2); Chris@14: $definition = substr_replace($definition, 'Child', 44, 0); Chris@14: $definition = unserialize($definition); Chris@14: Chris@14: $this->definition = $definition->setParent($parent); Chris@14: } Chris@14: Chris@14: return $this; Chris@14: } Chris@14: }