CDirectoryCacheDependency.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CDirectoryCacheDependency 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. * CDirectoryCacheDependency represents a dependency based on change of a directory.
  12. *
  13. * CDirectoryCacheDependency performs dependency checking based on the
  14. * modification time of the files contained in the specified directory.
  15. * The directory being checked is specified via {@link directory}.
  16. *
  17. * By default, all files under the specified directory and subdirectories
  18. * will be checked. If the last modification time of any of them is changed
  19. * or if different number of files are contained in a directory, the dependency
  20. * is reported as changed. By specifying {@link recursiveLevel},
  21. * one can limit the checking to a certain depth of the directory.
  22. *
  23. * Note, dependency checking for a directory is expensive because it involves
  24. * accessing modification time of multiple files under the directory.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @package system.caching.dependencies
  28. * @since 1.0
  29. */
  30. class CDirectoryCacheDependency extends CCacheDependency
  31. {
  32. /**
  33. * @var string the directory whose change is used to determine if the dependency has been changed.
  34. * If any of the files under the directory is changed, the dependency is considered as changed.
  35. */
  36. public $directory;
  37. /**
  38. * @var integer the depth of the subdirectories to be recursively checked.
  39. * If the value is less than 0, it means unlimited depth.
  40. * If the value is 0, it means checking the files directly under the specified directory.
  41. */
  42. public $recursiveLevel=-1;
  43. /**
  44. * @var string the regular expression matching valid file/directory names.
  45. * Only the matching files or directories will be checked for changes.
  46. * Defaults to null, meaning all files/directories will qualify.
  47. */
  48. public $namePattern;
  49. /**
  50. * Constructor.
  51. * @param string $directory the directory to be checked
  52. */
  53. public function __construct($directory=null)
  54. {
  55. $this->directory=$directory;
  56. }
  57. /**
  58. * Generates the data needed to determine if dependency has been changed.
  59. * This method returns the modification timestamps for files under the directory.
  60. * @throws CException if {@link directory} is empty
  61. * @return mixed the data needed to determine if dependency has been changed.
  62. */
  63. protected function generateDependentData()
  64. {
  65. if($this->directory!==null)
  66. return $this->generateTimestamps($this->directory);
  67. else
  68. throw new CException(Yii::t('yii','CDirectoryCacheDependency.directory cannot be empty.'));
  69. }
  70. /**
  71. * Determines the last modification time for files under the directory.
  72. * This method may go recursively into subdirectories if {@link recursiveLevel} is not 0.
  73. * @param string $directory the directory name
  74. * @param integer $level level of the recursion
  75. * @throws CException if given directory is not valid
  76. * @return array list of file modification time indexed by the file path
  77. */
  78. protected function generateTimestamps($directory,$level=0)
  79. {
  80. if(($dir=@opendir($directory))===false)
  81. throw new CException(Yii::t('yii','"{path}" is not a valid directory.',
  82. array('{path}'=>$directory)));
  83. $timestamps=array();
  84. while(($file=readdir($dir))!==false)
  85. {
  86. $path=$directory.DIRECTORY_SEPARATOR.$file;
  87. if($file==='.' || $file==='..')
  88. continue;
  89. if($this->namePattern!==null && !preg_match($this->namePattern,$file))
  90. continue;
  91. if(is_file($path))
  92. {
  93. if($this->validateFile($path))
  94. $timestamps[$path]=filemtime($path);
  95. }
  96. else
  97. {
  98. if(($this->recursiveLevel<0 || $level<$this->recursiveLevel) && $this->validateDirectory($path))
  99. $timestamps=array_merge($timestamps, $this->generateTimestamps($path,$level+1));
  100. }
  101. }
  102. closedir($dir);
  103. return $timestamps;
  104. }
  105. /**
  106. * Checks to see if the file should be checked for dependency.
  107. * This method is invoked when dependency of the whole directory is being checked.
  108. * By default, it always returns true, meaning the file should be checked.
  109. * You may override this method to check only certain files.
  110. * @param string $fileName the name of the file that may be checked for dependency.
  111. * @return boolean whether this file should be checked.
  112. */
  113. protected function validateFile($fileName)
  114. {
  115. return true;
  116. }
  117. /**
  118. * Checks to see if the specified subdirectory should be checked for dependency.
  119. * This method is invoked when dependency of the whole directory is being checked.
  120. * By default, it always returns true, meaning the subdirectory should be checked.
  121. * You may override this method to check only certain subdirectories.
  122. * @param string $directory the name of the subdirectory that may be checked for dependency.
  123. * @return boolean whether this subdirectory should be checked.
  124. */
  125. protected function validateDirectory($directory)
  126. {
  127. return true;
  128. }
  129. }