inital commit
[pharext/pharext.org] / app / Github / Storage / Session.php
1 <?php
2
3 namespace app\Github\Storage;
4
5 use app\Github\Storage;
6
7 class Session implements Storage
8 {
9 private $ns;
10
11 function __construct($ns = "github") {
12 $this->ns = $ns;
13 }
14
15 function set($key, $val, $ttl = null) {
16 $_SESSION[$this->ns][$key] = [$val, $ttl, isset($ttl) ? time() : null];
17 return $this;
18 }
19
20 function get($key, &$val = null, &$ltl = null, $update = false) {
21 if (!isset($_SESSION[$this->ns][$key])) {
22 return false;
23 }
24 list($val, $ttl, $set) = $_SESSION[$this->ns][$key];
25 if (!isset($ttl)) {
26 return true;
27 }
28 $now = time();
29 $ltl = $ttl - ($now - $set);
30 if ($ltl >= 0) {
31 if ($update) {
32 $_SESSION[$this->ns][$key][2] = $now;
33 }
34 return true;
35 }
36 return false;
37 }
38
39 function del($key) {
40 unset($_SESSION[$this->ns][$key]);
41 return $this;
42 }
43 }