AnnotatedRouteControllerLoader.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Routing;
  11. use Symfony\Component\Routing\Loader\AnnotationClassLoader;
  12. use Symfony\Component\Routing\Route;
  13. /**
  14. * AnnotatedRouteControllerLoader is an implementation of AnnotationClassLoader
  15. * that sets the '_controller' default based on the class and method names.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class AnnotatedRouteControllerLoader extends AnnotationClassLoader
  20. {
  21. /**
  22. * Configures the _controller default parameter of a given Route instance.
  23. *
  24. * @param mixed $annot The annotation class instance
  25. */
  26. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  27. {
  28. if ('__invoke' === $method->getName()) {
  29. $route->setDefault('_controller', $class->getName());
  30. } else {
  31. $route->setDefault('_controller', $class->getName().'::'.$method->getName());
  32. }
  33. }
  34. /**
  35. * Makes the default route name more sane by removing common keywords.
  36. *
  37. * @return string
  38. */
  39. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  40. {
  41. return preg_replace([
  42. '/(bundle|controller)_/',
  43. '/action(_\d+)?$/',
  44. '/__/',
  45. ], [
  46. '_',
  47. '\\1',
  48. '_',
  49. ], parent::getDefaultRouteName($class, $method));
  50. }
  51. }