FactoryTrait.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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\Exception\InvalidArgumentException;
  12. use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
  13. trait FactoryTrait
  14. {
  15. /**
  16. * Sets a factory.
  17. *
  18. * @param string|array|ReferenceConfigurator $factory A PHP callable reference
  19. *
  20. * @return $this
  21. */
  22. final public function factory($factory): self
  23. {
  24. if (\is_string($factory) && 1 === substr_count($factory, ':')) {
  25. $factoryParts = explode(':', $factory);
  26. throw new InvalidArgumentException(sprintf('Invalid factory "%s": the "service:method" notation is not available when using PHP-based DI configuration. Use "[service(\'%s\'), \'%s\']" instead.', $factory, $factoryParts[0], $factoryParts[1]));
  27. }
  28. $this->definition->setFactory(static::processValue($factory, true));
  29. return $this;
  30. }
  31. }