ConfigCache.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Cache;
  11. /**
  12. * A config cache stores values using var_export() and include.
  13. *
  14. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  15. */
  16. class ConfigCache
  17. {
  18. private $dir;
  19. /**
  20. * Construct.
  21. *
  22. * @param string $dir The cache directory
  23. */
  24. public function __construct($dir)
  25. {
  26. $this->dir = $dir;
  27. }
  28. /**
  29. * Checks of the cache has a file.
  30. *
  31. * @param string $resource A cache key
  32. *
  33. * @return Boolean True if a file exists
  34. */
  35. public function has($resource)
  36. {
  37. return file_exists($this->getSourcePath($resource));
  38. }
  39. /**
  40. * Writes a value to a file.
  41. *
  42. * @param string $resource A cache key
  43. * @param mixed $value A value to cache
  44. */
  45. public function set($resource, $value)
  46. {
  47. $path = $this->getSourcePath($resource);
  48. if (!is_dir($dir = dirname($path)) && false === @mkdir($dir, 0777, true)) {
  49. // @codeCoverageIgnoreStart
  50. throw new \RuntimeException('Unable to create directory '.$dir);
  51. // @codeCoverageIgnoreEnd
  52. }
  53. if (false === @file_put_contents($path, sprintf("<?php\n\n// $resource\nreturn %s;\n", var_export($value, true)))) {
  54. // @codeCoverageIgnoreStart
  55. throw new \RuntimeException('Unable to write file '.$path);
  56. // @codeCoverageIgnoreEnd
  57. }
  58. }
  59. /**
  60. * Loads and returns the value for the supplied cache key.
  61. *
  62. * @param string $resource A cache key
  63. *
  64. * @return mixed The cached value
  65. */
  66. public function get($resource)
  67. {
  68. $path = $this->getSourcePath($resource);
  69. if (!file_exists($path)) {
  70. throw new \RuntimeException('There is no cached value for '.$resource);
  71. }
  72. return include $path;
  73. }
  74. /**
  75. * Returns a timestamp for when the cache was created.
  76. *
  77. * @param string $resource A cache key
  78. *
  79. * @return integer A UNIX timestamp
  80. */
  81. public function getTimestamp($resource)
  82. {
  83. $path = $this->getSourcePath($resource);
  84. if (!file_exists($path)) {
  85. throw new \RuntimeException('There is no cached value for '.$resource);
  86. }
  87. if (false === $mtime = @filemtime($path)) {
  88. // @codeCoverageIgnoreStart
  89. throw new \RuntimeException('Unable to determine file mtime for '.$path);
  90. // @codeCoverageIgnoreEnd
  91. }
  92. return $mtime;
  93. }
  94. /**
  95. * Returns the path where the file corresponding to the supplied cache key can be included from.
  96. *
  97. * @param string $resource A cache key
  98. *
  99. * @return string A file path
  100. */
  101. private function getSourcePath($resource)
  102. {
  103. $key = md5($resource);
  104. return $this->dir.'/'.$key[0].'/'.$key.'.php';
  105. }
  106. }