1<?php
2
3/*
4 * This file is part of Mustache.php.
5 *
6 * (c) 2010-2017 Justin Hileman
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 * Mustache Template filesystem Loader implementation.
14 *
15 * A FilesystemLoader instance loads Mustache Template source from the filesystem by name:
16 *
17 *     $loader = new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views');
18 *     $tpl = $loader->load('foo'); // equivalent to `file_get_contents(dirname(__FILE__).'/views/foo.mustache');
19 *
20 * This is probably the most useful Mustache Loader implementation. It can be used for partials and normal Templates:
21 *
22 *     $m = new Mustache(array(
23 *          'loader'          => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),
24 *          'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials'),
25 *     ));
26 */
27class Mustache_Loader_FilesystemLoader implements Mustache_Loader
28{
29    private $baseDir;
30    private $extension = '.mustache';
31    private $templates = array();
32
33    /**
34     * Mustache filesystem Loader constructor.
35     *
36     * Passing an $options array allows overriding certain Loader options during instantiation:
37     *
38     *     $options = array(
39     *         // The filename extension used for Mustache templates. Defaults to '.mustache'
40     *         'extension' => '.ms',
41     *     );
42     *
43     * @throws Mustache_Exception_RuntimeException if $baseDir does not exist
44     *
45     * @param string $baseDir Base directory containing Mustache template files
46     * @param array  $options Array of Loader options (default: array())
47     */
48    public function __construct($baseDir, array $options = array())
49    {
50        $this->baseDir = $baseDir;
51
52        if (strpos($this->baseDir, '://') === false) {
53            $this->baseDir = realpath($this->baseDir);
54        }
55
56        if ($this->shouldCheckPath() && !is_dir($this->baseDir)) {
57            throw new Mustache_Exception_RuntimeException(sprintf('FilesystemLoader baseDir must be a directory: %s', $baseDir));
58        }
59
60        if (array_key_exists('extension', $options)) {
61            if (empty($options['extension'])) {
62                $this->extension = '';
63            } else {
64                $this->extension = '.' . ltrim($options['extension'], '.');
65            }
66        }
67    }
68
69    /**
70     * Load a Template by name.
71     *
72     *     $loader = new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views');
73     *     $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.mustache";
74     *
75     * @param string $name
76     *
77     * @return string Mustache Template source
78     */
79    public function load($name)
80    {
81        if (!isset($this->templates[$name])) {
82            $this->templates[$name] = $this->loadFile($name);
83        }
84
85        return $this->templates[$name];
86    }
87
88    /**
89     * Helper function for loading a Mustache file by name.
90     *
91     * @throws Mustache_Exception_UnknownTemplateException If a template file is not found
92     *
93     * @param string $name
94     *
95     * @return string Mustache Template source
96     */
97    protected function loadFile($name)
98    {
99        $fileName = $this->getFileName($name);
100
101        if ($this->shouldCheckPath() && !file_exists($fileName)) {
102            throw new Mustache_Exception_UnknownTemplateException($name);
103        }
104
105        return file_get_contents($fileName);
106    }
107
108    /**
109     * Helper function for getting a Mustache template file name.
110     *
111     * @param string $name
112     *
113     * @return string Template file name
114     */
115    protected function getFileName($name)
116    {
117        $fileName = $this->baseDir . '/' . $name;
118        if (substr($fileName, 0 - strlen($this->extension)) !== $this->extension) {
119            $fileName .= $this->extension;
120        }
121
122        return $fileName;
123    }
124
125    /**
126     * Only check if baseDir is a directory and requested templates are files if
127     * baseDir is using the filesystem stream wrapper.
128     *
129     * @return bool Whether to check `is_dir` and `file_exists`
130     */
131    protected function shouldCheckPath()
132    {
133        return strpos($this->baseDir, '://') === false || strpos($this->baseDir, 'file://') === 0;
134    }
135}
136