ChainedDependency.php 2.42 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
 * ChainedDependency represents a dependency which is composed of a list of other dependencies.
Qiang Xue committed
12
 *
Qiang Xue committed
13 14 15
 * 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
16 17
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
18
 * @since 2.0
Qiang Xue committed
19
 */
Qiang Xue committed
20
class ChainedDependency extends Dependency
Qiang Xue committed
21 22
{
	/**
23 24
	 * @var Dependency[] list of dependencies that this dependency is composed of.
	 * Each array element must be a dependency object.
Qiang Xue committed
25
	 */
26
	public $dependencies = [];
Qiang Xue committed
27 28 29 30 31 32 33
	/**
	 * @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
34 35

	/**
Qiang Xue committed
36
	 * Evaluates the dependency by generating and saving the data related with dependency.
37
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
38
	 */
39
	public function evaluateDependency($cache)
Qiang Xue committed
40
	{
Qiang Xue committed
41
		foreach ($this->dependencies as $dependency) {
42
			$dependency->evaluateDependency($cache);
Qiang Xue committed
43 44 45 46
		}
	}

	/**
Qiang Xue committed
47 48
	 * Generates the data needed to determine if dependency has been changed.
	 * This method does nothing in this class.
49
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
50
	 * @return mixed the data needed to determine if dependency has been changed.
Qiang Xue committed
51
	 */
52
	protected function generateDependencyData($cache)
Qiang Xue committed
53
	{
Qiang Xue committed
54
		return null;
Qiang Xue committed
55 56 57 58 59 60
	}

	/**
	 * Performs the actual dependency checking.
	 * This method returns true if any of the dependency objects
	 * reports a dependency change.
61
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
62 63
	 * @return boolean whether the dependency is changed or not.
	 */
64
	public function getHasChanged($cache)
Qiang Xue committed
65
	{
66
		foreach ($this->dependencies as $dependency) {
67
			if ($this->dependOnAll && $dependency->getHasChanged($cache)) {
Qiang Xue committed
68
				return true;
69
			} elseif (!$this->dependOnAll && !$dependency->getHasChanged($cache)) {
Qiang Xue committed
70 71
				return false;
			}
Qiang Xue committed
72
		}
Qiang Xue committed
73
		return !$this->dependOnAll;
Qiang Xue committed
74 75
	}
}