1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Provider\ORM; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface; |
18
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
21
|
|
|
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider as BaseRouteProvider; |
22
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
23
|
|
|
use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface; |
24
|
|
|
use Symfony\Component\Routing\RouteCollection; |
25
|
|
|
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route; |
26
|
|
|
|
27
|
|
|
class RouteProvider extends BaseRouteProvider implements RouteProviderInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var RouteRepositoryInterface |
31
|
|
|
*/ |
32
|
|
|
private $routeRepository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $internalRoutesCache; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var CandidatesInterface |
41
|
|
|
*/ |
42
|
111 |
|
private $candidatesStrategy; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* RouteProvider constructor. |
46
|
|
|
* |
47
|
|
|
* @param RouteRepositoryInterface $routeRepository |
48
|
111 |
|
* @param ManagerRegistry $managerRegistry |
49
|
111 |
|
* @param CandidatesInterface $candidatesStrategy |
50
|
111 |
|
* @param string $className |
51
|
|
|
*/ |
52
|
111 |
|
public function __construct(RouteRepositoryInterface $routeRepository, ManagerRegistry $managerRegistry, CandidatesInterface $candidatesStrategy, string $className) |
53
|
111 |
|
{ |
54
|
|
|
$this->routeRepository = $routeRepository; |
55
|
|
|
$this->internalRoutesCache = []; |
56
|
|
|
$this->candidatesStrategy = $candidatesStrategy; |
57
|
|
|
|
58
|
|
|
parent::__construct($managerRegistry, $candidatesStrategy, $className); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getRouteCollectionForRequest(Request $request) |
65
|
|
|
{ |
66
|
|
|
$collection = new RouteCollection(); |
67
|
|
|
|
68
|
|
|
$candidates = $this->candidatesStrategy->getCandidates($request); |
69
|
|
|
if (0 === count($candidates)) { |
70
|
|
|
return $collection; |
71
|
8 |
|
} |
72
|
|
|
// As we use Gedmo Sortable on position field, we need to reverse sorting to get child routes first |
73
|
8 |
|
$routes = $this->getByStaticPrefix($candidates, ['level' => 'DESC', 'position' => 'DESC']); |
74
|
|
|
|
75
|
|
|
/** @var $route Route */ |
76
|
|
|
foreach ($routes as $route) { |
77
|
|
|
$collection->add($route->getName(), $route); |
78
|
|
|
} |
79
|
2 |
|
|
80
|
|
|
return $collection; |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return RouteRepositoryInterface |
85
|
|
|
*/ |
86
|
|
|
public function getRepository(): RouteRepositoryInterface |
87
|
12 |
|
{ |
88
|
|
|
return $this->routeRepository; |
89
|
12 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getBaseRoute() |
95
|
96 |
|
{ |
96
|
|
|
throw new \Exception('Not implemented in ORM'); |
97
|
96 |
|
} |
98
|
58 |
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
96 |
|
*/ |
102
|
|
|
public function getOneById($id) |
103
|
|
|
{ |
104
|
|
|
return $this->routeRepository->findOneBy(['id' => $id]); |
105
|
96 |
|
} |
106
|
96 |
|
|
107
|
96 |
|
/** |
108
|
80 |
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getOneByName(string $name) |
111
|
24 |
|
{ |
112
|
|
|
return $this->routeRepository->findOneBy(['name' => $name]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getOneByStaticPrefix($staticPrefix) |
119
|
|
|
{ |
120
|
|
|
return $this->routeRepository->findOneBy(['staticPrefix' => $staticPrefix]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getByStaticPrefix(array $candidates, array $orderBy = []): array |
127
|
|
|
{ |
128
|
|
|
return $this->getRouteRepository()->findBy(['staticPrefix' => $candidates], $orderBy); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function getChildrenByStaticPrefix(array $candidates, array $orderBy = []): array |
135
|
|
|
{ |
136
|
|
|
return $this->getRepository()->getChildrenByStaticPrefix($candidates, $orderBy)->getQuery()->getResult(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function getWithChildrenByStaticPrefix(array $candidates): ?array |
143
|
|
|
{ |
144
|
|
|
$routes = null; |
|
|
|
|
145
|
|
|
$routesForChilldrensLoading = []; |
146
|
|
|
foreach ($candidates as $key => $providedRoute) { |
147
|
|
|
if (false !== strpos($providedRoute, '/*')) { |
148
|
|
|
$cleanRouteName = str_replace('/*', '', $providedRoute); |
149
|
|
|
$routesForChilldrensLoading[$cleanRouteName] = null; |
150
|
|
|
$candidates[$key] = $cleanRouteName; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$routesArray = $this->getByStaticPrefix($candidates); |
155
|
|
|
if (count($routesArray) <= 0) { |
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$routes = $this->getArrayOfIdsFromRoutesArray($routesArray); |
160
|
|
|
|
161
|
|
|
if (count($routesForChilldrensLoading) > 0) { |
162
|
|
|
foreach ($routesArray as $key => $element) { |
163
|
|
|
if (array_key_exists($element->getStaticPrefix(), $routesForChilldrensLoading)) { |
164
|
|
|
$routesForChilldrensLoading[$element->getStaticPrefix()] = $element->getId(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$routesForChilldrensLoading = array_filter(array_values($routesForChilldrensLoading)); |
169
|
|
|
$childrenRoutesArray = $this->getChildrenByStaticPrefix($routesForChilldrensLoading); |
170
|
|
|
if (count($childrenRoutesArray) > 0) { |
171
|
|
|
$routes = array_merge($routes, $this->getArrayOfIdsFromRoutesArray($childrenRoutesArray)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $routes; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
*/ |
181
|
|
|
public function getRouteByName($name) |
182
|
|
|
{ |
183
|
|
|
if (array_key_exists($name, $this->internalRoutesCache)) { |
184
|
|
|
return $this->internalRoutesCache[$name]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!$this->candidatesStrategy->isCandidate($name)) { |
188
|
|
|
throw new RouteNotFoundException(sprintf('Route "%s" is not handled by this route provider', $name)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$route = $this->getRouteRepository()->findOneBy(array('name' => $name)); |
192
|
|
|
$this->internalRoutesCache[$name] = $route; |
193
|
|
|
if (!$route) { |
194
|
|
|
throw new RouteNotFoundException("No route found for name '$name'"); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $route; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function getByMixed($routeData) |
204
|
|
|
{ |
205
|
|
|
if (is_int($routeData)) { |
206
|
|
|
$route = $this->getOneById($routeData); |
207
|
|
|
} elseif (is_string($routeData)) { |
208
|
|
|
if (false !== strpos($routeData, '/')) { |
209
|
|
|
$route = $this->getOneByStaticPrefix($routeData); |
210
|
|
|
} else { |
211
|
|
|
$route = $this->getRouteByName($routeData); |
212
|
|
|
} |
213
|
|
|
} elseif (is_array($routeData)) { |
214
|
|
|
$loadByStaticPrefix = true; |
215
|
|
|
foreach ($routeData as $key => $providedRoute) { |
216
|
|
|
if (!is_string($providedRoute)) { |
217
|
|
|
$loadByStaticPrefix = false; |
218
|
|
|
|
219
|
|
|
break; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if ($loadByStaticPrefix) { |
224
|
|
|
$route = $this->getWithChildrenByStaticPrefix($routeData); |
225
|
|
|
} else { |
226
|
|
|
$route = $routeData; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $route; |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $routes |
235
|
|
|
* |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
|
private function getArrayOfIdsFromRoutesArray(array $routes): array |
239
|
|
|
{ |
240
|
|
|
return array_map(function ($route) { |
241
|
|
|
return $route->getId(); |
242
|
|
|
}, $routes); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|