You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.2 KiB

3 years ago
  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\HttpKernel\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. /**
  18. * @author Alexander M. Turek <me@derrabus.de>
  19. */
  20. class ResettableServicePass implements CompilerPassInterface
  21. {
  22. private $tagName;
  23. public function __construct(string $tagName = 'kernel.reset')
  24. {
  25. if (0 < \func_num_args()) {
  26. trigger_deprecation('symfony/http-kernel', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  27. }
  28. $this->tagName = $tagName;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function process(ContainerBuilder $container)
  34. {
  35. if (!$container->has('services_resetter')) {
  36. return;
  37. }
  38. $services = $methods = [];
  39. foreach ($container->findTaggedServiceIds($this->tagName, true) as $id => $tags) {
  40. $services[$id] = new Reference($id, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE);
  41. foreach ($tags as $attributes) {
  42. if (!isset($attributes['method'])) {
  43. throw new RuntimeException(sprintf('Tag "%s" requires the "method" attribute to be set.', $this->tagName));
  44. }
  45. if (!isset($methods[$id])) {
  46. $methods[$id] = [];
  47. }
  48. $methods[$id][] = $attributes['method'];
  49. }
  50. }
  51. if (!$services) {
  52. $container->removeAlias('services_resetter');
  53. $container->removeDefinition('services_resetter');
  54. return;
  55. }
  56. $container->findDefinition('services_resetter')
  57. ->setArgument(0, new IteratorArgument($services))
  58. ->setArgument(1, $methods);
  59. }
  60. }