source: pro-violet-viettel/sourcecode/application/third_party/Twig/Template.php @ 545

Last change on this file since 545 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 10.1 KB
RevLine 
[345]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 base class for compiled templates.
15 *
16 * @package twig
17 * @author  Fabien Potencier <fabien.potencier@symfony-project.com>
18 */
19abstract class Twig_Template implements Twig_TemplateInterface
20{
21    static protected $cache = array();
22
23    protected $env;
24    protected $blocks;
25
26    /**
27     * Constructor.
28     *
29     * @param Twig_Environment $env A Twig_Environment instance
30     */
31    public function __construct(Twig_Environment $env)
32    {
33        $this->env = $env;
34        $this->blocks = array();
35    }
36
37    /**
38     * Returns the template name.
39     *
40     * @return string The template name
41     */
42    public function getTemplateName()
43    {
44        return null;
45    }
46
47    /**
48     * Returns the Twig environment.
49     *
50     * @return Twig_Environment The Twig environment
51     */
52    public function getEnvironment()
53    {
54        return $this->env;
55    }
56
57    /**
58     * Returns the parent template.
59     *
60     * @return Twig_TemplateInterface|false The parent template or false if there is no parent
61     */
62    public function getParent(array $context)
63    {
64        return false;
65    }
66
67    /**
68     * Displays a parent block.
69     *
70     * @param string $name    The block name to display from the parent
71     * @param array  $context The context
72     * @param array  $blocks  The current set of blocks
73     */
74    public function displayParentBlock($name, array $context, array $blocks = array())
75    {
76        if (false !== $parent = $this->getParent($context)) {
77            $parent->displayBlock($name, $context, $blocks);
78        } else {
79            throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
80        }
81    }
82
83    /**
84     * Displays a block.
85     *
86     * @param string $name    The block name to display
87     * @param array  $context The context
88     * @param array  $blocks  The current set of blocks
89     */
90    public function displayBlock($name, array $context, array $blocks = array())
91    {
92        if (isset($blocks[$name])) {
93            $b = $blocks;
94            unset($b[$name]);
95            call_user_func($blocks[$name], $context, $b);
96        } elseif (isset($this->blocks[$name])) {
97            call_user_func($this->blocks[$name], $context, $blocks);
98        } elseif (false !== $parent = $this->getParent($context)) {
99            $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks));
100        }
101    }
102
103    /**
104     * Renders a parent block.
105     *
106     * @param string $name    The block name to render from the parent
107     * @param array  $context The context
108     * @param array  $blocks  The current set of blocks
109     *
110     * @return string The rendered block
111     */
112    public function renderParentBlock($name, array $context, array $blocks = array())
113    {
114        ob_start();
115        $this->displayParentBlock($name, $context, $blocks);
116
117        return new Twig_Markup(ob_get_clean());
118    }
119
120    /**
121     * Renders a block.
122     *
123     * @param string $name    The block name to render
124     * @param array  $context The context
125     * @param array  $blocks  The current set of blocks
126     *
127     * @return string The rendered block
128     */
129    public function renderBlock($name, array $context, array $blocks = array())
130    {
131        ob_start();
132        $this->displayBlock($name, $context, $blocks);
133
134        return new Twig_Markup(ob_get_clean());
135    }
136
137    /**
138     * Returns whether a block exists or not.
139     *
140     * @param string $name The block name
141     *
142     * @return Boolean true if the block exists, false otherwise
143     */
144    public function hasBlock($name)
145    {
146        return isset($this->blocks[$name]);
147    }
148
149    /**
150     * Returns all block names.
151     *
152     * @return array An array of block names
153     */
154    public function getBlockNames()
155    {
156        return array_keys($this->blocks);
157    }
158
159    /**
160     * Displays the template with the given context.
161     *
162     * @param array $context An array of parameters to pass to the template
163     * @param array $blocks  An array of blocks to pass to the template
164     */
165    public function display(array $context, array $blocks = array())
166    {
167        try {
168            $this->doDisplay($context, $blocks);
169        } catch (Twig_Error $e) {
170            throw $e;
171        } catch (Exception $e) {
172            throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
173        }
174    }
175
176    /**
177     * Renders the template with the given context and returns it as string.
178     *
179     * @param array $context An array of parameters to pass to the template
180     *
181     * @return string The rendered template
182     */
183    public function render(array $context)
184    {
185        ob_start();
186        try {
187            $this->display($context);
188        } catch (Exception $e) {
189            // the count variable avoids an infinite loop on
190            // some Windows configurations where ob_get_level()
191            // never reaches 0
192            $count = 100;
193            while (ob_get_level() && --$count) {
194                ob_end_clean();
195            }
196
197            throw $e;
198        }
199
200        return ob_get_clean();
201    }
202
203    /**
204     * Auto-generated method to display the template with the given context.
205     *
206     * @param array $context An array of parameters to pass to the template
207     * @param array $blocks  An array of blocks to pass to the template
208     */
209    abstract protected function doDisplay(array $context, array $blocks = array());
210
211    /**
212     * Returns a variable from the context.
213     *
214     * @param array   $context The context
215     * @param string  $item    The variable to return from the context
216     *
217     * @param mixed The variable value in the context
218     *
219     * @throws Twig_Error_Runtime if the variable does not exist
220     */
221    protected function getContext($context, $item)
222    {
223        if (!array_key_exists($item, $context)) {
224            throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
225        }
226
227        return $context[$item];
228    }
229
230    /**
231     * Returns the attribute value for a given array/object.
232     *
233     * @param mixed   $object        The object or array from where to get the item
234     * @param mixed   $item          The item to get from the array or object
235     * @param array   $arguments     An array of arguments to pass if the item is an object method
236     * @param integer $type          The type of attribute (@see Twig_TemplateInterface)
237     * @param Boolean $noStrictCheck Whether to throw an exception if the item does not exist ot not
238     */
239    protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $noStrictCheck = false)
240    {
241        // array
242        if (Twig_TemplateInterface::METHOD_CALL !== $type) {
243            if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) {
244                return $object[$item];
245            }
246
247            if (Twig_TemplateInterface::ARRAY_CALL === $type) {
248                if (!$this->env->isStrictVariables() || $noStrictCheck) {
249                    return null;
250                }
251
252                if (is_object($object)) {
253                    throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
254                // array
255                } else {
256                    throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
257                }
258            }
259        }
260
261        if (!is_object($object)) {
262            if (!$this->env->isStrictVariables() || $noStrictCheck) {
263                return null;
264            }
265            throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object));
266        }
267
268        // get some information about the object
269        $class = get_class($object);
270        if (!isset(self::$cache[$class])) {
271            $r = new ReflectionClass($class);
272            self::$cache[$class] = array('methods' => array(), 'properties' => array());
273            foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
274                self::$cache[$class]['methods'][strtolower($method->getName())] = true;
275            }
276
277            foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
278                self::$cache[$class]['properties'][$property->getName()] = true;
279            }
280        }
281
282        // object property
283        if (Twig_TemplateInterface::METHOD_CALL !== $type) {
284            if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item)) {
285                if ($this->env->hasExtension('sandbox')) {
286                    $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
287                }
288
289                return $object->$item;
290            }
291        }
292
293        // object method
294        $lcItem = strtolower($item);
295        if (isset(self::$cache[$class]['methods'][$lcItem])) {
296            $method = $item;
297        } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
298            $method = 'get'.$item;
299        } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
300            $method = 'is'.$item;
301        } elseif (isset(self::$cache[$class]['methods']['__call'])) {
302            $method = $item;
303        } else {
304            if (!$this->env->isStrictVariables() || $noStrictCheck) {
305                return null;
306            }
307
308            throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
309        }
310
311        if ($this->env->hasExtension('sandbox')) {
312            $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
313        }
314
315        $ret = call_user_func_array(array($object, $method), $arguments);
316
317        if ($object instanceof Twig_TemplateInterface) {
318            return new Twig_Markup($ret);
319        }
320
321        return $ret;
322    }
323}
Note: See TracBrowser for help on using the repository browser.