AutoAliasServicePass.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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\Compiler;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  14. /**
  15. * Sets a service to be an alias of another one, given a format pattern.
  16. */
  17. class AutoAliasServicePass implements CompilerPassInterface
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function process(ContainerBuilder $container)
  23. {
  24. foreach ($container->findTaggedServiceIds('auto_alias') as $serviceId => $tags) {
  25. foreach ($tags as $tag) {
  26. if (!isset($tag['format'])) {
  27. throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId));
  28. }
  29. $aliasId = $container->getParameterBag()->resolveValue($tag['format']);
  30. if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) {
  31. $container->setAlias($serviceId, new Alias($aliasId, true));
  32. }
  33. }
  34. }
  35. }
  36. }