FileCache.php 4.3 KB

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