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\ElasticsearchBundle\Result\Result; |
16
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
17
|
|
|
use ONGR\ElasticsearchDSL\Query\MatchQuery; |
18
|
|
|
use ONGR\ElasticsearchDSL\Search; |
19
|
|
|
use Symfony\Cmf\Component\Routing\RouteProviderInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
22
|
|
|
use Symfony\Component\Routing\Route; |
23
|
|
|
use Symfony\Component\Routing\RouteCollection; |
24
|
|
|
|
25
|
|
|
class ElasticsearchRouteProvider implements RouteProviderInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var array Route map configuration to map Elasticsearch types and Controllers |
29
|
|
|
*/ |
30
|
|
|
private $routeMap; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Manager |
34
|
|
|
*/ |
35
|
|
|
private $manager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var MetadataCollector |
39
|
|
|
*/ |
40
|
|
|
private $collector; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* ElasticsearchRouteProvider constructor. |
44
|
|
|
* |
45
|
|
|
* @param array $routeMap |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $routeMap = [], $collector) |
48
|
|
|
{ |
49
|
|
|
$this->routeMap = $routeMap; |
50
|
|
|
$this->collector = $collector; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns Elasticsearch manager instance that was set in app/config.yml. |
55
|
|
|
* |
56
|
|
|
* @return Manager |
57
|
|
|
*/ |
58
|
|
|
public function getManager() |
59
|
|
|
{ |
60
|
|
|
return $this->manager; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Manager $manager |
65
|
|
|
*/ |
66
|
|
|
public function setManager(Manager $manager) |
67
|
|
|
{ |
68
|
|
|
$this->manager = $manager; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritDoc |
73
|
|
|
*/ |
74
|
|
|
public function getRouteCollectionForRequest(Request $request) |
75
|
|
|
{ |
76
|
|
|
if (!$this->manager) { |
77
|
|
|
throw new \Exception('Manager must be set to execute query to the elasticsearch'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$routeCollection = new RouteCollection(); |
81
|
|
|
$requestPath = $request->getPathInfo(); |
82
|
|
|
|
83
|
|
|
$search = new Search(); |
84
|
|
|
$search->addQuery(new MatchQuery('url', $requestPath)); |
85
|
|
|
|
86
|
|
|
$results = $this->manager->execute(array_keys($this->routeMap), $search, Result::RESULTS_OBJECT); |
87
|
|
|
|
88
|
|
|
foreach ($results as $document) { |
89
|
|
|
|
90
|
|
|
$type = $this->collector->getDocumentType(get_class($document)); |
91
|
|
|
|
92
|
|
|
if (array_key_exists($type, $this->routeMap)) { |
93
|
|
|
$route = new Route( |
94
|
|
|
$requestPath, |
95
|
|
|
[ |
96
|
|
|
'_controller' => $this->routeMap[$type], |
97
|
|
|
'document' => $document |
98
|
|
|
] |
99
|
|
|
); |
100
|
|
|
$routeCollection->add('ongr_route_'.$type, $route); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $routeCollection; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
|
|
public function getRouteByName($name) |
111
|
|
|
{ |
112
|
|
|
// TODO: Implement getRouteByName() method. |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
|
|
public function getRoutesByNames($names) |
119
|
|
|
{ |
120
|
|
|
// TODO: Implement getRoutesByNames() method. |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|