FileDependency.php 1.25 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8 9
namespace yii\caching;

10 11
use yii\base\InvalidConfigException;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * FileDependency represents a dependency based on a file's last modification time.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16
 * If th last modification time of the file specified via [[fileName]] is changed,
 * the dependency is considered as changed.
Qiang Xue committed
17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
19
 * @since 2.0
Qiang Xue committed
20
 */
Qiang Xue committed
21
class FileDependency extends Dependency
Qiang Xue committed
22 23 24
{
	/**
	 * @var string the name of the file whose last modification time is used to
25 26
	 * check if the dependency has been changed. This property must be always set,
	 * otherwise an exception would be raised.
Qiang Xue committed
27 28 29 30
	 */
	public $fileName;

	/**
31
	 * Initializes the database dependency object.
Qiang Xue committed
32
	 */
33
	public function init()
Qiang Xue committed
34
	{
35 36 37
		if ($this->file === null) {
			throw new InvalidConfigException('FileDependency::fileName must be set.');
		}
Qiang Xue committed
38 39 40 41 42 43 44
	}

	/**
	 * Generates the data needed to determine if dependency has been changed.
	 * This method returns the file's last modification time.
	 * @return mixed the data needed to determine if dependency has been changed.
	 */
Qiang Xue committed
45
	protected function generateDependencyData()
Qiang Xue committed
46
	{
Qiang Xue committed
47
		return @filemtime($this->fileName);
Qiang Xue committed
48 49
	}
}