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