Geen omschrijving

RemoveEmptyControllerArgumentLocatorsPass.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * Removes empty service-locators registered for ServiceValueResolver.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. class RemoveEmptyControllerArgumentLocatorsPass implements CompilerPassInterface
  19. {
  20. private $controllerLocator;
  21. public function __construct(string $controllerLocator = 'argument_resolver.controller_locator')
  22. {
  23. $this->controllerLocator = $controllerLocator;
  24. }
  25. public function process(ContainerBuilder $container)
  26. {
  27. $controllerLocator = $container->findDefinition($this->controllerLocator);
  28. $controllers = $controllerLocator->getArgument(0);
  29. foreach ($controllers as $controller => $argumentRef) {
  30. $argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]);
  31. if (!$argumentLocator->getArgument(0)) {
  32. // remove empty argument locators
  33. $reason = sprintf('Removing service-argument resolver for controller "%s": no corresponding services exist for the referenced types.', $controller);
  34. } else {
  35. // any methods listed for call-at-instantiation cannot be actions
  36. $reason = false;
  37. [$id, $action] = explode('::', $controller);
  38. $controllerDef = $container->getDefinition($id);
  39. foreach ($controllerDef->getMethodCalls() as [$method]) {
  40. if (0 === strcasecmp($action, $method)) {
  41. $reason = sprintf('Removing method "%s" of service "%s" from controller candidates: the method is called at instantiation, thus cannot be an action.', $action, $id);
  42. break;
  43. }
  44. }
  45. if (!$reason) {
  46. // Deprecated since Symfony 4.1. See Symfony\Component\HttpKernel\Controller\ContainerControllerResolver
  47. $controllers[$id.':'.$action] = $argumentRef;
  48. if ('__invoke' === $action) {
  49. $controllers[$id] = $argumentRef;
  50. }
  51. continue;
  52. }
  53. }
  54. unset($controllers[$controller]);
  55. $container->log($this, $reason);
  56. }
  57. $controllerLocator->replaceArgument(0, $controllers);
  58. }
  59. }