Completed
Push — master ( 3bb683...8cf661 )
by Korotkov
01:35
created

TwigFunctions::container()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace App\Web\Supports;
4
5
use Twig_SimpleFunction;
6
use Twig_Environment;
7
use Rudra\ContainerInterface;
8
9
trait TwigFunctions
10
{
11
12
    public function templateEngine(array $config): void
13
    {
14
        parent::templateEngine($config);
15
16
        $d = new Twig_SimpleFunction('d', function ($var) {
17
            return d($var);
18
        });
19
20
        $this->getTwig()->addFunction($d);
21
22
        $date = new Twig_SimpleFunction('date', function ($var) {
23
            return date($var);
24
        });
25
26
        $this->getTwig()->addFunction($date);
27
28
        $auth = new Twig_SimpleFunction('auth', function () {
29
            return $this->container()->get('auth')->access(true);
30
        });
31
32
        $this->getTwig()->addFunction($auth);
33
34
        $active = new Twig_SimpleFunction('active', function ($link, $page) {
35
            if ($link == $page) {
36
                echo 'class="active"';
37
            }
38
        });
39
40
        $this->getTwig()->addFunction($active);
41
42
        $value = new Twig_SimpleFunction('value', function ($var) {
43
            if ($this->container()->hasSession('value', $var)) {
44
                return $this->container()->getSession('value', $var);
45
            }
46
        });
47
48
        $this->getTwig()->addFunction($value);
49
50
        $alert = new Twig_SimpleFunction('alert', function ($value, $style, $label = null) {
51
            if ($this->container()->hasSession('alert', $value)) {
52
                return '<div class="alert alert-' . $style . '" style="padding: 15px">' . $this->container()->getSession('alert', $value) . $label . '</div>';
53
            }
54
        });
55
56
        $this->getTwig()->addFunction($alert);
57
58
        if (DEV) {
59
            $debugbarRenderer = $this->container()->get('debugbar')->getJavascriptRenderer();
60
            $this->getTwig()->addGlobal('debugbar', $debugbarRenderer);
61
        }
62
    }
63
64
    /**
65
     * @return Twig_Environment
66
     */
67
    public abstract function getTwig(): Twig_Environment;
68
69
    /**
70
     * @return mixed
71
     */
72
    public abstract function container(): ContainerInterface;
73
}