Project

General

Profile

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
 * Loads templates from other loaders.
14
 *
15
 * @package twig
16
 * @author  Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Loader_Chain implements Twig_LoaderInterface
19
{
20
    protected $loaders;
21

    
22
    /**
23
     * Constructor.
24
     *
25
     * @param Twig_LoaderInterface[] $loaders An array of loader instances
26
     */
27
    public function __construct(array $loaders = array())
28
    {
29
        $this->loaders = array();
30
        foreach ($loaders as $loader) {
31
            $this->addLoader($loader);
32
        }
33
    }
34

    
35
    /**
36
     * Adds a loader instance.
37
     *
38
     * @param Twig_LoaderInterface $loader A Loader instance
39
     */
40
    public function addLoader(Twig_LoaderInterface $loader)
41
    {
42
        $this->loaders[] = $loader;
43
    }
44

    
45
    /**
46
     * Gets the source code of a template, given its name.
47
     *
48
     * @param  string $name The name of the template to load
49
     *
50
     * @return string The template source code
51
     */
52
    public function getSource($name)
53
    {
54
        foreach ($this->loaders as $loader) {
55
            try {
56
                return $loader->getSource($name);
57
            } catch (Twig_Error_Loader $e) {
58
            }
59
        }
60

    
61
        throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
62
    }
63

    
64
    /**
65
     * Gets the cache key to use for the cache for a given template name.
66
     *
67
     * @param  string $name The name of the template to load
68
     *
69
     * @return string The cache key
70
     */
71
    public function getCacheKey($name)
72
    {
73
        foreach ($this->loaders as $loader) {
74
            try {
75
                return $loader->getCacheKey($name);
76
            } catch (Twig_Error_Loader $e) {
77
            }
78
        }
79

    
80
        throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
81
    }
82

    
83
    /**
84
     * Returns true if the template is still fresh.
85
     *
86
     * @param string    $name The template name
87
     * @param timestamp $time The last modification time of the cached template
88
     */
89
    public function isFresh($name, $time)
90
    {
91
        foreach ($this->loaders as $loader) {
92
            try {
93
                return $loader->isFresh($name, $time);
94
            } catch (Twig_Error_Loader $e) {
95
            }
96
        }
97

    
98
        throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
99
    }
100
}
(2-2/4)