FileCache.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Behat Gherkin.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Gherkin\Cache;
  10. use Behat\Gherkin\Exception\CacheException;
  11. use Behat\Gherkin\Node\FeatureNode;
  12. use Behat\Gherkin\Gherkin;
  13. /**
  14. * File cache.
  15. * Caches feature into a file.
  16. *
  17. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  18. */
  19. class FileCache implements CacheInterface
  20. {
  21. private $path;
  22. /**
  23. * Initializes file cache.
  24. *
  25. * @param string $path Path to the folder where to store caches.
  26. *
  27. * @throws CacheException
  28. */
  29. public function __construct($path)
  30. {
  31. $this->path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'v'.Gherkin::VERSION;
  32. if (!is_dir($this->path)) {
  33. @mkdir($this->path, 0777, true);
  34. }
  35. if (!is_writeable($this->path)) {
  36. throw new CacheException(sprintf('Cache path "%s" is not writeable. Check your filesystem permissions or disable Gherkin file cache.', $this->path));
  37. }
  38. }
  39. /**
  40. * Checks that cache for feature exists and is fresh.
  41. *
  42. * @param string $path Feature path
  43. * @param integer $timestamp The last time feature was updated
  44. *
  45. * @return bool
  46. */
  47. public function isFresh($path, $timestamp)
  48. {
  49. $cachePath = $this->getCachePathFor($path);
  50. if (!file_exists($cachePath)) {
  51. return false;
  52. }
  53. return filemtime($cachePath) > $timestamp;
  54. }
  55. /**
  56. * Reads feature cache from path.
  57. *
  58. * @param string $path Feature path
  59. *
  60. * @return FeatureNode
  61. *
  62. * @throws CacheException
  63. */
  64. public function read($path)
  65. {
  66. $cachePath = $this->getCachePathFor($path);
  67. $feature = unserialize(file_get_contents($cachePath));
  68. if (!$feature instanceof FeatureNode) {
  69. throw new CacheException(sprintf('Can not load cache for a feature "%s" from "%s".', $path, $cachePath ));
  70. }
  71. return $feature;
  72. }
  73. /**
  74. * Caches feature node.
  75. *
  76. * @param string $path Feature path
  77. * @param FeatureNode $feature Feature instance
  78. */
  79. public function write($path, FeatureNode $feature)
  80. {
  81. file_put_contents($this->getCachePathFor($path), serialize($feature));
  82. }
  83. /**
  84. * Returns feature cache file path from features path.
  85. *
  86. * @param string $path Feature path
  87. *
  88. * @return string
  89. */
  90. protected function getCachePathFor($path)
  91. {
  92. return $this->path.'/'.md5($path).'.feature.cache';
  93. }
  94. }