ExpressionDependency.php 2.19 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
 * the same as the one evaluated when storing the data to cache.
 *
17 18 19
 * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
 * please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php).
 *
Qiang Xue committed
20
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
21
 * @since 2.0
Qiang Xue committed
22
 */
Qiang Xue committed
23
class ExpressionDependency extends Dependency
Qiang Xue committed
24 25
{
	/**
26
	 * @var string the string representation of a PHP expression whose result is used to determine the dependency.
27
	 * A PHP expression can be any PHP code that evaluates to a value. To learn more about what an expression is,
28
	 * please refer to the [php manual](http://www.php.net/manual/en/language.expressions.php).
Qiang Xue committed
29
	 */
30
	public $expression;
31
	/**
32 33
	 * @var mixed custom parameters associated with this dependency. You may get the value
	 * of this property in [[expression]] using `$this->params`.
34
	 */
35
	public $params;
36 37 38 39

	/**
	 * Constructor.
	 * @param string $expression the PHP expression whose result is used to determine the dependency.
40
	 * @param mixed $params the custom parameters associated with this dependency
41 42
	 * @param array $config name-value pairs that will be used to initialize the object properties
	 */
43
	public function __construct($expression = 'true', $params = null, $config = array())
44 45
	{
		$this->expression = $expression;
46
		$this->params = $params;
47 48
		parent::__construct($config);
	}
Qiang Xue committed
49 50 51 52

	/**
	 * Generates the data needed to determine if dependency has been changed.
	 * This method returns the result of the PHP expression.
53
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
54 55
	 * @return mixed the data needed to determine if dependency has been changed.
	 */
56
	protected function generateDependencyData($cache)
Qiang Xue committed
57
	{
58
		return eval("return {$this->expression};");
Qiang Xue committed
59 60
	}
}