-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathFileSessionStorage.php
79 lines (70 loc) · 2.09 KB
/
FileSessionStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Shopify\Auth;
/**
* Stores Sessions in a local file so that Session variables can be accessed throughout the app.
*/
class FileSessionStorage implements SessionStorage
{
private readonly string $path;
/**
* Initializes FileSessionStorage object
*
* @param string $path Path to store the session files in
*/
public function __construct(string $path = '/tmp/shopify_api_sessions')
{
if (!is_dir($path)) {
mkdir($path);
}
$this->path = $path;
}
/**
* Loads the Session object from the serialized file
*
* @param string $sessionId Id of the Session that is being loaded
* @return Session Returns Session if found, null otherwise
*/
public function loadSession(string $sessionId): ?Session
{
$path = $this->getPath($sessionId);
if (!file_exists($path)) {
return null;
}
return unserialize(file_get_contents($path));
}
/**
* Stores session into a file
*
* @param Session $session An instance of the session class to be stored in a file
* @return bool True if the number of bytes stored by file_put_contents() is > 0, false otherwise
*/
public function storeSession(Session $session): bool
{
return file_put_contents($this->getPath($session->getId()), serialize($session)) > 0;
}
/**
* Helper function that builds a path
*
* @param string $sessionId A Session ID
* @return string The path to the file that the Session will be stored in
*/
private function getPath(string $sessionId): string
{
return "{$this->path}/{$sessionId}";
}
/**
* Deletes a Session file
*
* @param string $sessionId The ID of the Session to be deleted
* @return bool Returns True if the file has been deleted or didn't exist
*/
public function deleteSession(string $sessionId): bool
{
$path = $this->getPath($sessionId);
if (file_exists($path)) {
unlink($path);
}
return true;
}
}