ControllerNameParser.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  13. /**
  14. * ControllerNameParser converts controller from the short notation a:b:c
  15. * (BlogBundle:Post:index) to a fully-qualified class::method string
  16. * (Bundle\BlogBundle\Controller\PostController::indexAction).
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ControllerNameParser
  21. {
  22. protected $kernel;
  23. protected $logger;
  24. /**
  25. * Constructor.
  26. *
  27. * @param KernelInterface $kernel A KernelInterface instance
  28. * @param LoggerInterface $logger A LoggerInterface instance
  29. */
  30. public function __construct(KernelInterface $kernel, LoggerInterface $logger = null)
  31. {
  32. $this->kernel = $kernel;
  33. $this->logger = $logger;
  34. }
  35. /**
  36. * Converts a short notation a:b:c to a class::method.
  37. *
  38. * @param string $controller A short notation controller (a:b:c)
  39. *
  40. * @param string A controller (class::method)
  41. */
  42. public function parse($controller)
  43. {
  44. if (3 != count($parts = explode(':', $controller))) {
  45. throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller));
  46. }
  47. list($bundle, $controller, $action) = $parts;
  48. $class = null;
  49. $logs = array();
  50. foreach ($this->kernel->getBundle($bundle, false) as $b) {
  51. $try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
  52. if (!class_exists($try)) {
  53. if (null !== $this->logger) {
  54. $logs[] = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try);
  55. }
  56. } else {
  57. $class = $try;
  58. break;
  59. }
  60. }
  61. if (null === $class) {
  62. $this->handleControllerNotFoundException($bundle, $controller, $logs);
  63. }
  64. return $class.'::'.$action.'Action';
  65. }
  66. private function handleControllerNotFoundException($bundle, $controller, array $logs)
  67. {
  68. // just one log, return it as the exception
  69. if (1 == count($logs)) {
  70. throw new \InvalidArgumentException($logs[0]);
  71. }
  72. // many logs, use a message that mentions each searched bundle
  73. $names = array();
  74. foreach ($this->kernel->getBundle($bundle, false) as $b) {
  75. $names[] = $b->getName();
  76. }
  77. $msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $names));
  78. throw new \InvalidArgumentException($msg);
  79. }
  80. }