ControllerNameParser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. /**
  13. * ControllerNameParser converts controller from the short notation a:b:c
  14. * (BlogBundle:Post:index) to a fully-qualified class::method string
  15. * (Bundle\BlogBundle\Controller\PostController::indexAction).
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class ControllerNameParser
  20. {
  21. protected $kernel;
  22. public function __construct(KernelInterface $kernel)
  23. {
  24. $this->kernel = $kernel;
  25. }
  26. /**
  27. * Converts a short notation a:b:c to a class::method.
  28. *
  29. * @param string $controller A short notation controller (a:b:c)
  30. *
  31. * @return string A string in the class::method notation
  32. *
  33. * @throws \InvalidArgumentException when the specified bundle is not enabled
  34. * or the controller cannot be found
  35. */
  36. public function parse($controller)
  37. {
  38. $parts = explode(':', $controller);
  39. if (3 !== \count($parts) || \in_array('', $parts, true)) {
  40. throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "a:b:c" controller string.', $controller));
  41. }
  42. $originalController = $controller;
  43. list($bundle, $controller, $action) = $parts;
  44. $controller = str_replace('/', '\\', $controller);
  45. $bundles = array();
  46. try {
  47. // this throws an exception if there is no such bundle
  48. $allBundles = $this->kernel->getBundle($bundle, false);
  49. } catch (\InvalidArgumentException $e) {
  50. $message = sprintf(
  51. 'The "%s" (from the _controller value "%s") does not exist or is not enabled in your kernel!',
  52. $bundle,
  53. $originalController
  54. );
  55. if ($alternative = $this->findAlternative($bundle)) {
  56. $message .= sprintf(' Did you mean "%s:%s:%s"?', $alternative, $controller, $action);
  57. }
  58. throw new \InvalidArgumentException($message, 0, $e);
  59. }
  60. foreach ($allBundles as $b) {
  61. $try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
  62. if (class_exists($try)) {
  63. return $try.'::'.$action.'Action';
  64. }
  65. $bundles[] = $b->getName();
  66. $msg = sprintf('The _controller value "%s:%s:%s" maps to a "%s" class, but this class was not found. Create this class or check the spelling of the class and its namespace.', $bundle, $controller, $action, $try);
  67. }
  68. if (\count($bundles) > 1) {
  69. $msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $bundles));
  70. }
  71. throw new \InvalidArgumentException($msg);
  72. }
  73. /**
  74. * Converts a class::method notation to a short one (a:b:c).
  75. *
  76. * @param string $controller A string in the class::method notation
  77. *
  78. * @return string A short notation controller (a:b:c)
  79. *
  80. * @throws \InvalidArgumentException when the controller is not valid or cannot be found in any bundle
  81. */
  82. public function build($controller)
  83. {
  84. if (0 === preg_match('#^(.*?\\\\Controller\\\\(.+)Controller)::(.+)Action$#', $controller, $match)) {
  85. throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "class::method" string.', $controller));
  86. }
  87. $className = $match[1];
  88. $controllerName = $match[2];
  89. $actionName = $match[3];
  90. foreach ($this->kernel->getBundles() as $name => $bundle) {
  91. if (0 !== strpos($className, $bundle->getNamespace())) {
  92. continue;
  93. }
  94. return sprintf('%s:%s:%s', $name, $controllerName, $actionName);
  95. }
  96. throw new \InvalidArgumentException(sprintf('Unable to find a bundle that defines controller "%s".', $controller));
  97. }
  98. /**
  99. * Attempts to find a bundle that is *similar* to the given bundle name.
  100. *
  101. * @param string $nonExistentBundleName
  102. *
  103. * @return string
  104. */
  105. private function findAlternative($nonExistentBundleName)
  106. {
  107. $bundleNames = array_map(function ($b) {
  108. return $b->getName();
  109. }, $this->kernel->getBundles());
  110. $alternative = null;
  111. $shortest = null;
  112. foreach ($bundleNames as $bundleName) {
  113. // if there's a partial match, return it immediately
  114. if (false !== strpos($bundleName, $nonExistentBundleName)) {
  115. return $bundleName;
  116. }
  117. $lev = levenshtein($nonExistentBundleName, $bundleName);
  118. if ($lev <= \strlen($nonExistentBundleName) / 3 && (null === $alternative || $lev < $shortest)) {
  119. $alternative = $bundleName;
  120. $shortest = $lev;
  121. }
  122. }
  123. return $alternative;
  124. }
  125. }