Project

General

Profile

« Previous | Next » 

Revision 1863

Added by darkviper almost 12 years ago

updated Twig template engine to stable version 1.12.2

View differences:

branches/2.8.x/CHANGELOG
11 11
! = Update/Change
12 12
===============================================================================
13 13

  
14
19 Feb-2013 Build 1862 Werner v.d.Decken(DarkViper)
15
! updated Twig template engine to stable version 1.12.2
14 16
18 Feb-2013 Build 1861 Werner v.d.Decken(DarkViper)
15 17
+ added temporary class WbAdaptor (replacement for future registry)
16 18
04 Feb-2013 Build 1860 Werner v.d.Decken(DarkViper)
branches/2.8.x/wb/include/Sensio/Twig/LICENSE
1
Copyright (c) 2009-2013 by the Twig Team, see AUTHORS for more details.
2

  
3
Some rights reserved.
4

  
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are
7
met:
8

  
9
    * Redistributions of source code must retain the above copyright
10
      notice, this list of conditions and the following disclaimer.
11

  
12
    * Redistributions in binary form must reproduce the above
13
      copyright notice, this list of conditions and the following
14
      disclaimer in the documentation and/or other materials provided
15
      with the distribution.
16

  
17
    * The names of the contributors may not be used to endorse or
18
      promote products derived from this software without specific
19
      prior written permission.
20

  
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
branches/2.8.x/wb/include/Sensio/Twig/AUTHORS
1
Twig is written and maintained by the Twig Team:
2

  
3
Lead Developer:
4

  
5
- Fabien Potencier <fabien.potencier@symfony-project.org>
6

  
7
Project Founder:
8

  
9
- Armin Ronacher <armin.ronacher@active-4.com>
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Test/IntegrationTestCase.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Integration test helper
14
 *
15
 * @package twig
16
 * @author  Fabien Potencier <fabien@symfony.com>
17
 * @author  Karma Dordrak <drak@zikula.org>
18
 */
19
abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
20
{
21
    abstract protected function getExtensions();
22
    abstract protected function getFixturesDir();
23

  
24
    /**
25
     * @dataProvider getTests
26
     */
27
    public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
28
    {
29
        $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
30
    }
31

  
32
    public function getTests()
33
    {
34
        $fixturesDir = realpath($this->getFixturesDir());
35
        $tests = array();
36

  
37
        foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
38
            if (!preg_match('/\.test$/', $file)) {
39
                continue;
40
            }
41

  
42
            $test = file_get_contents($file->getRealpath());
43

  
44
            if (preg_match('/
45
                    --TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
46
                $message = $match[1];
47
                $condition = $match[2];
48
                $templates = $this->parseTemplates($match[3]);
49
                $exception = $match[5];
50
                $outputs = array(array(null, $match[4], null, ''));
51
            } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
52
                $message = $match[1];
53
                $condition = $match[2];
54
                $templates = $this->parseTemplates($match[3]);
55
                $exception = false;
56
                preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
57
            } else {
58
                throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
59
            }
60

  
61
            $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
62
        }
63

  
64
        return $tests;
65
    }
66

  
67
    protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
68
    {
69
        if ($condition) {
70
            eval('$ret = '.$condition.';');
71
            if (!$ret) {
72
                $this->markTestSkipped($condition);
73
            }
74
        }
75

  
76
        $loader = new Twig_Loader_Array($templates);
77

  
78
        foreach ($outputs as $match) {
79
            $config = array_merge(array(
80
                'cache' => false,
81
                'strict_variables' => true,
82
            ), $match[2] ? eval($match[2].';') : array());
83
            $twig = new Twig_Environment($loader, $config);
84
            $twig->addGlobal('global', 'global');
85
            foreach ($this->getExtensions() as $extension) {
86
                $twig->addExtension($extension);
87
            }
88

  
89
            try {
90
                $template = $twig->loadTemplate('index.twig');
91
            } catch (Exception $e) {
92
                if (false !== $exception) {
93
                    $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
94

  
95
                    return;
96
                }
97

  
98
                if ($e instanceof Twig_Error_Syntax) {
99
                    $e->setTemplateFile($file);
100

  
101
                    throw $e;
102
                }
103

  
104
                throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
105
            }
106

  
107
            try {
108
                $output = trim($template->render(eval($match[1].';')), "\n ");
109
            } catch (Exception $e) {
110
                if (false !== $exception) {
111
                    $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
112

  
113
                    return;
114
                }
115

  
116
                if ($e instanceof Twig_Error_Syntax) {
117
                    $e->setTemplateFile($file);
118
                } else {
119
                    $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
120
                }
121

  
122
                $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
123
            }
124

  
125
            if (false !== $exception) {
126
                list($class, ) = explode(':', $exception);
127
                $this->assertThat(NULL, new PHPUnit_Framework_Constraint_Exception($class));
128
            }
129

  
130
            $expected = trim($match[3], "\n ");
131

  
132
            if ($expected != $output) {
133
                echo 'Compiled template that failed:';
134

  
135
                foreach (array_keys($templates) as $name) {
136
                    echo "Template: $name\n";
137
                    $source = $loader->getSource($name);
138
                    echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
139
                }
140
            }
141
            $this->assertEquals($expected, $output, $message.' (in '.$file.')');
142
        }
143
    }
144

  
145
    protected static function parseTemplates($test)
146
    {
147
        $templates = array();
148
        preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
149
        foreach ($matches as $match) {
150
            $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
151
        }
152

  
153
        return $templates;
154
    }
155
}
0 156

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Test/Method.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a method template test.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 * @deprecated since 1.12 (to be removed in 2.0)
18
 */
19
class Twig_Test_Method extends Twig_Test
20
{
21
    protected $extension;
22
    protected $method;
23

  
24
    public function __construct(Twig_ExtensionInterface $extension, $method, array $options = array())
25
    {
26
        $options['callable'] = array($extension, $method);
27

  
28
        parent::__construct($options);
29

  
30
        $this->extension = $extension;
31
        $this->method = $method;
32
    }
33

  
34
    public function compile()
35
    {
36
        return sprintf('$this->env->getExtension(\'%s\')->%s', $this->extension->getName(), $this->method);
37
    }
38
}
0 39

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Test/NodeTestCase.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
12
{
13
    abstract public function getTests();
14

  
15
    /**
16
     * @dataProvider getTests
17
     */
18
    public function testCompile($node, $source, $environment = null)
19
    {
20
        $this->assertNodeCompilation($source, $node, $environment);
21
    }
22

  
23
    public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null)
24
    {
25
        $compiler = $this->getCompiler($environment);
26
        $compiler->compile($node);
27

  
28
        $this->assertEquals($source, trim($compiler->getSource()));
29
    }
30

  
31
    protected function getCompiler(Twig_Environment $environment = null)
32
    {
33
        return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
34
    }
35

  
36
    protected function getEnvironment()
37
    {
38
        return new Twig_Environment();
39
    }
40

  
41
    protected function getVariableGetter($name)
42
    {
43
        if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
44
            return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
45
        }
46

  
47
        return sprintf('$this->getContext($context, "%s")', $name);
48
    }
49

  
50
    protected function getAttributeGetter()
51
    {
52
        if (function_exists('twig_template_get_attributes')) {
53
            return 'twig_template_get_attributes($this, ';
54
        }
55

  
56
        return '$this->getAttribute(';
57
    }
58
}
0 59

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Test/Function.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a function template test.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 * @deprecated since 1.12 (to be removed in 2.0)
18
 */
19
class Twig_Test_Function extends Twig_Test
20
{
21
    protected $function;
22

  
23
    public function __construct($function, array $options = array())
24
    {
25
        $options['callable'] = $function;
26

  
27
        parent::__construct($options);
28

  
29
        $this->function = $function;
30
    }
31

  
32
    public function compile()
33
    {
34
        return $this->function;
35
    }
36
}
0 37

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Test/Node.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a template test as a Node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 * @deprecated since 1.12 (to be removed in 2.0)
18
 */
19
class Twig_Test_Node extends Twig_Test
20
{
21
    protected $class;
22

  
23
    public function __construct($class, array $options = array())
24
    {
25
        parent::__construct($options);
26

  
27
        $this->class = $class;
28
    }
29

  
30
    public function getClass()
31
    {
32
        return $this->class;
33
    }
34

  
35
    public function compile()
36
    {
37
    }
38
}
0 39

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Parser.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 * (c) 2009 Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

  
13
/**
14
 * Default parser implementation.
15
 *
16
 * @package twig
17
 * @author  Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Parser implements Twig_ParserInterface
20
{
21
    protected $stack = array();
22
    protected $stream;
23
    protected $parent;
24
    protected $handlers;
25
    protected $visitors;
26
    protected $expressionParser;
27
    protected $blocks;
28
    protected $blockStack;
29
    protected $macros;
30
    protected $env;
31
    protected $reservedMacroNames;
32
    protected $importedSymbols;
33
    protected $traits;
34
    protected $embeddedTemplates = array();
35

  
36
    /**
37
     * Constructor.
38
     *
39
     * @param Twig_Environment $env A Twig_Environment instance
40
     */
41
    public function __construct(Twig_Environment $env)
42
    {
43
        $this->env = $env;
44
    }
45

  
46
    public function getEnvironment()
47
    {
48
        return $this->env;
49
    }
50

  
51
    public function getVarName()
52
    {
53
        return sprintf('__internal_%s', hash('sha1', uniqid(mt_rand(), true), false));
54
    }
55

  
56
    public function getFilename()
57
    {
58
        return $this->stream->getFilename();
59
    }
60

  
61
    /**
62
     * Converts a token stream to a node tree.
63
     *
64
     * @param Twig_TokenStream $stream A token stream instance
65
     *
66
     * @return Twig_Node_Module A node tree
67
     */
68
    public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = false)
69
    {
70
        // push all variables into the stack to keep the current state of the parser
71
        $vars = get_object_vars($this);
72
        unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser']);
73
        $this->stack[] = $vars;
74

  
75
        // tag handlers
76
        if (null === $this->handlers) {
77
            $this->handlers = $this->env->getTokenParsers();
78
            $this->handlers->setParser($this);
79
        }
80

  
81
        // node visitors
82
        if (null === $this->visitors) {
83
            $this->visitors = $this->env->getNodeVisitors();
84
        }
85

  
86
        if (null === $this->expressionParser) {
87
            $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
88
        }
89

  
90
        $this->stream = $stream;
91
        $this->parent = null;
92
        $this->blocks = array();
93
        $this->macros = array();
94
        $this->traits = array();
95
        $this->blockStack = array();
96
        $this->importedSymbols = array(array());
97
        $this->embeddedTemplates = array();
98

  
99
        try {
100
            $body = $this->subparse($test, $dropNeedle);
101

  
102
            if (null !== $this->parent) {
103
                if (null === $body = $this->filterBodyNodes($body)) {
104
                    $body = new Twig_Node();
105
                }
106
            }
107
        } catch (Twig_Error_Syntax $e) {
108
            if (!$e->getTemplateFile()) {
109
                $e->setTemplateFile($this->getFilename());
110
            }
111

  
112
            if (!$e->getTemplateLine()) {
113
                $e->setTemplateLine($this->stream->getCurrent()->getLine());
114
            }
115

  
116
            throw $e;
117
        }
118

  
119
        $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this->getFilename());
120

  
121
        $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
122

  
123
        $node = $traverser->traverse($node);
124

  
125
        // restore previous stack so previous parse() call can resume working
126
        foreach (array_pop($this->stack) as $key => $val) {
127
            $this->$key = $val;
128
        }
129

  
130
        return $node;
131
    }
132

  
133
    public function subparse($test, $dropNeedle = false)
134
    {
135
        $lineno = $this->getCurrentToken()->getLine();
136
        $rv = array();
137
        while (!$this->stream->isEOF()) {
138
            switch ($this->getCurrentToken()->getType()) {
139
                case Twig_Token::TEXT_TYPE:
140
                    $token = $this->stream->next();
141
                    $rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
142
                    break;
143

  
144
                case Twig_Token::VAR_START_TYPE:
145
                    $token = $this->stream->next();
146
                    $expr = $this->expressionParser->parseExpression();
147
                    $this->stream->expect(Twig_Token::VAR_END_TYPE);
148
                    $rv[] = new Twig_Node_Print($expr, $token->getLine());
149
                    break;
150

  
151
                case Twig_Token::BLOCK_START_TYPE:
152
                    $this->stream->next();
153
                    $token = $this->getCurrentToken();
154

  
155
                    if ($token->getType() !== Twig_Token::NAME_TYPE) {
156
                        throw new Twig_Error_Syntax('A block must start with a tag name', $token->getLine(), $this->getFilename());
157
                    }
158

  
159
                    if (null !== $test && call_user_func($test, $token)) {
160
                        if ($dropNeedle) {
161
                            $this->stream->next();
162
                        }
163

  
164
                        if (1 === count($rv)) {
165
                            return $rv[0];
166
                        }
167

  
168
                        return new Twig_Node($rv, array(), $lineno);
169
                    }
170

  
171
                    $subparser = $this->handlers->getTokenParser($token->getValue());
172
                    if (null === $subparser) {
173
                        if (null !== $test) {
174
                            $error = sprintf('Unexpected tag name "%s"', $token->getValue());
175
                            if (is_array($test) && isset($test[0]) && $test[0] instanceof Twig_TokenParserInterface) {
176
                                $error .= sprintf(' (expecting closing tag for the "%s" tag defined near line %s)', $test[0]->getTag(), $lineno);
177
                            }
178

  
179
                            throw new Twig_Error_Syntax($error, $token->getLine(), $this->getFilename());
180
                        }
181

  
182
                        $message = sprintf('Unknown tag name "%s"', $token->getValue());
183
                        if ($alternatives = $this->env->computeAlternatives($token->getValue(), array_keys($this->env->getTags()))) {
184
                            $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
185
                        }
186

  
187
                        throw new Twig_Error_Syntax($message, $token->getLine(), $this->getFilename());
188
                    }
189

  
190
                    $this->stream->next();
191

  
192
                    $node = $subparser->parse($token);
193
                    if (null !== $node) {
194
                        $rv[] = $node;
195
                    }
196
                    break;
197

  
198
                default:
199
                    throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', 0, $this->getFilename());
200
            }
201
        }
202

  
203
        if (1 === count($rv)) {
204
            return $rv[0];
205
        }
206

  
207
        return new Twig_Node($rv, array(), $lineno);
208
    }
209

  
210
    public function addHandler($name, $class)
211
    {
212
        $this->handlers[$name] = $class;
213
    }
214

  
215
    public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
216
    {
217
        $this->visitors[] = $visitor;
218
    }
219

  
220
    public function getBlockStack()
221
    {
222
        return $this->blockStack;
223
    }
224

  
225
    public function peekBlockStack()
226
    {
227
        return $this->blockStack[count($this->blockStack) - 1];
228
    }
229

  
230
    public function popBlockStack()
231
    {
232
        array_pop($this->blockStack);
233
    }
234

  
235
    public function pushBlockStack($name)
236
    {
237
        $this->blockStack[] = $name;
238
    }
239

  
240
    public function hasBlock($name)
241
    {
242
        return isset($this->blocks[$name]);
243
    }
244

  
245
    public function getBlock($name)
246
    {
247
        return $this->blocks[$name];
248
    }
249

  
250
    public function setBlock($name, $value)
251
    {
252
        $this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getLine());
253
    }
254

  
255
    public function hasMacro($name)
256
    {
257
        return isset($this->macros[$name]);
258
    }
259

  
260
    public function setMacro($name, Twig_Node_Macro $node)
261
    {
262
        if (null === $this->reservedMacroNames) {
263
            $this->reservedMacroNames = array();
264
            $r = new ReflectionClass($this->env->getBaseTemplateClass());
265
            foreach ($r->getMethods() as $method) {
266
                $this->reservedMacroNames[] = $method->getName();
267
            }
268
        }
269

  
270
        if (in_array($name, $this->reservedMacroNames)) {
271
            throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword', $name), $node->getLine(), $this->getFilename());
272
        }
273

  
274
        $this->macros[$name] = $node;
275
    }
276

  
277
    public function addTrait($trait)
278
    {
279
        $this->traits[] = $trait;
280
    }
281

  
282
    public function hasTraits()
283
    {
284
        return count($this->traits) > 0;
285
    }
286

  
287
    public function embedTemplate(Twig_Node_Module $template)
288
    {
289
        $template->setIndex(mt_rand());
290

  
291
        $this->embeddedTemplates[] = $template;
292
    }
293

  
294
    public function addImportedSymbol($type, $alias, $name = null, Twig_Node_Expression $node = null)
295
    {
296
        $this->importedSymbols[0][$type][$alias] = array('name' => $name, 'node' => $node);
297
    }
298

  
299
    public function getImportedSymbol($type, $alias)
300
    {
301
        foreach ($this->importedSymbols as $functions) {
302
            if (isset($functions[$type][$alias])) {
303
                return $functions[$type][$alias];
304
            }
305
        }
306
    }
307

  
308
    public function isMainScope()
309
    {
310
        return 1 === count($this->importedSymbols);
311
    }
312

  
313
    public function pushLocalScope()
314
    {
315
        array_unshift($this->importedSymbols, array());
316
    }
317

  
318
    public function popLocalScope()
319
    {
320
        array_shift($this->importedSymbols);
321
    }
322

  
323
    /**
324
     * Gets the expression parser.
325
     *
326
     * @return Twig_ExpressionParser The expression parser
327
     */
328
    public function getExpressionParser()
329
    {
330
        return $this->expressionParser;
331
    }
332

  
333
    public function getParent()
334
    {
335
        return $this->parent;
336
    }
337

  
338
    public function setParent($parent)
339
    {
340
        $this->parent = $parent;
341
    }
342

  
343
    /**
344
     * Gets the token stream.
345
     *
346
     * @return Twig_TokenStream The token stream
347
     */
348
    public function getStream()
349
    {
350
        return $this->stream;
351
    }
352

  
353
    /**
354
     * Gets the current token.
355
     *
356
     * @return Twig_Token The current token
357
     */
358
    public function getCurrentToken()
359
    {
360
        return $this->stream->getCurrent();
361
    }
362

  
363
    protected function filterBodyNodes(Twig_NodeInterface $node)
364
    {
365
        // check that the body does not contain non-empty output nodes
366
        if (
367
            ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')))
368
            ||
369
            (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
370
        ) {
371
            if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
372
                throw new Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->getFilename());
373
            }
374

  
375
            throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->getFilename());
376
        }
377

  
378
        // bypass "set" nodes as they "capture" the output
379
        if ($node instanceof Twig_Node_Set) {
380
            return $node;
381
        }
382

  
383
        if ($node instanceof Twig_NodeOutputInterface) {
384
            return;
385
        }
386

  
387
        foreach ($node as $k => $n) {
388
            if (null !== $n && null === $n = $this->filterBodyNodes($n)) {
389
                $node->removeNode($k);
390
            }
391
        }
392

  
393
        return $node;
394
    }
395
}
0 396

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/ExtensionInterface.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Interface implemented by extension classes.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
interface Twig_ExtensionInterface
19
{
20
    /**
21
     * Initializes the runtime environment.
22
     *
23
     * This is where you can load some file that contains filter functions for instance.
24
     *
25
     * @param Twig_Environment $environment The current Twig_Environment instance
26
     */
27
    public function initRuntime(Twig_Environment $environment);
28

  
29
    /**
30
     * Returns the token parser instances to add to the existing list.
31
     *
32
     * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
33
     */
34
    public function getTokenParsers();
35

  
36
    /**
37
     * Returns the node visitor instances to add to the existing list.
38
     *
39
     * @return array An array of Twig_NodeVisitorInterface instances
40
     */
41
    public function getNodeVisitors();
42

  
43
    /**
44
     * Returns a list of filters to add to the existing list.
45
     *
46
     * @return array An array of filters
47
     */
48
    public function getFilters();
49

  
50
    /**
51
     * Returns a list of tests to add to the existing list.
52
     *
53
     * @return array An array of tests
54
     */
55
    public function getTests();
56

  
57
    /**
58
     * Returns a list of functions to add to the existing list.
59
     *
60
     * @return array An array of functions
61
     */
62
    public function getFunctions();
63

  
64
    /**
65
     * Returns a list of operators to add to the existing list.
66
     *
67
     * @return array An array of operators
68
     */
69
    public function getOperators();
70

  
71
    /**
72
     * Returns a list of global variables to add to the existing list.
73
     *
74
     * @return array An array of global variables
75
     */
76
    public function getGlobals();
77

  
78
    /**
79
     * Returns the name of the extension.
80
     *
81
     * @return string The extension name
82
     */
83
    public function getName();
84
}
0 85

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/TokenParser.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Base class for all token parsers.
14
 *
15
 * @package twig
16
 * @author  Fabien Potencier <fabien@symfony.com>
17
 */
18
abstract class Twig_TokenParser implements Twig_TokenParserInterface
19
{
20
    /**
21
     * @var Twig_Parser
22
     */
23
    protected $parser;
24

  
25
    /**
26
     * Sets the parser associated with this token parser
27
     *
28
     * @param $parser A Twig_Parser instance
29
     */
30
    public function setParser(Twig_Parser $parser)
31
    {
32
        $this->parser = $parser;
33
    }
34
}
0 35

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/AutoEscape.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents an autoescape node.
14
 *
15
 * The value is the escaping strategy (can be html, js, ...)
16
 *
17
 * The true value is equivalent to html.
18
 *
19
 * If autoescaping is disabled, then the value is false.
20
 *
21
 * @package    twig
22
 * @author     Fabien Potencier <fabien@symfony.com>
23
 */
24
class Twig_Node_AutoEscape extends Twig_Node
25
{
26
    public function __construct($value, Twig_NodeInterface $body, $lineno, $tag = 'autoescape')
27
    {
28
        parent::__construct(array('body' => $body), array('value' => $value), $lineno, $tag);
29
    }
30

  
31
    /**
32
     * Compiles the node to PHP.
33
     *
34
     * @param Twig_Compiler A Twig_Compiler instance
35
     */
36
    public function compile(Twig_Compiler $compiler)
37
    {
38
        $compiler->subcompile($this->getNode('body'));
39
    }
40
}
0 41

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/Block.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 * (c) 2009 Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

  
13
/**
14
 * Represents a block node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_Block extends Twig_Node
20
{
21
    public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler
34
            ->addDebugInfo($this)
35
            ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")
36
            ->indent()
37
        ;
38

  
39
        $compiler
40
            ->subcompile($this->getNode('body'))
41
            ->outdent()
42
            ->write("}\n\n")
43
        ;
44
    }
45
}
0 46

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/Set.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a set node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Set extends Twig_Node
19
{
20
    public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag);
23

  
24
        /*
25
         * Optimizes the node when capture is used for a large block of text.
26
         *
27
         * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
28
         */
29
        if ($this->getAttribute('capture')) {
30
            $this->setAttribute('safe', true);
31

  
32
            $values = $this->getNode('values');
33
            if ($values instanceof Twig_Node_Text) {
34
                $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
35
                $this->setAttribute('capture', false);
36
            }
37
        }
38
    }
39

  
40
    /**
41
     * Compiles the node to PHP.
42
     *
43
     * @param Twig_Compiler A Twig_Compiler instance
44
     */
45
    public function compile(Twig_Compiler $compiler)
46
    {
47
        $compiler->addDebugInfo($this);
48

  
49
        if (count($this->getNode('names')) > 1) {
50
            $compiler->write('list(');
51
            foreach ($this->getNode('names') as $idx => $node) {
52
                if ($idx) {
53
                    $compiler->raw(', ');
54
                }
55

  
56
                $compiler->subcompile($node);
57
            }
58
            $compiler->raw(')');
59
        } else {
60
            if ($this->getAttribute('capture')) {
61
                $compiler
62
                    ->write("ob_start();\n")
63
                    ->subcompile($this->getNode('values'))
64
                ;
65
            }
66

  
67
            $compiler->subcompile($this->getNode('names'), false);
68

  
69
            if ($this->getAttribute('capture')) {
70
                $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
71
            }
72
        }
73

  
74
        if (!$this->getAttribute('capture')) {
75
            $compiler->raw(' = ');
76

  
77
            if (count($this->getNode('names')) > 1) {
78
                $compiler->write('array(');
79
                foreach ($this->getNode('values') as $idx => $value) {
80
                    if ($idx) {
81
                        $compiler->raw(', ');
82
                    }
83

  
84
                    $compiler->subcompile($value);
85
                }
86
                $compiler->raw(')');
87
            } else {
88
                if ($this->getAttribute('safe')) {
89
                    $compiler
90
                        ->raw("('' === \$tmp = ")
91
                        ->subcompile($this->getNode('values'))
92
                        ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())")
93
                    ;
94
                } else {
95
                    $compiler->subcompile($this->getNode('values'));
96
                }
97
            }
98
        }
99

  
100
        $compiler->raw(";\n");
101
    }
102
}
0 103

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/Print.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 * (c) 2009 Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

  
13
/**
14
 * Represents a node that outputs an expression.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_Print extends Twig_Node implements Twig_NodeOutputInterface
20
{
21
    public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler
34
            ->addDebugInfo($this)
35
            ->write('echo ')
36
            ->subcompile($this->getNode('expr'))
37
            ->raw(";\n")
38
        ;
39
    }
40
}
0 41

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/Body.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a body node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Body extends Twig_Node
19
{
20
}
0 21

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/Sandbox.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a sandbox node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Sandbox extends Twig_Node
19
{
20
    public function __construct(Twig_NodeInterface $body, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('body' => $body), array(), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        $compiler
33
            ->addDebugInfo($this)
34
            ->write("\$sandbox = \$this->env->getExtension('sandbox');\n")
35
            ->write("if (!\$alreadySandboxed = \$sandbox->isSandboxed()) {\n")
36
            ->indent()
37
            ->write("\$sandbox->enableSandbox();\n")
38
            ->outdent()
39
            ->write("}\n")
40
            ->subcompile($this->getNode('body'))
41
            ->write("if (!\$alreadySandboxed) {\n")
42
            ->indent()
43
            ->write("\$sandbox->disableSandbox();\n")
44
            ->outdent()
45
            ->write("}\n")
46
        ;
47
    }
48
}
0 49

  
branches/2.8.x/wb/include/Sensio/Twig/lib/Twig/Node/If.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 * (c) 2009 Armin Ronacher
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12

  
13
/**
14
 * Represents an if node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_If extends Twig_Node
20
{
21
    public function __construct(Twig_NodeInterface $tests, Twig_NodeInterface $else = null, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('tests' => $tests, 'else' => $else), array(), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler->addDebugInfo($this);
34
        for ($i = 0; $i < count($this->getNode('tests')); $i += 2) {
35
            if ($i > 0) {
36
                $compiler
37
                    ->outdent()
38
                    ->write("} elseif (")
39
                ;
40
            } else {
41
                $compiler
42
                    ->write('if (')
43
                ;
44
            }
45

  
46
            $compiler
47
                ->subcompile($this->getNode('tests')->getNode($i))
48
                ->raw(") {\n")
49
                ->indent()
50
                ->subcompile($this->getNode('tests')->getNode($i + 1))
51
            ;
52
        }
53

  
54
        if ($this->hasNode('else') && null !== $this->getNode('else')) {
55
            $compiler
56
                ->outdent()
57
                ->write("} else {\n")
58
                ->indent()
59
                ->subcompile($this->getNode('else'))
60
            ;
61
        }
62

  
63
        $compiler
64
            ->outdent()
65
            ->write("}\n");
66
    }
67
}
0 68

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff