ExpressionDependency.php 1.38 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;

Qiang Xue committed
10
/**
Qiang Xue committed
11
 * ExpressionDependency represents a dependency based on the result of a PHP expression.
Qiang Xue committed
12
 *
13 14
 * 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
15 16 17
 * the same as the one evaluated when storing the data to cache.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
18
 * @since 2.0
Qiang Xue committed
19
 */
Qiang Xue committed
20
class ExpressionDependency extends Dependency
Qiang Xue committed
21 22 23 24
{
	/**
	 * @var string the PHP expression whose result is used to determine the dependency.
	 */
25 26 27 28 29 30 31 32 33 34 35 36
	public $expression;

	/**
	 * Constructor.
	 * @param string $expression the PHP expression whose result is used to determine the dependency.
	 * @param array $config name-value pairs that will be used to initialize the object properties
	 */
	public function __construct($expression = 'true', $config = array())
	{
		$this->expression = $expression;
		parent::__construct($config);
	}
Qiang Xue committed
37 38 39 40 41 42

	/**
	 * 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
43
	protected function generateDependencyData()
Qiang Xue committed
44
	{
45
		return eval("return {$this->expression};");
Qiang Xue committed
46 47
	}
}