ProviderBasedGenerator.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony CMF package.
  4. *
  5. * (c) 2011-2015 Symfony CMF
  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\Cmf\Component\Routing;
  11. use Symfony\Component\Routing\Generator\UrlGenerator;
  12. use Symfony\Component\Routing\Route as SymfonyRoute;
  13. use Symfony\Component\Routing\RequestContext;
  14. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * A Generator that uses a RouteProvider rather than a RouteCollection.
  18. *
  19. * @author Larry Garfield
  20. */
  21. class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorInterface
  22. {
  23. /**
  24. * The route provider for this generator.
  25. *
  26. * @var RouteProviderInterface
  27. */
  28. protected $provider;
  29. /**
  30. * @param RouteProviderInterface $provider
  31. * @param LoggerInterface $logger
  32. */
  33. public function __construct(RouteProviderInterface $provider, LoggerInterface $logger = null)
  34. {
  35. $this->provider = $provider;
  36. $this->logger = $logger;
  37. $this->context = new RequestContext();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
  43. {
  44. if ($name instanceof SymfonyRoute) {
  45. $route = $name;
  46. } elseif (null === $route = $this->provider->getRouteByName($name)) {
  47. throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
  48. }
  49. // the Route has a cache of its own and is not recompiled as long as it does not get modified
  50. $compiledRoute = $route->compile();
  51. $hostTokens = $compiledRoute->getHostTokens();
  52. $debug_message = $this->getRouteDebugMessage($name);
  53. return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens);
  54. }
  55. /**
  56. * Support a route object and any string as route name.
  57. *
  58. * {@inheritdoc}
  59. */
  60. public function supports($name)
  61. {
  62. return is_string($name) || $name instanceof SymfonyRoute;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getRouteDebugMessage($name, array $parameters = array())
  68. {
  69. if (is_scalar($name)) {
  70. return $name;
  71. }
  72. if (is_array($name)) {
  73. return serialize($name);
  74. }
  75. if ($name instanceof RouteObjectInterface) {
  76. return 'Route with key '.$name->getRouteKey();
  77. }
  78. if ($name instanceof SymfonyRoute) {
  79. return 'Route with path '.$name->getPath();
  80. }
  81. return get_class($name);
  82. }
  83. }