1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
6
|
|
|
use Twig\Environment; |
7
|
|
|
use Twig\Extension\AbstractExtension; |
8
|
|
|
use Twig\TwigFunction; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* LocaleSwitcherTwigExtension |
12
|
|
|
* |
13
|
|
|
* @final since 5.4 |
14
|
|
|
*/ |
15
|
|
|
class LocaleSwitcherTwigExtension extends AbstractExtension |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var DomainConfigurationInterface |
19
|
|
|
*/ |
20
|
|
|
private $domainConfiguration; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param DomainConfigurationInterface $domainConfiguration |
24
|
|
|
*/ |
25
|
|
|
public function __construct(DomainConfigurationInterface $domainConfiguration) |
26
|
|
|
{ |
27
|
|
|
$this->domainConfiguration = $domainConfiguration; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get Twig functions defined in this extension. |
32
|
|
|
* |
33
|
|
|
* @return array |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
public function getFunctions() |
36
|
|
|
{ |
37
|
|
|
return array( |
38
|
|
|
new TwigFunction('localeswitcher_widget', array($this, 'renderWidget'), array('needs_environment' => true, 'is_safe' => array('html'))), |
39
|
|
|
new TwigFunction('get_locales', array($this, 'getLocales')), |
40
|
|
|
new TwigFunction('get_backend_locales', array($this, 'getBackendLocales')), |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Render locale switcher widget. |
46
|
|
|
* |
47
|
|
|
* @param Environment $env |
48
|
|
|
* @param array $locales The locales |
49
|
|
|
* @param string $route The route |
50
|
|
|
* @param array $parameters The route parameters |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function renderWidget(Environment $env, $locales, $route, array $parameters = array()) |
55
|
|
|
{ |
56
|
|
|
$template = $env->load( |
57
|
|
|
'@KunstmaanAdmin/LocaleSwitcherTwigExtension/widget.html.twig' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return $template->render( |
61
|
|
|
array_merge( |
62
|
|
|
$parameters, |
63
|
|
|
array( |
64
|
|
|
'locales' => $locales, |
65
|
|
|
'route' => $route, |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getLocales() |
75
|
|
|
{ |
76
|
|
|
return $this->domainConfiguration->getFrontendLocales(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getBackendLocales($switchedHost = null) |
83
|
|
|
{ |
84
|
|
|
return $this->domainConfiguration->getBackendLocales($switchedHost); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.