TwigSimpleFileLoader.php 1.54 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

namespace yii\twig;

dev-meghraj committed
12

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

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

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

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

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

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