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
12namespace Twig\Loader;
13
14use Twig\Error\LoaderError;
15use Twig\Source;
16
17/**
18 * Loads a template from an array.
19 *
20 * When using this loader with a cache mechanism, you should know that a new cache
21 * key is generated each time a template content "changes" (the cache key being the
22 * source code of the template). If you don't want to see your cache grows out of
23 * control, you need to take care of clearing the old cache file by yourself.
24 *
25 * This loader should only be used for unit testing.
26 *
27 * @final
28 *
29 * @author Fabien Potencier <fabien@symfony.com>
30 */
31class ArrayLoader implements LoaderInterface, ExistsLoaderInterface, SourceContextLoaderInterface
32{
33    protected $templates = [];
34
35    /**
36     * @param array $templates An array of templates (keys are the names, and values are the source code)
37     */
38    public function __construct(array $templates = [])
39    {
40        $this->templates = $templates;
41    }
42
43    /**
44     * Adds or overrides a template.
45     *
46     * @param string $name     The template name
47     * @param string $template The template source
48     */
49    public function setTemplate($name, $template)
50    {
51        $this->templates[(string) $name] = $template;
52    }
53
54    public function getSource($name)
55    {
56        @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', \get_class($this)), E_USER_DEPRECATED);
57
58        $name = (string) $name;
59        if (!isset($this->templates[$name])) {
60            throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
61        }
62
63        return $this->templates[$name];
64    }
65
66    public function getSourceContext($name)
67    {
68        $name = (string) $name;
69        if (!isset($this->templates[$name])) {
70            throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
71        }
72
73        return new Source($this->templates[$name], $name);
74    }
75
76    public function exists($name)
77    {
78        return isset($this->templates[(string) $name]);
79    }
80
81    public function getCacheKey($name)
82    {
83        $name = (string) $name;
84        if (!isset($this->templates[$name])) {
85            throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
86        }
87
88        return $name.':'.$this->templates[$name];
89    }
90
91    public function isFresh($name, $time)
92    {
93        $name = (string) $name;
94        if (!isset($this->templates[$name])) {
95            throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
96        }
97
98        return true;
99    }
100}
101
102class_alias('Twig\Loader\ArrayLoader', 'Twig_Loader_Array');
103