AnnotatedRouteControllerLoader.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Sensio\Bundle\FrameworkExtraBundle\Routing;
  3. use Symfony\Component\Routing\Loader\AnnotationClassLoader;
  4. use Symfony\Component\Routing\Route;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\AnnotationReader as ConfigurationAnnotationReader;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien@symfony.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * AnnotatedRouteControllerLoader is an implementation of AnnotationClassLoader
  17. * that sets the '_controller' default based on the class and method names.
  18. *
  19. * It also parse the @Method annotation.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class AnnotatedRouteControllerLoader extends AnnotationClassLoader
  24. {
  25. /**
  26. * Configures the _controller default parameter and eventually the _method
  27. * requirement of a given Route instance.
  28. *
  29. * @param Route $route A Route instance
  30. * @param ReflectionClass $class A ReflectionClass instance
  31. * @param ReflectionMethod $method A ReflectionClass method
  32. */
  33. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  34. {
  35. // controller
  36. $classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
  37. if ($classAnnot && $service = $classAnnot->getService()) {
  38. $route->setDefault('_controller', $service.':'.$method->getName());
  39. } else {
  40. $route->setDefault('_controller', $class->getName().'::'.$method->getName());
  41. }
  42. // requirements (@Method)
  43. foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
  44. if ($configuration instanceof Method) {
  45. $route->setRequirement('_method', implode('|', $configuration->getMethods()));
  46. }
  47. }
  48. }
  49. /**
  50. * Makes the default route name more sane by removing common keywords.
  51. *
  52. * @param ReflectionClass $class A ReflectionClass instance
  53. * @param ReflectionMethod $method A ReflectionMethod instance
  54. * @return string
  55. */
  56. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  57. {
  58. $routeName = parent::getDefaultRouteName($class, $method);
  59. return preg_replace(array(
  60. '/(bundle|controller)_/',
  61. '/action(_\d+)?$/',
  62. '/__/'
  63. ), array(
  64. '_',
  65. '\\1',
  66. '_'
  67. ), $routeName);
  68. }
  69. }