Dependency.php 1.42 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Qiang Xue committed
3
 * Dependency class file.
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
10 11
namespace yii\caching;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * Dependency is the base class for cache dependency classes.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16
 * Child classes should override its [[generateDependencyData()]] for generating
 * the actual dependency data.
Qiang Xue committed
17 18 19 20
 *
 * @property boolean $hasChanged Whether the dependency has changed.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
21
 * @since 2.0
Qiang Xue committed
22
 */
Qiang Xue committed
23
abstract class Dependency extends \yii\base\Object
Qiang Xue committed
24 25
{
	/**
Qiang Xue committed
26 27
	 * @var mixed the dependency data that is saved in cache and later is compared with the
	 * latest dependency data.
Qiang Xue committed
28
	 */
Qiang Xue committed
29
	public $data;
Qiang Xue committed
30 31 32 33 34 35 36

	/**
	 * Evaluates the dependency by generating and saving the data related with dependency.
	 * This method is invoked by cache before writing data into it.
	 */
	public function evaluateDependency()
	{
Qiang Xue committed
37
		$this->data = $this->generateDependencyData();
Qiang Xue committed
38 39 40 41 42 43 44
	}

	/**
	 * @return boolean whether the dependency has changed.
	 */
	public function getHasChanged()
	{
Qiang Xue committed
45
		return $this->generateDependencyData() !== $this->data;
Qiang Xue committed
46 47 48 49
	}

	/**
	 * Generates the data needed to determine if dependency has been changed.
Qiang Xue committed
50
	 * Derived classes should override this method to generate the actual dependency data.
Qiang Xue committed
51 52
	 * @return mixed the data needed to determine if dependency has been changed.
	 */
Qiang Xue committed
53
	abstract protected function generateDependencyData();
Qiang Xue committed
54
}