TwigSimpleFileLoader.php 1.71 KB
Newer Older
1
<?php
dev-meghraj committed
2 3 4 5 6 7 8
/**
 * Simple file system wrapper for twig to process twig files
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
9 10 11 12

namespace yii\twig;

/**
dev-meghraj committed
13
 * Twig view file loader class.
14 15 16
 *
 * @author dev-mraj <dev.meghraj@gmail.com>
 */
17 18
class TwigSimpleFileLoader implements \Twig_LoaderInterface
{
19 20 21 22
    /**
     * @var string Path to directory
     */
    private $_dir;
dev-meghraj committed
23

24 25 26 27 28 29 30
    /**
     * @param string $dir path to directory
     */
    public function __construct($dir)
    {
        $this->_dir = $dir;
    }
dev-meghraj committed
31

32 33 34 35 36 37 38 39 40 41 42
    /**
     * Compare a file's freshness with previously stored timestamp
     *
     * @param  string  $name file name to check
     * @param  integer $time timestamp to compare with
     * @return boolean true if file is still fresh and not changes, false otherwise
     */
    public function isFresh($name, $time)
    {
        return filemtime($this->getFilePath($name)) <= $time;
    }
dev-meghraj committed
43

44 45 46 47 48 49 50 51 52 53
    /**
     * Get the source of given file name
     *
     * @param  string $name file name
     * @return string contents of given file name
     */
    public function getSource($name)
    {
        return file_get_contents($this->getFilePath($name));
    }
dev-meghraj committed
54

55 56 57 58 59 60 61 62 63
    /**
     * Get unique key that can represent this file uniquely among other files.
     * @param  string $name
     * @return string
     */
    public function getCacheKey($name)
    {
        return $this->getFilePath($name);
    }
dev-meghraj committed
64

65 66 67 68 69 70 71 72 73
    /**
     * internally used to get absolute path of given file name
     * @param  string $name file name
     * @return string absolute path of file
     */
    protected function getFilePath($name)
    {
        return $this->_dir . '/' . $name;
    }
AlexGx committed
74
}