summaryrefslogtreecommitdiffstats
path: root/src/Controller/View/Twig.php
blob: 38e97d0e072cb99777c6b3ff3de15a38e8964cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

namespace Jasny\Controller\View;

use Psr\Http\Message\ServerRequestInterface;

/**
 * View using Twig
 */
trait Twig
{
    /**
     * Twig environment
     * @var \Twig_Environment
     */
    protected $twig;


    /**
     * Get server request
     * @return ServerRequestInterface
     */
    abstract public function getRequest();

    /**
     * Output result
     *
     * @param mixed  $data
     * @param string $format  Output format as MIME or extension
     */
    abstract public function output($data, $format = null);


    /**
     * Get path of the view files
     *
     * @return string
     */
    protected function getViewPath()
    {
        return getcwd();
    }

    /**
     * Assert valid variable, function and filter name
     * 
     * @param string $name
     * @throws \InvalidArgumentException
     */
    protected function assertViewVariableName($name)
    {
        if (!is_string($name)) {
            $type = (is_object($name) ? get_class($name) . ' ' : '') . gettype($name);
            throw new \InvalidArgumentException("Expected name to be a string, not a $type");
        }

        if (!preg_match('/^[a-z]\w*$/i', $name)) {
            throw new \InvalidArgumentException("Invalid name '$name'");
        }
    }
    
    /**
     * Add a global variable to the view.
     *
     * @param string $name   Variable name
     * @param mixed  $value
     * @return $this
     */
    public function setViewVariable($name, $value)
    {
        $this->assertViewVariableName($name);

        $this->getTwig()->addGlobal($name, $value);

        return $this;
    }

    /**
     * Expose a function to the view.
     *
     * @param string      $name      Function name
     * @param string|null $function
     * @param string      $as        'function' or 'filter'
     * @return $this
     */
    public function setViewFunction($name, $function = null, $as = 'function')
    {
        $this->assertViewVariableName($name);
        
        if ($as === 'function') {
            $function = new \Twig_SimpleFunction($name, $function ?: $name);
            $this->getTwig()->addFunction($function);
        } elseif ($as === 'filter') {
            $filter = new \Twig_SimpleFilter($name, $function ?: $name);
            $this->getTwig()->addFilter($filter);
        } else {
            $not = is_string($as) ? "'$as'" : 'a ' . gettype($as);
            throw new \InvalidArgumentException("You should create either a 'function' or 'filter', not $not");
        }

        return $this;
    }

    /**
     * Get twig environment instance
     *
     * @return \Twig_Environment
     */
    public function createTwigEnvironment()
    {
        $path = $this->getViewPath();
        $loader = new \Twig_Loader_Filesystem($path);

        return new \Twig_Environment($loader);
    }

    /**
     * Initialize the Twig environment
     */
    protected function initTwig()
    {
        $this->twig = $this->createTwigEnvironment();

        $extensions = ['DateExtension', 'PcreExtension', 'TextExtension', 'ArrayExtension'];
        foreach ($extensions as $name) {
            $class = "Jasny\Twig\\$name";

            if (class_exists($class)) {
                $this->twig->addExtension(new $class());
            }
        }

        $this->twig->addGlobal('current_url', $this->getRequest()->getUri());
        
        if (method_exists($this, 'flash')) {
            $this->twig->addGlobal('flash', $this->flash());
        }
    }

    /**
     * Get Twig environment
     *
     * @return \Twig_Environment
     */
    public function getTwig()
    {
        if (!isset($this->twig)) {
            $this->initTwig();
        }

        return $this->twig;
    }


    /**
     * View rendered template
     *
     * @param string $name    Template name
     * @param array  $context Template context
     */
    public function view($name, array $context = [])
    {
        if (!pathinfo($name, PATHINFO_EXTENSION)) {
            $name .= '.html.twig';
        }

        $twig = $this->getTwig();
        $tmpl = $twig->loadTemplate($name);

        $this->output($tmpl->render($context), 'text/html; charset=' . $twig->getCharset());
    }
}