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