api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TempFileCache.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Cache;
  3. class TempFileCache implements Cache
  4. {
  5. public const SUFFIX = 'cache';
  6. private $path;
  7. private $segments;
  8. public function __construct(string $prefix, string $config)
  9. {
  10. $this->segments = [];
  11. $s = DIRECTORY_SEPARATOR;
  12. $ps = PATH_SEPARATOR;
  13. if ($config == '') {
  14. $this->path = sys_get_temp_dir() . $s . $prefix . self::SUFFIX;
  15. } elseif (strpos($config, $ps) === false) {
  16. $this->path = $config;
  17. } else {
  18. list($path, $segments) = explode($ps, $config);
  19. $this->path = $path;
  20. $this->segments = explode(',', $segments);
  21. }
  22. if (file_exists($this->path) && is_dir($this->path)) {
  23. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), false);
  24. }
  25. }
  26. private function getFileName(string $key): string
  27. {
  28. $s = DIRECTORY_SEPARATOR;
  29. $md5 = md5($key);
  30. $filename = rtrim($this->path, $s) . $s;
  31. $i = 0;
  32. foreach ($this->segments as $segment) {
  33. $filename .= substr($md5, $i, $segment) . $s;
  34. $i += $segment;
  35. }
  36. $filename .= substr($md5, $i);
  37. return $filename;
  38. }
  39. public function set(string $key, string $value, int $ttl = 0): bool
  40. {
  41. $filename = $this->getFileName($key);
  42. $dirname = dirname($filename);
  43. if (!file_exists($dirname)) {
  44. if (!mkdir($dirname, 0755, true)) {
  45. return false;
  46. }
  47. }
  48. $string = $ttl . '|' . $value;
  49. return $this->filePutContents($filename, $string) !== false;
  50. }
  51. private function filePutContents($filename, $string)
  52. {
  53. return file_put_contents($filename, $string, LOCK_EX);
  54. }
  55. private function fileGetContents($filename)
  56. {
  57. $file = fopen($filename, 'rb');
  58. if ($file === false) {
  59. return false;
  60. }
  61. $lock = flock($file, LOCK_SH);
  62. if (!$lock) {
  63. fclose($file);
  64. return false;
  65. }
  66. $string = '';
  67. while (!feof($file)) {
  68. $string .= fread($file, 8192);
  69. }
  70. flock($file, LOCK_UN);
  71. fclose($file);
  72. return $string;
  73. }
  74. private function getString($filename): string
  75. {
  76. $data = $this->fileGetContents($filename);
  77. if ($data === false) {
  78. return '';
  79. }
  80. list($ttl, $string) = explode('|', $data, 2);
  81. if ($ttl > 0 && time() - filemtime($filename) > $ttl) {
  82. return '';
  83. }
  84. return $string;
  85. }
  86. public function get(string $key): string
  87. {
  88. $filename = $this->getFileName($key);
  89. if (!file_exists($filename)) {
  90. return '';
  91. }
  92. $string = $this->getString($filename);
  93. if ($string == null) {
  94. return '';
  95. }
  96. return $string;
  97. }
  98. private function clean(string $path, array $segments, int $len, bool $all) /*: void*/
  99. {
  100. $entries = scandir($path);
  101. foreach ($entries as $entry) {
  102. if ($entry === '.' || $entry === '..') {
  103. continue;
  104. }
  105. $filename = $path . DIRECTORY_SEPARATOR . $entry;
  106. if (count($segments) == 0) {
  107. if (strlen($entry) != $len) {
  108. continue;
  109. }
  110. if (is_file($filename)) {
  111. if ($all || $this->getString($filename) == null) {
  112. unlink($filename);
  113. }
  114. }
  115. } else {
  116. if (strlen($entry) != $segments[0]) {
  117. continue;
  118. }
  119. if (is_dir($filename)) {
  120. $this->clean($filename, array_slice($segments, 1), $len - $segments[0], $all);
  121. rmdir($filename);
  122. }
  123. }
  124. }
  125. }
  126. public function clear(): bool
  127. {
  128. if (!file_exists($this->path) || !is_dir($this->path)) {
  129. return false;
  130. }
  131. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), true);
  132. return true;
  133. }
  134. }