/crmeb/vendor/topthink/framework/src/think/session/driver/File.php

https://github.com/crmeb/CRMEB · PHP · 249 lines · 136 code · 35 blank · 78 comment · 21 complexity · 5562c7748b07f4286a6838d1cb0d9793 MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\session\driver;
  13. use Closure;
  14. use Exception;
  15. use FilesystemIterator;
  16. use Generator;
  17. use SplFileInfo;
  18. use think\App;
  19. use think\contract\SessionHandlerInterface;
  20. /**
  21. * Session 文件驱动
  22. */
  23. class File implements SessionHandlerInterface
  24. {
  25. protected $config = [
  26. 'path' => '',
  27. 'expire' => 1440,
  28. 'prefix' => '',
  29. 'data_compress' => false,
  30. 'gc_probability' => 1,
  31. 'gc_divisor' => 100,
  32. ];
  33. public function __construct(App $app, array $config = [])
  34. {
  35. $this->config = array_merge($this->config, $config);
  36. if (empty($this->config['path'])) {
  37. $this->config['path'] = $app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'session' . DIRECTORY_SEPARATOR;
  38. } elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
  39. $this->config['path'] .= DIRECTORY_SEPARATOR;
  40. }
  41. $this->init();
  42. }
  43. /**
  44. * 打开Session
  45. * @access protected
  46. * @throws Exception
  47. */
  48. protected function init(): void
  49. {
  50. try {
  51. !is_dir($this->config['path']) && mkdir($this->config['path'], 0755, true);
  52. } catch (\Exception $e) {
  53. // 写入失败
  54. }
  55. // 垃圾回收
  56. if (random_int(1, $this->config['gc_divisor']) <= $this->config['gc_probability']) {
  57. $this->gc();
  58. }
  59. }
  60. /**
  61. * Session 垃圾回收
  62. * @access public
  63. * @return void
  64. */
  65. public function gc(): void
  66. {
  67. $lifetime = $this->config['expire'];
  68. $now = time();
  69. $files = $this->findFiles($this->config['path'], function (SplFileInfo $item) use ($lifetime, $now) {
  70. return $now - $lifetime > $item->getMTime();
  71. });
  72. foreach ($files as $file) {
  73. $this->unlink($file->getPathname());
  74. }
  75. }
  76. /**
  77. * 查找文件
  78. * @param string $root
  79. * @param Closure $filter
  80. * @return Generator
  81. */
  82. protected function findFiles(string $root, Closure $filter)
  83. {
  84. $items = new FilesystemIterator($root);
  85. /** @var SplFileInfo $item */
  86. foreach ($items as $item) {
  87. if ($item->isDir() && !$item->isLink()) {
  88. yield from $this->findFiles($item->getPathname(), $filter);
  89. } else {
  90. if ($filter($item)) {
  91. yield $item;
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * 取得变量的存储文件名
  98. * @access protected
  99. * @param string $name 缓存变量名
  100. * @param bool $auto 是否自动创建目录
  101. * @return string
  102. */
  103. protected function getFileName(string $name, bool $auto = false): string
  104. {
  105. if ($this->config['prefix']) {
  106. // 使用子目录
  107. $name = $this->config['prefix'] . DIRECTORY_SEPARATOR . 'sess_' . $name;
  108. } else {
  109. $name = 'sess_' . $name;
  110. }
  111. $filename = $this->config['path'] . $name;
  112. $dir = dirname($filename);
  113. if ($auto && !is_dir($dir)) {
  114. try {
  115. mkdir($dir, 0755, true);
  116. } catch (\Exception $e) {
  117. // 创建失败
  118. }
  119. }
  120. return $filename;
  121. }
  122. /**
  123. * 读取Session
  124. * @access public
  125. * @param string $sessID
  126. * @return string
  127. */
  128. public function read(string $sessID): string
  129. {
  130. $filename = $this->getFileName($sessID);
  131. if (is_file($filename) && filemtime($filename) >= time() - $this->config['expire']) {
  132. $content = $this->readFile($filename);
  133. if ($this->config['data_compress'] && function_exists('gzcompress')) {
  134. //启用数据压缩
  135. $content = gzuncompress($content);
  136. }
  137. return $content;
  138. }
  139. return '';
  140. }
  141. /**
  142. * 写文件(加锁)
  143. * @param $path
  144. * @param $content
  145. * @return bool
  146. */
  147. protected function writeFile($path, $content): bool
  148. {
  149. return (bool) file_put_contents($path, $content, LOCK_EX);
  150. }
  151. /**
  152. * 读取文件内容(加锁)
  153. * @param $path
  154. * @return string
  155. */
  156. protected function readFile($path): string
  157. {
  158. $contents = '';
  159. $handle = fopen($path, 'rb');
  160. if ($handle) {
  161. try {
  162. if (flock($handle, LOCK_SH)) {
  163. clearstatcache(true, $path);
  164. $contents = fread($handle, filesize($path) ?: 1);
  165. flock($handle, LOCK_UN);
  166. }
  167. } finally {
  168. fclose($handle);
  169. }
  170. }
  171. return $contents;
  172. }
  173. /**
  174. * 写入Session
  175. * @access public
  176. * @param string $sessID
  177. * @param string $sessData
  178. * @return bool
  179. */
  180. public function write(string $sessID, string $sessData): bool
  181. {
  182. $filename = $this->getFileName($sessID, true);
  183. $data = $sessData;
  184. if ($this->config['data_compress'] && function_exists('gzcompress')) {
  185. //数据压缩
  186. $data = gzcompress($data, 3);
  187. }
  188. return $this->writeFile($filename, $data);
  189. }
  190. /**
  191. * 删除Session
  192. * @access public
  193. * @param string $sessID
  194. * @return bool
  195. */
  196. public function delete(string $sessID): bool
  197. {
  198. try {
  199. return $this->unlink($this->getFileName($sessID));
  200. } catch (\Exception $e) {
  201. return false;
  202. }
  203. }
  204. /**
  205. * 判断文件是否存在后,删除
  206. * @access private
  207. * @param string $file
  208. * @return bool
  209. */
  210. private function unlink(string $file): bool
  211. {
  212. return is_file($file) && unlink($file);
  213. }
  214. }