CChainedCacheDependency.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * CChainedCacheDependency class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CChainedCacheDependency represents a list of cache dependencies.
  12. *
  13. * If any of the dependencies reports a dependency change, CChainedCacheDependency
  14. * will return true for the checking.
  15. *
  16. * To add dependencies to CChainedCacheDependency, use {@link getDependencies Dependencies}
  17. * which gives a {@link CTypedList} instance and can be used like an array
  18. * (see {@link CList} for more details}).
  19. *
  20. * @property CTypedList $dependencies List of dependency objects.
  21. * @property boolean $hasChanged Whether the dependency is changed or not.
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @package system.caching.dependencies
  25. * @since 1.0
  26. */
  27. class CChainedCacheDependency extends CComponent implements ICacheDependency
  28. {
  29. private $_dependencies=null;
  30. /**
  31. * Constructor.
  32. * @param array $dependencies the dependencies to be added to this chain.
  33. * @since 1.1.4
  34. */
  35. public function __construct($dependencies=array())
  36. {
  37. if(!empty($dependencies))
  38. $this->setDependencies($dependencies);
  39. }
  40. /**
  41. * @return CTypedList list of dependency objects
  42. */
  43. public function getDependencies()
  44. {
  45. if($this->_dependencies===null)
  46. $this->_dependencies=new CTypedList('ICacheDependency');
  47. return $this->_dependencies;
  48. }
  49. /**
  50. * @param array $values list of dependency objects or configurations to be added to this chain.
  51. * If a dependency is specified as a configuration, it must be an array that can be recognized
  52. * by {@link YiiBase::createComponent}.
  53. */
  54. public function setDependencies($values)
  55. {
  56. $dependencies=$this->getDependencies();
  57. foreach($values as $value)
  58. {
  59. if(is_array($value))
  60. $value=Yii::createComponent($value);
  61. $dependencies->add($value);
  62. }
  63. }
  64. /**
  65. * Evaluates the dependency by generating and saving the data related with dependency.
  66. */
  67. public function evaluateDependency()
  68. {
  69. if($this->_dependencies!==null)
  70. {
  71. foreach($this->_dependencies as $dependency)
  72. $dependency->evaluateDependency();
  73. }
  74. }
  75. /**
  76. * Performs the actual dependency checking.
  77. * This method returns true if any of the dependency objects
  78. * reports a dependency change.
  79. * @return boolean whether the dependency is changed or not.
  80. */
  81. public function getHasChanged()
  82. {
  83. if($this->_dependencies!==null)
  84. {
  85. foreach($this->_dependencies as $dependency)
  86. if($dependency->getHasChanged())
  87. return true;
  88. }
  89. return false;
  90. }
  91. }