updated-packages
This commit is contained in:
@@ -13,7 +13,8 @@ namespace Symfony\Component\HttpFoundation\Session\Storage;
|
||||
|
||||
/**
|
||||
* MockFileSessionStorage is used to mock sessions for
|
||||
* functional testing when done in a single PHP process.
|
||||
* functional testing where you may need to persist session data
|
||||
* across separate PHP processes.
|
||||
*
|
||||
* No PHP session is actually started since a session can be initialized
|
||||
* and shutdown only once per PHP execution cycle and this class does
|
||||
@@ -27,9 +28,8 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* @param string $savePath Path of directory to save session files
|
||||
* @param string $name Session name
|
||||
* @param MetadataBag $metaBag MetadataBag instance
|
||||
* @param string $savePath Path of directory to save session files
|
||||
* @param string $name Session name
|
||||
*/
|
||||
public function __construct(string $savePath = null, string $name = 'MOCKSESSID', MetadataBag $metaBag = null)
|
||||
{
|
||||
@@ -38,7 +38,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
}
|
||||
|
||||
if (!is_dir($savePath) && !@mkdir($savePath, 0777, true) && !is_dir($savePath)) {
|
||||
throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s"', $savePath));
|
||||
throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s".', $savePath));
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
@@ -88,7 +88,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
public function save()
|
||||
{
|
||||
if (!$this->started) {
|
||||
throw new \RuntimeException('Trying to save a session that was not started yet or was already closed');
|
||||
throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
|
||||
}
|
||||
|
||||
$data = $this->data;
|
||||
@@ -98,13 +98,16 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
if (array($key = $this->metadataBag->getStorageKey()) === array_keys($data)) {
|
||||
if ([$key = $this->metadataBag->getStorageKey()] === array_keys($data)) {
|
||||
unset($data[$key]);
|
||||
}
|
||||
|
||||
try {
|
||||
if ($data) {
|
||||
file_put_contents($this->getFilePath(), serialize($data));
|
||||
$path = $this->getFilePath();
|
||||
$tmp = $path.bin2hex(random_bytes(6));
|
||||
file_put_contents($tmp, serialize($data));
|
||||
rename($tmp, $path);
|
||||
} else {
|
||||
$this->destroy();
|
||||
}
|
||||
@@ -112,9 +115,8 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
// this is needed for Silex, where the session object is re-used across requests
|
||||
// in functional tests. In Symfony, the container is rebooted, so we don't have
|
||||
// this issue
|
||||
// this is needed when the session object is re-used across multiple requests
|
||||
// in functional tests.
|
||||
$this->started = false;
|
||||
}
|
||||
|
||||
@@ -122,19 +124,20 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
* Deletes a session from persistent storage.
|
||||
* Deliberately leaves session data in memory intact.
|
||||
*/
|
||||
private function destroy()
|
||||
private function destroy(): void
|
||||
{
|
||||
if (is_file($this->getFilePath())) {
|
||||
set_error_handler(static function () {});
|
||||
try {
|
||||
unlink($this->getFilePath());
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate path to file.
|
||||
*
|
||||
* @return string File path
|
||||
*/
|
||||
private function getFilePath()
|
||||
private function getFilePath(): string
|
||||
{
|
||||
return $this->savePath.'/'.$this->id.'.mocksess';
|
||||
}
|
||||
@@ -142,10 +145,16 @@ class MockFileSessionStorage extends MockArraySessionStorage
|
||||
/**
|
||||
* Reads session from storage and loads session.
|
||||
*/
|
||||
private function read()
|
||||
private function read(): void
|
||||
{
|
||||
$filePath = $this->getFilePath();
|
||||
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();
|
||||
set_error_handler(static function () {});
|
||||
try {
|
||||
$data = file_get_contents($this->getFilePath());
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
$this->data = $data ? unserialize($data) : [];
|
||||
|
||||
$this->loadSession();
|
||||
}
|
||||
|
Reference in New Issue
Block a user