4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing;
14 use Symfony\Component\Routing\Generator\UrlGenerator;
15 use Symfony\Component\Routing\Route as SymfonyRoute;
16 use Symfony\Component\Routing\RequestContext;
17 use Symfony\Component\Routing\Exception\RouteNotFoundException;
18 use Psr\Log\LoggerInterface;
21 * A Generator that uses a RouteProvider rather than a RouteCollection.
23 * @author Larry Garfield
25 class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorInterface
28 * The route provider for this generator.
30 * @var RouteProviderInterface
35 * @param RouteProviderInterface $provider
36 * @param LoggerInterface $logger
38 public function __construct(RouteProviderInterface $provider, LoggerInterface $logger = null)
40 $this->provider = $provider;
41 $this->logger = $logger;
42 $this->context = new RequestContext();
48 public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
50 if ($name instanceof SymfonyRoute) {
52 } elseif (null === $route = $this->provider->getRouteByName($name)) {
53 throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
56 // the Route has a cache of its own and is not recompiled as long as it does not get modified
57 $compiledRoute = $route->compile();
58 $hostTokens = $compiledRoute->getHostTokens();
60 $debug_message = $this->getRouteDebugMessage($name);
62 return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens);
66 * Support a route object and any string as route name.
70 public function supports($name)
72 return is_string($name) || $name instanceof SymfonyRoute;
78 public function getRouteDebugMessage($name, array $parameters = array())
80 if (is_scalar($name)) {
84 if (is_array($name)) {
85 return serialize($name);
88 if ($name instanceof RouteObjectInterface) {
89 return 'Route with key '.$name->getRouteKey();
92 if ($name instanceof SymfonyRoute) {
93 return 'Route with path '.$name->getPath();
96 return get_class($name);