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 production filesystem Loader implementation.
14 *
15 * A production-ready FilesystemLoader, which doesn't require reading a file if it already exists in the template cache.
16 *
17 * {@inheritdoc}
18 */
19class Mustache_Loader_ProductionFilesystemLoader extends Mustache_Loader_FilesystemLoader
20{
21    private $statProps;
22
23    /**
24     * Mustache production filesystem Loader constructor.
25     *
26     * Passing an $options array allows overriding certain Loader options during instantiation:
27     *
28     *     $options = array(
29     *         // The filename extension used for Mustache templates. Defaults to '.mustache'
30     *         'extension' => '.ms',
31     *         'stat_props' => array('size', 'mtime'),
32     *     );
33     *
34     * Specifying 'stat_props' overrides the stat properties used to invalidate the template cache. By default, this
35     * uses 'mtime' and 'size', but this can be set to any of the properties supported by stat():
36     *
37     *     http://php.net/manual/en/function.stat.php
38     *
39     * You can also disable filesystem stat entirely:
40     *
41     *     $options = array('stat_props' => null);
42     *
43     * But with great power comes great responsibility. Namely, if you disable stat-based cache invalidation,
44     * YOU MUST CLEAR THE TEMPLATE CACHE YOURSELF when your templates change. Make it part of your build or deploy
45     * process so you don't forget!
46     *
47     * @throws Mustache_Exception_RuntimeException if $baseDir does not exist.
48     *
49     * @param string $baseDir Base directory containing Mustache template files.
50     * @param array  $options Array of Loader options (default: array())
51     */
52    public function __construct($baseDir, array $options = array())
53    {
54        parent::__construct($baseDir, $options);
55
56        if (array_key_exists('stat_props', $options)) {
57            if (empty($options['stat_props'])) {
58                $this->statProps = array();
59            } else {
60                $this->statProps = $options['stat_props'];
61            }
62        } else {
63            $this->statProps = array('size', 'mtime');
64        }
65    }
66
67    /**
68     * Helper function for loading a Mustache file by name.
69     *
70     * @throws Mustache_Exception_UnknownTemplateException If a template file is not found.
71     *
72     * @param string $name
73     *
74     * @return Mustache_Source Mustache Template source
75     */
76    protected function loadFile($name)
77    {
78        $fileName = $this->getFileName($name);
79
80        if (!file_exists($fileName)) {
81            throw new Mustache_Exception_UnknownTemplateException($name);
82        }
83
84        return new Mustache_Source_FilesystemSource($fileName, $this->statProps);
85    }
86}
87