-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
227e606
commit bd579d4
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* FileAdapter.php | ||
* | ||
* This file is part of InitPHP Sessions. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 Muhammet ŞAFAK | ||
* @license ./LICENSE MIT | ||
* @version 2.0 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
namespace InitPHP\Sessions\Adapters; | ||
|
||
use InitPHP\Sessions\AbstractAdapter; | ||
use InitPHP\Sessions\Interfaces\AdapterInterface; | ||
|
||
class FileAdapter extends AbstractAdapter implements AdapterInterface | ||
{ | ||
|
||
private $path; | ||
|
||
private string $prefix = 'sess_'; | ||
|
||
public function __construct(array $options) | ||
{ | ||
$path = $options['path']; | ||
if (!is_dir($path) || !is_writable($path)) { | ||
throw new \InvalidArgumentException("Belirtilen dizin geçerli ve yazılabilir olmalıdır."); | ||
} | ||
$this->path = $path; | ||
isset($options['prefix']) && $this->prefix = $options['prefix']; | ||
} | ||
|
||
|
||
public function read($id) | ||
{ | ||
$id = $this->prefix . $id; | ||
|
||
return (string) @file_get_contents("{$this->path}/$id"); | ||
} | ||
|
||
public function write($id, $data) | ||
{ | ||
$id = $this->prefix . $id; | ||
|
||
return file_put_contents("{$this->path}/$id", $data) !== false; | ||
} | ||
|
||
public function destroy($id) | ||
{ | ||
$id = $this->prefix . $id; | ||
|
||
$session = "{$this->path}/$id"; | ||
if (file_exists($session)) { | ||
unlink($session); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function gc($max_lifetime) | ||
{ | ||
$files = glob("{$this->path}/{$this->prefix}*"); | ||
$currentTime = time(); | ||
|
||
foreach ($files as $file) { | ||
if (filemtime($file) + $max_lifetime < $currentTime && file_exists($file)) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters