1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MultiDomainBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper; |
7
|
|
|
use Kunstmaan\AdminBundle\Helper\DomainConfiguration as BaseDomainConfiguration; |
8
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
|
11
|
|
|
class DomainConfiguration extends BaseDomainConfiguration |
12
|
|
|
{ |
13
|
|
|
const OVERRIDE_HOST = '_override_host'; |
14
|
|
|
const SWITCH_HOST = '_switch_host'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Node |
18
|
|
|
*/ |
19
|
|
|
protected $rootNode = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $hosts; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $aliases = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AdminRouteHelper |
33
|
|
|
*/ |
34
|
|
|
protected $adminRouteHelper; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ContainerInterface|string $multilanguage |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($requestStack, $multilanguage, $defaultLocale, $requiredLocales); |
42
|
|
|
|
43
|
|
|
if ($requestStack instanceof ContainerInterface) { |
44
|
|
|
@trigger_error('Container injection and the usage of the container is deprecated in KunstmaanNodeBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0.', E_USER_DEPRECATED); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->container = $requestStack; |
47
|
|
|
$this->adminRouteHelper = $this->container->get('kunstmaan_admin.adminroute.helper'); |
48
|
|
|
$this->hosts = $this->container->getParameter('kunstmaan_multi_domain.hosts'); |
|
|
|
|
49
|
|
|
$this->em = $this->container->get('doctrine.orm.entity_manager'); |
|
|
|
|
50
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
$this->adminRouteHelper = $adminRouteHelper; |
53
|
|
|
$this->hosts = $hosts; |
|
|
|
|
54
|
|
|
$this->em = $em; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($this->hosts as $host => $hostInfo) { |
58
|
|
|
if (isset($hostInfo['aliases'])) { |
59
|
|
|
foreach ($hostInfo['aliases'] as $alias) { |
60
|
|
|
$this->aliases[$alias] = $host; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getHost() |
70
|
|
|
{ |
71
|
|
|
if ($this->hasHostOverride()) { |
72
|
|
|
return $this->getHostOverride(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$host = parent::getHost(); |
76
|
|
|
if (isset($this->aliases[$host])) { |
77
|
|
|
$host = $this->aliases[$host]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $host; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function getHosts() |
87
|
|
|
{ |
88
|
|
|
return array_keys($this->hosts); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getDefaultLocale() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$host = $this->getHost(); |
97
|
|
|
if (isset($this->hosts[$host]['default_locale'])) { |
98
|
|
|
return $this->hosts[$host]['default_locale']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return parent::getDefaultLocale(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string|null $host |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function isMultiLanguage($host = null) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$host = $this->getRealHost($host); |
112
|
|
|
|
113
|
|
|
if (isset($this->hosts[$host])) { |
114
|
|
|
$hostInfo = $this->hosts[$host]; |
115
|
|
|
|
116
|
|
|
return ('multi_lang' === $hostInfo['type']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return parent::isMultiLanguage(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|null $host |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function getFrontendLocales($host = null) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$host = $this->getRealHost($host); |
130
|
|
|
|
131
|
|
|
if (isset($this->hosts[$host]['locales'])) { |
132
|
|
|
return array_keys($this->hosts[$host]['locales']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return parent::getBackendLocales(); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string|null $host |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function getBackendLocales($host = null) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$host = $this->getRealHost($host); |
146
|
|
|
|
147
|
|
|
if (isset($this->hosts[$host]['locales'])) { |
148
|
|
|
return array_values($this->hosts[$host]['locales']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return parent::getBackendLocales(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isMultiDomainHost() |
158
|
|
|
{ |
159
|
|
|
$host = $this->getHost(); |
160
|
|
|
|
161
|
|
|
return isset($this->hosts[$host]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Fetch the root node for the current host |
166
|
|
|
* |
167
|
|
|
* @param string|null $host |
168
|
|
|
* |
169
|
|
|
* @return Node|null |
170
|
|
|
*/ |
171
|
|
|
public function getRootNode($host = null) |
172
|
|
|
{ |
173
|
|
|
if (!$this->isMultiDomainHost()) { |
174
|
|
|
return parent::getRootNode(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (is_null($this->rootNode)) { |
178
|
|
|
$host = $this->getRealHost($host); |
179
|
|
|
|
180
|
|
|
$internalName = $this->hosts[$host]['root']; |
181
|
|
|
$nodeRepo = $this->em->getRepository('KunstmaanNodeBundle:Node'); |
182
|
|
|
$this->rootNode = $nodeRepo->getNodeByInternalName($internalName); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->rootNode; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return (optional) extra config settings for the current host |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function getExtraData() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$host = $this->getHost(); |
194
|
|
|
|
195
|
|
|
if (!isset($this->hosts[$host]['extra'])) { |
196
|
|
|
return parent::getExtraData(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->hosts[$host]['extra']; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return (optional) extra config settings for the locales for the current host |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function getLocalesExtraData() |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$host = $this->getHost(); |
208
|
|
|
|
209
|
|
|
if (!isset($this->hosts[$host]['locales_extra'])) { |
210
|
|
|
return parent::getLocalesExtraData(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this->hosts[$host]['locales_extra']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
View Code Duplication |
protected function hasHostOverride() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$request = $this->getMasterRequest(); |
222
|
|
|
|
223
|
|
|
return !is_null($request) && |
224
|
|
|
$this->adminRouteHelper->isAdminRoute($request->getRequestUri()) && |
225
|
|
|
$request->hasPreviousSession() && |
226
|
|
|
$request->getSession()->has(self::OVERRIDE_HOST); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
View Code Duplication |
public function hasHostSwitched() |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$request = $this->getMasterRequest(); |
235
|
|
|
|
236
|
|
|
return !is_null($request) && |
237
|
|
|
$this->adminRouteHelper->isAdminRoute($request->getRequestUri()) && |
238
|
|
|
$request->hasPreviousSession() && |
239
|
|
|
$request->getSession()->has(self::SWITCH_HOST); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string|null |
244
|
|
|
*/ |
245
|
|
|
protected function getHostOverride() |
246
|
|
|
{ |
247
|
|
|
if (null !== ($request = $this->getMasterRequest()) && $request->hasPreviousSession()) { |
248
|
|
|
return $request->getSession()->get(self::OVERRIDE_HOST); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return null; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
public function getHostSwitched() |
258
|
|
|
{ |
259
|
|
|
$request = $this->getMasterRequest(); |
260
|
|
|
|
261
|
|
|
$host = $this->getHost(); |
262
|
|
|
|
263
|
|
|
if ($this->hasHostSwitched()) { |
264
|
|
|
$host = $request->getSession()->get(self::SWITCH_HOST); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $this->hosts[$host]; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return array |
272
|
|
|
*/ |
273
|
|
|
public function getFullHostConfig() |
274
|
|
|
{ |
275
|
|
|
return $this->hosts; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string|null $host |
280
|
|
|
* |
281
|
|
|
* @return array |
282
|
|
|
*/ |
283
|
|
|
public function getFullHost($host = null) |
284
|
|
|
{ |
285
|
|
|
$host = $this->getRealHost($host); |
286
|
|
|
|
287
|
|
|
if ($host && isset($this->hosts[$host])) { |
|
|
|
|
288
|
|
|
return $this->hosts[$host]; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return null; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param int $id |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
|
|
public function getFullHostById($id) |
301
|
|
|
{ |
302
|
|
|
foreach ($this->hosts as $host => $parameters) { |
303
|
|
|
if (!isset($parameters['id']) || $parameters['id'] !== $id) { |
304
|
|
|
continue; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $parameters; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return null; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param string|null $host |
315
|
|
|
* |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
public function getHostBaseUrl($host = null) |
319
|
|
|
{ |
320
|
|
|
$config = $this->getFullHost($host); |
321
|
|
|
|
322
|
|
|
return sprintf('%s://%s', $config['protocol'], $config['host']); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string|null $host |
327
|
|
|
* |
328
|
|
|
* @return null|string |
329
|
|
|
*/ |
330
|
|
|
private function getRealHost($host = null) |
331
|
|
|
{ |
332
|
|
|
if (!$host) { |
|
|
|
|
333
|
|
|
$host = $this->getHost(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
return $host; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.