| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation\Session\Storage; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class MockFileSessionStorage extends MockArraySessionStorage |
| 27 |
{ |
| 28 |
private string $savePath; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct(?string $savePath = null, string $name = 'MOCKSESSID', ?MetadataBag $metaBag = null) |
| 34 |
{ |
| 35 |
$savePath ??= sys_get_temp_dir(); |
| 36 |
|
| 37 |
if (!is_dir($savePath) && !@mkdir($savePath, 0o777, true) && !is_dir($savePath)) { |
| 38 |
throw new \RuntimeException(\sprintf('Session Storage was not able to create directory "%s".', $savePath)); |
| 39 |
} |
| 40 |
|
| 41 |
$this->savePath = $savePath; |
| 42 |
|
| 43 |
parent::__construct($name, $metaBag); |
| 44 |
} |
| 45 |
|
| 46 |
public function start(): bool |
| 47 |
{ |
| 48 |
if ($this->started) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
if (!$this->id) { |
| 53 |
$this->id = $this->generateId(); |
| 54 |
} |
| 55 |
|
| 56 |
$this->read(); |
| 57 |
|
| 58 |
$this->started = true; |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
public function regenerate(bool $destroy = false, ?int $lifetime = null): bool |
| 64 |
{ |
| 65 |
if (!$this->started) { |
| 66 |
$this->start(); |
| 67 |
} |
| 68 |
|
| 69 |
if ($destroy) { |
| 70 |
$this->destroy(); |
| 71 |
} |
| 72 |
|
| 73 |
return parent::regenerate($destroy, $lifetime); |
| 74 |
} |
| 75 |
|
| 76 |
public function save(): void |
| 77 |
{ |
| 78 |
if (!$this->started) { |
| 79 |
throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.'); |
| 80 |
} |
| 81 |
|
| 82 |
$data = $this->data; |
| 83 |
|
| 84 |
foreach ($this->bags as $bag) { |
| 85 |
if (empty($data[$key = $bag->getStorageKey()])) { |
| 86 |
unset($data[$key]); |
| 87 |
} |
| 88 |
} |
| 89 |
if ([$key = $this->metadataBag->getStorageKey()] === array_keys($data)) { |
| 90 |
unset($data[$key]); |
| 91 |
} |
| 92 |
|
| 93 |
try { |
| 94 |
if ($data) { |
| 95 |
$path = $this->getFilePath(); |
| 96 |
$tmp = $path.bin2hex(random_bytes(6)); |
| 97 |
file_put_contents($tmp, serialize($data)); |
| 98 |
rename($tmp, $path); |
| 99 |
} else { |
| 100 |
$this->destroy(); |
| 101 |
} |
| 102 |
} finally { |
| 103 |
$this->data = $data; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
$this->started = false; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private function destroy(): void |
| 116 |
{ |
| 117 |
set_error_handler(static function () {}); |
| 118 |
try { |
| 119 |
unlink($this->getFilePath()); |
| 120 |
} finally { |
| 121 |
restore_error_handler(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private function getFilePath(): string |
| 129 |
{ |
| 130 |
return $this->savePath.'/'.$this->id.'.mocksess'; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private function read(): void |
| 137 |
{ |
| 138 |
set_error_handler(static function () {}); |
| 139 |
try { |
| 140 |
$data = file_get_contents($this->getFilePath()); |
| 141 |
} finally { |
| 142 |
restore_error_handler(); |
| 143 |
} |
| 144 |
|
| 145 |
$this->data = $data ? unserialize($data) : []; |
| 146 |
|
| 147 |
$this->loadSession(); |
| 148 |
} |
| 149 |
} |