FilesystemCache.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Moinax\TvDb\Http\Cache;
  3. class FilesystemCache implements Cache
  4. {
  5. private $directory;
  6. public function __construct($directory)
  7. {
  8. $this->directory = $directory;
  9. }
  10. /**
  11. *
  12. * @see \Moinax\TvDb\Http\Cache\Cache::getDate()
  13. */
  14. public function getDate($resource)
  15. {
  16. $path = $this->getPath($resource);
  17. if(!file_exists($path)) {
  18. return -1;
  19. }
  20. return filemtime($path);
  21. }
  22. /**
  23. *
  24. * @see \Moinax\TvDb\Http\Cache\Cache::setDate()
  25. */
  26. public function setDate($resource, $date)
  27. {
  28. $path = $this->getPath($resource);
  29. if(! file_exists($path)) {
  30. return;
  31. }
  32. touch($path, $date);
  33. }
  34. /**
  35. *
  36. * @see \Moinax\TvDb\Http\Cache\Cache::cache()
  37. */
  38. public function cache($resource, $date, $content)
  39. {
  40. $path = $this->getPath($resource);
  41. $dirname = dirname($path);
  42. if(!is_dir($dirname)) {
  43. mkdir($dirname, 0777, true);
  44. }
  45. file_put_contents($path, $content);
  46. $this->setDate($resource, $date);
  47. }
  48. /**
  49. *
  50. * @see \Moinax\TvDb\Http\Cache\Cache::getContent()
  51. */
  52. public function getContent($resource)
  53. {
  54. $path = $this->getPath($resource);
  55. if(! file_exists($path)) {
  56. return '';
  57. }
  58. return file_get_contents($path);
  59. }
  60. private function getPath($resource) {
  61. return $this->directory . $resource;
  62. }
  63. }