78 linhas
2.6KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. /**
  9. * ChainedDependency represents a dependency which is composed of a list of other dependencies.
  10. *
  11. * When [[dependOnAll]] is true, if any of the dependencies has changed, this dependency is
  12. * considered changed; When [[dependOnAll]] is false, if one of the dependencies has NOT changed,
  13. * this dependency is considered NOT changed.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class ChainedDependency extends Dependency
  19. {
  20. /**
  21. * @var Dependency[] list of dependencies that this dependency is composed of.
  22. * Each array element must be a dependency object.
  23. */
  24. public $dependencies = [];
  25. /**
  26. * @var boolean whether this dependency is depending on every dependency in [[dependencies]].
  27. * Defaults to true, meaning if any of the dependencies has changed, this dependency is considered changed.
  28. * When it is set false, it means if one of the dependencies has NOT changed, this dependency
  29. * is considered NOT changed.
  30. */
  31. public $dependOnAll = true;
  32. /**
  33. * Evaluates the dependency by generating and saving the data related with dependency.
  34. * @param Cache $cache the cache component that is currently evaluating this dependency
  35. */
  36. public function evaluateDependency($cache)
  37. {
  38. foreach ($this->dependencies as $dependency) {
  39. $dependency->evaluateDependency($cache);
  40. }
  41. }
  42. /**
  43. * Generates the data needed to determine if dependency has been changed.
  44. * This method does nothing in this class.
  45. * @param Cache $cache the cache component that is currently evaluating this dependency
  46. * @return mixed the data needed to determine if dependency has been changed.
  47. */
  48. protected function generateDependencyData($cache)
  49. {
  50. return null;
  51. }
  52. /**
  53. * Performs the actual dependency checking.
  54. * This method returns true if any of the dependency objects
  55. * reports a dependency change.
  56. * @param Cache $cache the cache component that is currently evaluating this dependency
  57. * @return boolean whether the dependency is changed or not.
  58. */
  59. public function getHasChanged($cache)
  60. {
  61. foreach ($this->dependencies as $dependency) {
  62. if ($this->dependOnAll && $dependency->getHasChanged($cache)) {
  63. return true;
  64. } elseif (!$this->dependOnAll && !$dependency->getHasChanged($cache)) {
  65. return false;
  66. }
  67. }
  68. return !$this->dependOnAll;
  69. }
  70. }