FileCache.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Cache;
  9. use JonnyW\PhantomJs\StringUtils;
  10. use JonnyW\PhantomJs\Exception\NotWritableException;
  11. use JonnyW\PhantomJs\Exception\NotExistsException;
  12. /**
  13. * PHP PhantomJs
  14. *
  15. * @author Jon Wenmoth <contact@jonnyw.me>
  16. */
  17. class FileCache implements CacheInterface
  18. {
  19. /**
  20. * Default write directory
  21. *
  22. * @var string
  23. * @access protected
  24. */
  25. protected $directory;
  26. /**
  27. * Default write extension
  28. *
  29. * @var string
  30. * @access protected
  31. */
  32. protected $extension;
  33. /**
  34. * Internal constructor.
  35. *
  36. * @access public
  37. * @param string $directory
  38. * @param string $extension
  39. */
  40. public function __construct($directory, $extension)
  41. {
  42. $this->directory = rtrim($directory, DIRECTORY_SEPARATOR);
  43. $this->extension = $extension;
  44. }
  45. /**
  46. * Write data to storage.
  47. *
  48. * @access public
  49. * @param string $id
  50. * @param string $data
  51. * @return string
  52. * @throws \JonnyW\PhantomJs\Exception\NotWritableException
  53. */
  54. public function save($id, $data)
  55. {
  56. $file = $this->getFilename($id);
  57. if (!$this->isWritable($file)) {
  58. throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $file));
  59. }
  60. if ($this->writeData($file, $data) === false) {
  61. $this->delete($file);
  62. throw new NotWritableException(sprintf('Data could not be written to file on system. Please make sure that file is writeable: %s', $file));
  63. }
  64. return $file;
  65. }
  66. /**
  67. * Fetch data from file.
  68. *
  69. * @access public
  70. * @param string $id
  71. * @return mixed|void
  72. * @throws \JonnyW\PhantomJs\Exception\NotExistsException
  73. */
  74. public function fetch($id)
  75. {
  76. $file = $this->getFilename($id);
  77. if (!$this->exists($id)) {
  78. throw new NotExistsException(sprintf('Could not fetch data from file as file does not exist: %s', $file));
  79. }
  80. return $this->readData($file);
  81. }
  82. /**
  83. * Delete data from storage.
  84. *
  85. * @access public
  86. * @param string $id
  87. * @return void
  88. */
  89. public function delete($id)
  90. {
  91. $files = glob($this->getFilename($id));
  92. if (count($files)) {
  93. array_map('unlink', $files);
  94. }
  95. }
  96. /**
  97. * Data exists in storage.
  98. *
  99. * @access public
  100. * @param string $id
  101. * @return boolean
  102. */
  103. public function exists($id)
  104. {
  105. return (bool) (file_exists($this->getFilename($id)));
  106. }
  107. /**
  108. * Is data writeable.
  109. *
  110. * @access protected
  111. * @param $file
  112. * @return boolean
  113. */
  114. protected function isWritable($file)
  115. {
  116. return (bool) ((file_exists($file) && is_writable($file)) || (!file_exists($file) && is_writable(dirname($file))));
  117. }
  118. /**
  119. * Write data to file.
  120. *
  121. * @access protected
  122. * @param string $file
  123. * @param string $data
  124. * @return boolean
  125. */
  126. protected function writeData($file, $data)
  127. {
  128. return file_put_contents($file, $data);
  129. }
  130. /**
  131. * Read data from file.
  132. *
  133. * @access protected
  134. * @param string $file
  135. * @return mixed
  136. */
  137. protected function readData($file)
  138. {
  139. return file_get_contents($file);
  140. }
  141. /**
  142. * Get filename
  143. *
  144. * @access protected
  145. * @param string $id
  146. * @return string
  147. * @throws \JonnyW\PhantomJs\Exception\NotWritableException
  148. */
  149. protected function getFileName($id)
  150. {
  151. if (is_dir($id)) {
  152. return sprintf('%1$s/%2$s.%3$s', rtrim($id, DIRECTORY_SEPARATOR), StringUtils::random(20), $this->extension);
  153. }
  154. $dirName = dirname($id);
  155. if (!file_exists($id) && $dirName === '.') {
  156. return sprintf('%1$s/%2$s', $this->directory, $id);
  157. }
  158. if (!file_exists($id) && !is_writable($dirName)) {
  159. throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $id));
  160. }
  161. return $id;
  162. }
  163. }