vendor/sensio/framework-extra-bundle/src/Routing/AnnotatedRouteControllerLoader.php line 19

Open in your IDE?
  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 Sensio\Bundle\FrameworkExtraBundle\Routing;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route as FrameworkExtraBundleRoute;
  13. use Symfony\Component\Routing\Loader\AnnotationClassLoader;
  14. use Symfony\Component\Routing\Route;
  15. @trigger_error(sprintf('The "%s" class is deprecated since version 5.2. Use "%s" instead.', AnnotatedRouteControllerLoader::class, \Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader::class), \E_USER_DEPRECATED);
  16. /**
  17. * AnnotatedRouteControllerLoader is an implementation of AnnotationClassLoader
  18. * that sets the '_controller' default based on the class and method names.
  19. *
  20. * It also parse the @Method annotation.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @deprecated since version 5.2
  25. */
  26. class AnnotatedRouteControllerLoader extends AnnotationClassLoader
  27. {
  28. /**
  29. * Configures the _controller default parameter and eventually the HTTP method
  30. * requirement of a given Route instance.
  31. *
  32. * @param mixed $annot The annotation class instance
  33. *
  34. * @throws \LogicException When the service option is specified on a method
  35. */
  36. protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
  37. {
  38. // controller
  39. $classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
  40. if ($classAnnot instanceof FrameworkExtraBundleRoute && $service = $classAnnot->getService()) {
  41. $route->setDefault('_controller', $service.':'.$method->getName());
  42. } elseif ('__invoke' === $method->getName()) {
  43. $route->setDefault('_controller', $class->getName());
  44. } else {
  45. $route->setDefault('_controller', $class->getName().'::'.$method->getName());
  46. }
  47. // requirements (@Method)
  48. foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
  49. if ($configuration instanceof Method) {
  50. $route->setMethods($configuration->getMethods());
  51. } elseif ($configuration instanceof FrameworkExtraBundleRoute && $configuration->getService()) {
  52. throw new \LogicException('The service option can only be specified at class level.');
  53. }
  54. }
  55. }
  56. protected function getGlobals(\ReflectionClass $class)
  57. {
  58. $globals = parent::getGlobals($class);
  59. foreach ($this->reader->getClassAnnotations($class) as $configuration) {
  60. if ($configuration instanceof Method) {
  61. $globals['methods'] = array_merge($globals['methods'], $configuration->getMethods());
  62. }
  63. }
  64. return $globals;
  65. }
  66. /**
  67. * Makes the default route name more sane by removing common keywords.
  68. *
  69. * @return string The default route name
  70. */
  71. protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
  72. {
  73. $routeName = parent::getDefaultRouteName($class, $method);
  74. return preg_replace(
  75. ['/(bundle|controller)_/', '/action(_\d+)?$/', '/__/'],
  76. ['_', '\\1', '_'],
  77. $routeName
  78. );
  79. }
  80. }