FactoryTrait.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. trait FactoryTrait
  13. {
  14. /**
  15. * Sets a factory.
  16. *
  17. * @param string|array $factory A PHP callable reference
  18. *
  19. * @return $this
  20. */
  21. final public function factory($factory): self
  22. {
  23. if (\is_string($factory) && 1 === substr_count($factory, ':')) {
  24. $factoryParts = explode(':', $factory);
  25. 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]));
  26. }
  27. $this->definition->setFactory(static::processValue($factory, true));
  28. return $this;
  29. }
  30. }