Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

AdminBundle/Twig/LocaleSwitcherTwigExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Twig;
4
5
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
6
7
/**
8
 * LocaleSwitcherTwigExtension
9
 */
10
class LocaleSwitcherTwigExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var DomainConfigurationInterface
14
     */
15
    private $domainConfiguration;
16
17
    /**
18
     * @param DomainConfigurationInterface $domainConfiguration
19
     */
20
    public function __construct(DomainConfigurationInterface $domainConfiguration)
21
    {
22
        $this->domainConfiguration = $domainConfiguration;
23
    }
24
25
    /**
26
     * Get Twig functions defined in this extension.
27
     *
28
     * @return array
29
     */
30
    public function getFunctions()
31
    {
32
        return array(
33
            new \Twig_SimpleFunction('localeswitcher_widget', array($this, 'renderWidget'), array('needs_environment' => true, 'is_safe' => array('html'))),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
34
            new \Twig_SimpleFunction('get_locales', array($this, 'getLocales')),
35
            new \Twig_SimpleFunction('get_backend_locales', array($this, 'getBackendLocales')),
36
        );
37
    }
38
39
    /**
40
     * Render locale switcher widget.
41
     *
42
     * @param \Twig_Environment $env
43
     * @param array             $locales    The locales
44
     * @param string            $route      The route
45
     * @param array             $parameters The route parameters
46
     *
47
     * @return string
48
     */
49
    public function renderWidget(\Twig_Environment $env, $locales, $route, array $parameters = array())
50
    {
51
        $template = $env->loadTemplate(
52
            'KunstmaanAdminBundle:LocaleSwitcherTwigExtension:widget.html.twig'
53
        );
54
55
        return $template->render(
56
            array_merge(
57
                $parameters,
58
                array(
59
                    'locales' => $locales,
60
                    'route' => $route,
61
                )
62
            )
63
        );
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getLocales()
70
    {
71
        return $this->domainConfiguration->getFrontendLocales();
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function getBackendLocales($switchedHost = null)
78
    {
79
        return $this->domainConfiguration->getBackendLocales($switchedHost);
80
    }
81
}
82