FileDependency.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. use Yii;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * FileDependency represents a dependency based on a file's last modification time.
  12. *
  13. * If the last modification time of the file specified via [[fileName]] is changed,
  14. * the dependency is considered as changed.
  15. *
  16. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class FileDependency extends Dependency
  22. {
  23. /**
  24. * @var string the file path or [path alias](guide:concept-aliases) whose last modification time is used to
  25. * check if the dependency has been changed.
  26. */
  27. public $fileName;
  28. /**
  29. * Generates the data needed to determine if dependency has been changed.
  30. * This method returns the file's last modification time.
  31. * @param CacheInterface $cache the cache component that is currently evaluating this dependency
  32. * @return mixed the data needed to determine if dependency has been changed.
  33. * @throws InvalidConfigException if [[fileName]] is not set
  34. */
  35. protected function generateDependencyData($cache)
  36. {
  37. if ($this->fileName === null) {
  38. throw new InvalidConfigException('FileDependency::fileName must be set');
  39. }
  40. $fileName = Yii::getAlias($this->fileName);
  41. clearstatcache(false, $fileName);
  42. return @filemtime($fileName);
  43. }
  44. }