TwigSimpleFileLoader.php 1.64 KB
Newer Older
1
<?php
dev-meghraj committed
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
7 8 9 10

namespace yii\twig;

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

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

30 31 32
    /**
     * Compare a file's freshness with previously stored timestamp
     *
33 34
     * @param string $name file name to check
     * @param integer $time timestamp to compare with
35 36 37 38 39 40
     * @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
41

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

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

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