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 ONGR\ElasticsearchBundle\Mapping\MetadataCollector; |
15
|
|
|
use ONGR\RouterBundle\Document\SeoAwareInterface; |
16
|
|
|
use Symfony\Cmf\Component\Routing\ProviderBasedGenerator; |
17
|
|
|
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface; |
18
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
19
|
|
|
use Symfony\Component\Routing\Route; |
20
|
|
|
|
21
|
|
|
class DocumentUrlGenerator extends ProviderBasedGenerator implements VersatileGeneratorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array Route map configuration to map Elasticsearch types and Controllers. |
25
|
|
|
*/ |
26
|
|
|
private $routeMap; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var MetadataCollector |
30
|
|
|
*/ |
31
|
|
|
private $collector; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function getRouteMap() |
37
|
|
|
{ |
38
|
|
|
return $this->routeMap; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $routeMap |
43
|
|
|
*/ |
44
|
|
|
public function setRouteMap($routeMap) |
45
|
|
|
{ |
46
|
|
|
$this->routeMap = $routeMap; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return MetadataCollector |
51
|
|
|
*/ |
52
|
|
|
public function getCollector() |
53
|
|
|
{ |
54
|
|
|
return $this->collector; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param MetadataCollector $collector |
59
|
|
|
*/ |
60
|
|
|
public function setCollector(MetadataCollector $collector) |
61
|
|
|
{ |
62
|
|
|
$this->collector = $collector; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
|
|
if ($name instanceof SeoAwareInterface) { |
72
|
|
|
$documentUrl = $name->getUrl(); |
73
|
|
|
} else { |
74
|
|
|
throw new RouteNotFoundException(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$type = $this->collector->getDocumentType(get_class($name)); |
78
|
|
|
$route = new Route( |
79
|
|
|
$documentUrl, |
80
|
|
|
[ |
81
|
|
|
'_controller' => $this->routeMap[$type], |
82
|
|
|
'document' => $name, |
83
|
|
|
'type' => $type, |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
// the Route has a cache of its own and is not recompiled as long as it does not get modified |
88
|
|
|
$compiledRoute = $route->compile(); |
89
|
|
|
$hostTokens = $compiledRoute->getHostTokens(); |
90
|
|
|
|
91
|
|
|
$debug_message = $this->getRouteDebugMessage($name); |
92
|
|
|
|
93
|
|
|
return $this->doGenerate( |
94
|
|
|
$compiledRoute->getVariables(), |
95
|
|
|
$route->getDefaults(), |
96
|
|
|
$route->getRequirements(), |
97
|
|
|
$compiledRoute->getTokens(), |
98
|
|
|
$parameters, |
99
|
|
|
$debug_message, |
100
|
|
|
$referenceType, |
101
|
|
|
$hostTokens |
102
|
|
|
); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
throw new RouteNotFoundException('Document is not correct or route cannot be generated.'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
/** |
108
|
|
|
* @param mixed $name The route "name" which may also be an object or anything |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
* @throws RouteNotFoundException |
112
|
|
|
*/ |
113
|
|
|
public function supports($name) |
114
|
|
|
{ |
115
|
|
|
if ($name instanceof SeoAwareInterface) { |
116
|
|
|
return true; |
117
|
|
|
} else { |
118
|
|
|
throw new RouteNotFoundException('$name must be an instance of SeoAwareInterface'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
/** |
122
|
|
|
* @param mixed $name |
123
|
|
|
* @param array $parameters which should contain a content field containing |
124
|
|
|
* a RouteReferrersReadInterface object |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getRouteDebugMessage($name, array $parameters = array()) |
129
|
|
|
{ |
130
|
|
|
if ($name instanceof SeoAwareInterface) { |
131
|
|
|
return 'The route object is fit for parsing to generate() method'; |
132
|
|
|
} else { |
133
|
|
|
return $name.' must be an instance of SeoAwareInterface'; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..