1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MultiDomainBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
6
|
|
|
use Twig\Extension\AbstractExtension; |
7
|
|
|
use Twig\TwigFunction; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @final since 5.4 |
11
|
|
|
*/ |
12
|
|
|
class MultiDomainTwigExtension extends AbstractExtension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DomainConfigurationInterface |
16
|
|
|
*/ |
17
|
|
|
private $domainConfiguration; |
18
|
|
|
|
19
|
|
|
public function __construct(DomainConfigurationInterface $domainConfiguration) |
20
|
|
|
{ |
21
|
|
|
$this->domainConfiguration = $domainConfiguration; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get Twig functions defined in this extension. |
26
|
|
|
* |
27
|
|
|
* @return array |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
public function getFunctions() |
30
|
|
|
{ |
31
|
|
|
return array( |
32
|
|
|
new TwigFunction('get_multi_domain_hosts', array($this, 'getMultiDomainHosts')), |
33
|
|
|
new TwigFunction('get_current_host', array($this, 'getCurrentHost')), |
34
|
|
|
new TwigFunction('get_extra_data', array($this, 'getExtraData')), |
35
|
|
|
new TwigFunction('get_current_full_host', array($this, 'getCurrentFullHost')), |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $key |
41
|
|
|
*/ |
42
|
|
|
public function getExtraData($key) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$extraData = $this->domainConfiguration->getExtraData(); |
45
|
|
|
|
46
|
|
|
if ($extraData[$key]) { |
47
|
|
|
return $extraData[$key]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getMultiDomainHosts() |
57
|
|
|
{ |
58
|
|
|
return $this->domainConfiguration->getHosts(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getCurrentHost() |
65
|
|
|
{ |
66
|
|
|
return $this->domainConfiguration->getHost(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function getCurrentFullHost() |
73
|
|
|
{ |
74
|
|
|
return $this->domainConfiguration->getFullHost(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.