66 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
<?php
 | 
						|
namespace app\service\driver;
 | 
						|
 | 
						|
 | 
						|
 | 
						|
use think\App;
 | 
						|
use think\session\driver\File;
 | 
						|
 | 
						|
/**
 | 
						|
 * 自定义session file 驱动器
 | 
						|
 *
 | 
						|
 * 用于修复TP 自带file驱动器bug
 | 
						|
 *
 | 
						|
 * Class sessionFile
 | 
						|
 * @package app\service\driver
 | 
						|
 */
 | 
						|
class SessionFile extends File
 | 
						|
{
 | 
						|
 | 
						|
    public function __construct(App $app, array $config = [])
 | 
						|
    {
 | 
						|
        parent::__construct($app, $config);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * bug v20220302 error: SplFileInfo::getMTime(): stat failed for filePath(xxx)
 | 
						|
     * 修复此函数
 | 
						|
     *
 | 
						|
     * Session 垃圾回收
 | 
						|
     * @access public
 | 
						|
     * @return void
 | 
						|
     */
 | 
						|
    public function gc(): void
 | 
						|
    {
 | 
						|
        $lifetime = $this->config['expire'];
 | 
						|
        $now      = time();
 | 
						|
 | 
						|
        $files = $this->findFiles($this->config['path'], function (\SplFileInfo $item) use ($lifetime, $now) {
 | 
						|
            // 添加文件是否存在判断
 | 
						|
            if (!$item->isFile()) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
 | 
						|
            return $now - $lifetime > $item->getMTime();
 | 
						|
        });
 | 
						|
 | 
						|
        foreach ($files as $file) {
 | 
						|
            $this->unlink($file->getPathname());
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 重构unlink方法
 | 
						|
     *
 | 
						|
     * 判断文件是否存在后,删除
 | 
						|
     *
 | 
						|
     * @access private
 | 
						|
     * @param string $file
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    protected function unlink(string $file): bool
 | 
						|
    {
 | 
						|
        return is_file($file) && unlink($file);
 | 
						|
    }
 | 
						|
} |