ChainedDependency.php 3.06 KB
Newer Older
Qiang Xue committed
1 2
<?php
/**
Qiang Xue committed
3
 * ChainedDependency class file.
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 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
 * ChainedDependency represents a dependency which is composed of a list of other dependencies.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16 17
 * When [[dependOnAll]] is true, if any of the dependencies has changed, this dependency is
 * considered changed; When [[dependOnAll]] is false, if one of the dependencies has NOT changed,
 * this dependency is considered NOT changed.
Qiang Xue committed
18 19 20 21
 *
 * @property boolean $hasChanged Whether the dependency is changed or not.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
22
 * @since 2.0
Qiang Xue committed
23
 */
Qiang Xue committed
24
class ChainedDependency extends Dependency
Qiang Xue committed
25 26
{
	/**
Qiang Xue committed
27 28 29
	 * @var array list of dependencies that this dependency is composed of.
	 * Each array element should be a dependency object or a configuration array
	 * that can be used to create a dependency object via [[\Yii::createObject()]].
Qiang Xue committed
30
	 */
Qiang Xue committed
31 32 33 34 35 36 37 38
	public $dependencies = array();
	/**
	 * @var boolean whether this dependency is depending on every dependency in [[dependencies]].
	 * Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
	 * When it is set false, it means if one of the dependencies has NOT changed, this dependency
	 * is considered NOT changed.
	 */
	public $dependOnAll = true;
Qiang Xue committed
39 40

	/**
Qiang Xue committed
41 42 43 44
	 * Constructor.
	 * @param array $dependencies list of dependencies that this dependency is composed of.
	 * Each array element should be a dependency object or a configuration array
	 * that can be used to create a dependency object via [[\Yii::createObject()]].
Qiang Xue committed
45
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
46
	 */
Qiang Xue committed
47
	public function __construct($dependencies = array(), $config = array())
Qiang Xue committed
48
	{
Qiang Xue committed
49
		$this->dependencies = $dependencies;
Qiang Xue committed
50
		parent::__construct($config);
Qiang Xue committed
51 52 53
	}

	/**
Qiang Xue committed
54
	 * Evaluates the dependency by generating and saving the data related with dependency.
Qiang Xue committed
55
	 */
Qiang Xue committed
56
	public function evaluateDependency()
Qiang Xue committed
57
	{
Qiang Xue committed
58 59 60 61 62
		foreach ($this->dependencies as $dependency) {
			if (!$dependency instanceof Dependency) {
				$dependency = \Yii::createObject($dependency);
			}
			$dependency->evalulateDependency();
Qiang Xue committed
63 64 65 66
		}
	}

	/**
Qiang Xue committed
67 68 69
	 * Generates the data needed to determine if dependency has been changed.
	 * This method does nothing in this class.
	 * @return mixed the data needed to determine if dependency has been changed.
Qiang Xue committed
70
	 */
Qiang Xue committed
71
	protected function generateDependencyData()
Qiang Xue committed
72
	{
Qiang Xue committed
73
		return null;
Qiang Xue committed
74 75 76 77 78 79 80 81 82 83
	}

	/**
	 * Performs the actual dependency checking.
	 * This method returns true if any of the dependency objects
	 * reports a dependency change.
	 * @return boolean whether the dependency is changed or not.
	 */
	public function getHasChanged()
	{
Qiang Xue committed
84 85 86 87 88 89 90 91 92
		foreach ($this->dependencies as $dependency) {
			if (!$dependency instanceof Dependency) {
				$dependency = \Yii::createObject($dependency);
			}
			if ($this->dependOnAll && $dependency->getHasChanged()) {
				return true;
			} elseif (!$this->dependOnAll && !$dependency->getHasChanged()) {
				return false;
			}
Qiang Xue committed
93
		}
Qiang Xue committed
94
		return !$this->dependOnAll;
Qiang Xue committed
95 96
	}
}