ExpressionDependency.php 1.42 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Qiang Xue committed
3
 * ExpressionDependency 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
 * ExpressionDependency represents a dependency based on the result of a PHP expression.
Qiang Xue committed
14
 *
15 16
 * ExpressionDependency will use `eval()` to evaluate the PHP expression.
 * The dependency is reported as unchanged if and only if the result of the expression is
Qiang Xue committed
17 18 19
 * the same as the one evaluated when storing the data to cache.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
20
 * @since 2.0
Qiang Xue committed
21
 */
Qiang Xue committed
22
class ExpressionDependency extends Dependency
Qiang Xue committed
23 24 25 26 27 28 29 30 31
{
	/**
	 * @var string the PHP expression whose result is used to determine the dependency.
	 */
	public $expression;

	/**
	 * Constructor.
	 * @param string $expression the PHP expression whose result is used to determine the dependency.
Qiang Xue committed
32
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
33
	 */
Qiang Xue committed
34
	public function __construct($expression = 'true', $config = array())
Qiang Xue committed
35
	{
Qiang Xue committed
36
		$this->expression = $expression;
Qiang Xue committed
37
		parent::__construct($config);
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 result of the PHP expression.
	 * @return mixed the data needed to determine if dependency has been changed.
	 */
Qiang Xue committed
45
	protected function generateDependencyData()
Qiang Xue committed
46
	{
47
		return eval("return {$this->expression};");
Qiang Xue committed
48 49
	}
}