<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\session\driver;
use Closure;
use Exception;
use FilesystemIterator;
use Generator;
use SplFileInfo;
use think\App;
use think\contract\SessionHandlerInterface;
class File implements SessionHandlerInterface
{
protected $config = [
'path' => '',
'expire' => 1440,
'prefix' => '',
'data_compress' => false,
'gc_probability' => 1,
'gc_divisor' => 100,
];
public function __construct(App $app, array $config = [])
{
$this->config = array_merge($this->config, $config);
if (empty($this->config['path'])) {
$this->config['path'] = $app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'session' . DIRECTORY_SEPARATOR;
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
$this->config['path'] .= DIRECTORY_SEPARATOR;
}
$this->init();
}
protected function init(): void
{
try {
!is_dir($this->config['path']) && mkdir($this->config['path'], 0755, true);
} catch (\Exception $e) {
}
if (random_int(1, $this->config['gc_divisor']) <= $this->config['gc_probability']) {
$this->gc();
}
}
public function gc(): void
{
$lifetime = $this->config['expire'];
$now = time();
$files = $this->findFiles($this->config['path'], function (SplFileInfo $item) use ($lifetime, $now) {
return $now - $lifetime > $item->getMTime();
});
foreach ($files as $file) {
$this->unlink($file->getPathname());
}
}
protected function findFiles(string $root, Closure $filter)
{
$items = new FilesystemIterator($root);
foreach ($items as $item) {
if ($item->isDir() && !$item->isLink()) {
yield from $this->findFiles($item->getPathname(), $filter);
} else {
if ($filter($item)) {
yield $item;
}
}
}
}
protected function getFileName(string $name, bool $auto = false): string
{
if ($this->config['prefix']) {
$name = $this->config['prefix'] . DIRECTORY_SEPARATOR . 'sess_' . $name;
} else {
$name = 'sess_' . $name;
}
$filename = $this->config['path'] . $name;
$dir = dirname($filename);
if ($auto && !is_dir($dir)) {
try {
mkdir($dir, 0755, true);
} catch (\Exception $e) {
}
}
return $filename;
}
public function read(string $sessID): string
{
$filename = $this->getFileName($sessID);
if (is_file($filename) && filemtime($filename) >= time() - $this->config['expire']) {
$content = $this->readFile($filename);
if ($this->config['data_compress'] && function_exists('gzcompress')) {
$content = (string) gzuncompress($content);
}
return $content;
}
return '';
}
protected function writeFile($path, $content): bool
{
return (bool) file_put_contents($path, $content, LOCK_EX);
}
protected function readFile($path): string
{
$contents = '';
$handle = fopen($path, 'rb');
if ($handle) {
try {
if (flock($handle, LOCK_SH)) {
clearstatcache(true, $path);
$contents = fread($handle, filesize($path) ?: 1);
flock($handle, LOCK_UN);
}
} finally {
fclose($handle);
}
}
return $contents;
}
public function write(string $sessID, string $sessData): bool
{
$filename = $this->getFileName($sessID, true);
$data = $sessData;
if ($this->config['data_compress'] && function_exists('gzcompress')) {
$data = gzcompress($data, 3);
}
return $this->writeFile($filename, $data);
}
public function delete(string $sessID): bool
{
try {
return $this->unlink($this->getFileName($sessID));
} catch (\Exception $e) {
return false;
}
}
private function unlink(string $file): bool
{
return is_file($file) && unlink($file);
}
}