1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/http |
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\Http; |
11
|
|
|
|
12
|
|
|
use Slick\Http\Session\Driver\NullDriver; |
13
|
|
|
use Slick\Http\Session\Driver\ServerDriver; |
14
|
|
|
use Slick\Http\Session\Exception\ClassNotFoundException; |
15
|
|
|
use Slick\Http\Session\Exception\InvalidDriverClassException; |
16
|
|
|
use Slick\Http\Session\SessionDriverInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Session factory class |
20
|
|
|
* |
21
|
|
|
* @package Slick\Http |
22
|
|
|
*/ |
23
|
|
|
final class Session |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
const DRIVER_NULL = NullDriver::class; |
27
|
|
|
const DRIVER_SERVER = ServerDriver::class; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $driverClass; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $options; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Creates a Session factory |
41
|
|
|
* |
42
|
|
|
* @param string $driverClass |
43
|
|
|
* @param array $options |
44
|
|
|
*/ |
45
|
|
|
public function __construct($driverClass, array $options = []) |
46
|
|
|
{ |
47
|
|
|
$this->driverClass = $driverClass; |
48
|
|
|
$this->options = $options; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates the session driver with provided options |
53
|
|
|
* |
54
|
6 |
|
* @param string $driverClass |
55
|
|
|
* @param array $options |
56
|
6 |
|
* |
57
|
6 |
|
* @return SessionDriverInterface |
58
|
6 |
|
* |
59
|
|
|
* @throws ClassNotFoundException if class does not exists |
60
|
|
|
* @throws InvalidDriverClassException if class does not implement the SessionDriverInterface |
61
|
|
|
*/ |
62
|
|
|
public static function create($driverClass = self::DRIVER_SERVER, array $options = []) |
63
|
|
|
{ |
64
|
|
|
$session = new Session($driverClass, $options); |
65
|
|
|
return $session->initialize(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
8 |
|
* Initializes a new session driver |
70
|
|
|
* |
71
|
8 |
|
* @return SessionDriverInterface |
72
|
6 |
|
* |
73
|
4 |
|
* @throws ClassNotFoundException if class does not exists |
74
|
|
|
* @throws InvalidDriverClassException if class does not implement the SessionDriverInterface |
75
|
|
|
*/ |
76
|
|
|
public function initialize() |
77
|
|
|
{ |
78
|
|
|
$this->checkClassExistence(); |
79
|
|
|
|
80
|
|
|
$this->checkClassType(); |
81
|
|
|
|
82
|
|
|
$className = $this->driverClass; |
83
|
|
|
return new $className($this->options); |
84
|
8 |
|
} |
85
|
|
|
|
86
|
8 |
|
/** |
87
|
2 |
|
* Checks if current driver class implements SessionDriverInterface |
88
|
2 |
|
*/ |
89
|
2 |
|
private function checkClassType() |
90
|
|
|
{ |
91
|
|
|
if (! is_subclass_of($this->driverClass, SessionDriverInterface::class)) { |
92
|
6 |
|
throw new InvalidDriverClassException( |
93
|
|
|
sprintf( |
94
|
|
|
"Session driver classes must implement %s interface.", |
95
|
|
|
SessionDriverInterface::class |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
6 |
|
* Check if driver class exists |
103
|
|
|
*/ |
104
|
6 |
|
private function checkClassExistence() |
105
|
2 |
|
{ |
106
|
2 |
|
if (!class_exists($this->driverClass)) { |
107
|
|
|
throw new ClassNotFoundException( |
108
|
2 |
|
"Session driver class '{$this->driverClass}'' does not exists." |
109
|
|
|
); |
110
|
4 |
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|