Version 1
[yaffs-website] / vendor / symfony-cmf / routing / ProviderBasedGenerator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing;
13
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;
19
20 /**
21  * A Generator that uses a RouteProvider rather than a RouteCollection.
22  *
23  * @author Larry Garfield
24  */
25 class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorInterface
26 {
27     /**
28      * The route provider for this generator.
29      *
30      * @var RouteProviderInterface
31      */
32     protected $provider;
33
34     /**
35      * @param RouteProviderInterface $provider
36      * @param LoggerInterface        $logger
37      */
38     public function __construct(RouteProviderInterface $provider, LoggerInterface $logger = null)
39     {
40         $this->provider = $provider;
41         $this->logger = $logger;
42         $this->context = new RequestContext();
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
49     {
50         if ($name instanceof SymfonyRoute) {
51             $route = $name;
52         } elseif (null === $route = $this->provider->getRouteByName($name)) {
53             throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
54         }
55
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();
59
60         $debug_message = $this->getRouteDebugMessage($name);
61
62         return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens);
63     }
64
65     /**
66      * Support a route object and any string as route name.
67      *
68      * {@inheritdoc}
69      */
70     public function supports($name)
71     {
72         return is_string($name) || $name instanceof SymfonyRoute;
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     public function getRouteDebugMessage($name, array $parameters = array())
79     {
80         if (is_scalar($name)) {
81             return $name;
82         }
83
84         if (is_array($name)) {
85             return serialize($name);
86         }
87
88         if ($name instanceof RouteObjectInterface) {
89             return 'Route with key '.$name->getRouteKey();
90         }
91
92         if ($name instanceof SymfonyRoute) {
93             return 'Route with path '.$name->getPath();
94         }
95
96         return get_class($name);
97     }
98 }