|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/session package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Session\Driver; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Session\SessionDriverInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Session driver, base class for all session drivers |
|
16
|
|
|
* |
|
17
|
|
|
* @package Slick\Session\Driver |
|
18
|
|
|
* @author Filipe Silva <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Driver implements SessionDriverInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string Session prefix on key names |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $prefix; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Driver constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* The $options parameter is an associative array where key will be matched |
|
32
|
|
|
* with the driver property and if property exists the correspondent value |
|
33
|
|
|
* will be assigned. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $options |
|
36
|
|
|
*/ |
|
37
|
14 |
|
public function __construct(array $options = []) |
|
38
|
|
|
{ |
|
39
|
14 |
|
foreach ($options as $name => $option) { |
|
40
|
4 |
|
if (property_exists($this, $name)) { |
|
41
|
4 |
|
$this->{$name} = $option; |
|
42
|
4 |
|
} |
|
43
|
14 |
|
} |
|
44
|
14 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function set($key, $value) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
2 |
|
$prefix = $this->prefix; |
|
52
|
2 |
|
$_SESSION[$prefix.$key] = $value; |
|
53
|
2 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function get($key, $default = null) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
4 |
|
$key = $this->prefix.$key; |
|
62
|
4 |
|
$value = $default; |
|
63
|
4 |
|
if (array_key_exists($key, $_SESSION)) { |
|
64
|
2 |
|
$value = $_SESSION[$key]; |
|
65
|
2 |
|
} |
|
66
|
4 |
|
return $value; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function erase($key) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
2 |
|
$prefix = $this->prefix; |
|
75
|
2 |
|
unset($_SESSION[$prefix.$key]); |
|
76
|
2 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Sets the prefix for session key names |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $prefix |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function setPrefix($prefix) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$this->prefix = $prefix; |
|
89
|
2 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: