AutoconfigureTrait.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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 AutoconfigureTrait
  14. {
  15. /**
  16. * Sets whether or not instanceof conditionals should be prepended with a global set.
  17. *
  18. * @return $this
  19. *
  20. * @throws InvalidArgumentException when a parent is already set
  21. */
  22. final public function autoconfigure(bool $autoconfigured = true): self
  23. {
  24. if ($autoconfigured && $this->definition instanceof ChildDefinition) {
  25. throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try disabling autoconfiguration for the service.', $this->id));
  26. }
  27. $this->definition->setAutoconfigured($autoconfigured);
  28. return $this;
  29. }
  30. }