Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/Session/Storage/MockFileSessionStorage.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpFoundation\Session\Storage;
13
14 /**
15 * MockFileSessionStorage is used to mock sessions for
16 * functional testing where you may need to persist session data
17 * across separate PHP processes.
18 *
19 * No PHP session is actually started since a session can be initialized
20 * and shutdown only once per PHP execution cycle and this class does
21 * not pollute any session related globals, including session_*() functions
22 * or session.* PHP ini directives.
23 *
24 * @author Drak <[email protected]>
25 */
26 class MockFileSessionStorage extends MockArraySessionStorage
27 {
28 private string $savePath;
29
30 /**
31 * @param string|null $savePath Path of directory to save session files
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 // this is needed when the session object is reused across multiple requests
107 // in functional tests.
108 $this->started = false;
109 }
110
111 /**
112 * Deletes a session from persistent storage.
113 * Deliberately leaves session data in memory intact.
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 * Calculate path to file.
127 */
128 private function getFilePath(): string
129 {
130 return $this->savePath.'/'.$this->id.'.mocksess';
131 }
132
133 /**
134 * Reads session from storage and loads session.
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 }