123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
-
- namespace Illuminate\Session;
-
- use SessionHandlerInterface;
- use Illuminate\Support\Carbon;
- use Symfony\Component\Finder\Finder;
- use Illuminate\Filesystem\Filesystem;
-
- class FileSessionHandler implements SessionHandlerInterface
- {
- /**
- * The filesystem instance.
- *
- * @var \Illuminate\Filesystem\Filesystem
- */
- protected $files;
-
- /**
- * The path where sessions should be stored.
- *
- * @var string
- */
- protected $path;
-
- /**
- * The number of minutes the session should be valid.
- *
- * @var int
- */
- protected $minutes;
-
- /**
- * Create a new file driven handler instance.
- *
- * @param \Illuminate\Filesystem\Filesystem $files
- * @param string $path
- * @param int $minutes
- * @return void
- */
- public function __construct(Filesystem $files, $path, $minutes)
- {
- $this->path = $path;
- $this->files = $files;
- $this->minutes = $minutes;
- }
-
- /**
- * {@inheritdoc}
- */
- public function open($savePath, $sessionName)
- {
- return true;
- }
-
- /**
- * {@inheritdoc}
- */
- public function close()
- {
- return true;
- }
-
- /**
- * {@inheritdoc}
- */
- public function read($sessionId)
- {
- if ($this->files->exists($path = $this->path.'/'.$sessionId)) {
- if (filemtime($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
- return $this->files->get($path, true);
- }
- }
-
- return '';
- }
-
- /**
- * {@inheritdoc}
- */
- public function write($sessionId, $data)
- {
- $this->files->put($this->path.'/'.$sessionId, $data, true);
-
- return true;
- }
-
- /**
- * {@inheritdoc}
- */
- public function destroy($sessionId)
- {
- $this->files->delete($this->path.'/'.$sessionId);
-
- return true;
- }
-
- /**
- * {@inheritdoc}
- */
- public function gc($lifetime)
- {
- $files = Finder::create()
- ->in($this->path)
- ->files()
- ->ignoreDotFiles(true)
- ->date('<= now - '.$lifetime.' seconds');
-
- foreach ($files as $file) {
- $this->files->delete($file->getRealPath());
- }
- }
- }
|