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.

79 lines
2.9 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\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. if (0 < \func_num_args()) {
  24. trigger_deprecation('symfony/http-kernel', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  25. }
  26. $this->controllerLocator = $controllerLocator;
  27. }
  28. public function process(ContainerBuilder $container)
  29. {
  30. $controllerLocator = $container->findDefinition($this->controllerLocator);
  31. $controllers = $controllerLocator->getArgument(0);
  32. foreach ($controllers as $controller => $argumentRef) {
  33. $argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]);
  34. if (!$argumentLocator->getArgument(0)) {
  35. // remove empty argument locators
  36. $reason = sprintf('Removing service-argument resolver for controller "%s": no corresponding services exist for the referenced types.', $controller);
  37. } else {
  38. // any methods listed for call-at-instantiation cannot be actions
  39. $reason = false;
  40. [$id, $action] = explode('::', $controller);
  41. if ($container->hasAlias($id)) {
  42. continue;
  43. }
  44. $controllerDef = $container->getDefinition($id);
  45. foreach ($controllerDef->getMethodCalls() as [$method]) {
  46. if (0 === strcasecmp($action, $method)) {
  47. $reason = sprintf('Removing method "%s" of service "%s" from controller candidates: the method is called at instantiation, thus cannot be an action.', $action, $id);
  48. break;
  49. }
  50. }
  51. if (!$reason) {
  52. // see Symfony\Component\HttpKernel\Controller\ContainerControllerResolver
  53. $controllers[$id.':'.$action] = $argumentRef;
  54. if ('__invoke' === $action) {
  55. $controllers[$id] = $argumentRef;
  56. }
  57. continue;
  58. }
  59. }
  60. unset($controllers[$controller]);
  61. $container->log($this, $reason);
  62. }
  63. $controllerLocator->replaceArgument(0, $controllers);
  64. }
  65. }