|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Interfaces\SessionManagementInterface; |
|
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Traits\Creator; |
|
7
|
|
|
|
|
8
|
|
|
class SessionManagement implements SessionManagementInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use Creator; |
|
11
|
|
|
|
|
12
|
|
|
protected $sessionFileLocation = ''; |
|
13
|
|
|
|
|
14
|
|
|
public static function initSession(string $sessionFileLocation): SessionManagementInterface |
|
15
|
|
|
{ |
|
16
|
|
|
$obj = self::create(); |
|
17
|
|
|
$obj->setSessionFileLocation($sessionFileLocation); |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
return $obj; |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function setSessionFileLocation($sessionFileLocation): SessionManagementInterface |
|
23
|
|
|
{ |
|
24
|
|
|
$this->sessionFileLocation = $sessionFileLocation; |
|
25
|
|
|
$link = $sessionFileLocation; |
|
26
|
|
|
$dir = dirname($link); |
|
27
|
|
|
if (! file_exists($dir)) { |
|
28
|
|
|
user_error('Could not find: ' . $dir); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getSessionFileLocation(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->sessionFileLocation; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function deleteSession() |
|
40
|
|
|
{ |
|
41
|
|
|
unlink($this->getSessionFileLocation()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getSessionValue(string $key): string |
|
45
|
|
|
{ |
|
46
|
|
|
$session = $this->getSessionData(); |
|
47
|
|
|
if (isset($session[$key])) { |
|
48
|
|
|
return $session[$key]; |
|
49
|
|
|
} |
|
50
|
|
|
return ''; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getSessionData(): array |
|
54
|
|
|
{ |
|
55
|
|
|
if (file_exists($this->getSessionFileLocation())) { |
|
56
|
|
|
$data = file_get_contents($this->getSessionFileLocation()) ?? '{}'; |
|
57
|
|
|
if (! $data) { |
|
58
|
|
|
user_error('Could not read from: ' . $this->getSessionFileLocation()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return json_decode($data, true); |
|
62
|
|
|
} |
|
63
|
|
|
$this->setSessionData([]); |
|
64
|
|
|
|
|
65
|
|
|
return $this->getSessionData(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function setSessionData(array $session): SessionManagementInterface |
|
69
|
|
|
{ |
|
70
|
|
|
if (! file_exists($this->getSessionFileLocation())) { |
|
71
|
|
|
$session['Started'] = date('Y-m-d h:i '); |
|
72
|
|
|
} |
|
73
|
|
|
$data = json_encode($session, JSON_PRETTY_PRINT); |
|
74
|
|
|
try { |
|
75
|
|
|
$file = fopen($this->getSessionFileLocation(), 'w'); |
|
76
|
|
|
if ($file === false) { |
|
77
|
|
|
throw new \RuntimeException('Failed to open file: ' . $this->getSessionFileLocation()); |
|
78
|
|
|
} |
|
79
|
|
|
$writeOutcome = fwrite($file, (string) $data); |
|
80
|
|
|
if ($writeOutcome === false) { |
|
81
|
|
|
throw new \RuntimeException('Failed to write file: ' . $this->getSessionFileLocation()); |
|
82
|
|
|
} |
|
83
|
|
|
$closeOutcome = fclose($file); |
|
84
|
|
|
if ($closeOutcome === false) { |
|
85
|
|
|
throw new \RuntimeException('Failed to close file: ' . $this->getSessionFileLocation()); |
|
86
|
|
|
} |
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
// send error message if you can |
|
89
|
|
|
echo 'Caught exception: ' . $e->getMessage() . ' location of file: ' . $this->getSessionFileLocation(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function setSessionValue(string $key, string $value): SessionManagementInterface |
|
96
|
|
|
{ |
|
97
|
|
|
$session = $this->getSessionData(); |
|
98
|
|
|
$session[$key] = trim($value); |
|
99
|
|
|
$this->setSessionData($session); |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|