1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Session\Factories; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Session\Adapters\Database\DatabaseSessionAdapter; |
18
|
|
|
use Quantum\Libraries\Session\Adapters\Native\NativeSessionAdapter; |
19
|
|
|
use Quantum\Libraries\Session\Exceptions\SessionException; |
20
|
|
|
use Quantum\Libraries\Config\Exceptions\ConfigException; |
21
|
|
|
use Quantum\Libraries\Session\Session; |
22
|
|
|
use Quantum\Di\Exceptions\DiException; |
23
|
|
|
use Quantum\Exceptions\BaseException; |
24
|
|
|
use Quantum\Loader\Setup; |
25
|
|
|
use ReflectionException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class SessionFactory |
29
|
|
|
* @package Quantum\Libraries\Session |
30
|
|
|
*/ |
31
|
|
|
class SessionFactory |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Supported adapters |
36
|
|
|
*/ |
37
|
|
|
const ADAPTERS = [ |
38
|
|
|
Session::NATIVE => NativeSessionAdapter::class, |
39
|
|
|
Session::DATABASE => DatabaseSessionAdapter::class, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Session|null |
44
|
|
|
*/ |
45
|
|
|
private static $instance = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array|null $storage |
49
|
|
|
* @return Session |
50
|
|
|
* @throws BaseException |
51
|
|
|
* @throws ConfigException |
52
|
|
|
* @throws DiException |
53
|
|
|
* @throws ReflectionException |
54
|
|
|
*/ |
55
|
|
|
public static function get(?array $storage = null): Session |
56
|
|
|
{ |
57
|
|
|
if (self::$instance === null) { |
58
|
|
|
return self::$instance = self::createInstance($storage); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return self::$instance; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Session |
66
|
|
|
* @throws BaseException |
67
|
|
|
* @throws ConfigException |
68
|
|
|
* @throws DiException |
69
|
|
|
* @throws ReflectionException |
70
|
|
|
*/ |
71
|
|
|
private static function createInstance(): Session |
72
|
|
|
{ |
73
|
|
|
if (!config()->has('session')) { |
74
|
|
|
config()->import(new Setup('Config', 'session')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$adapter = config()->get('session.current'); |
78
|
|
|
|
79
|
|
|
$adapterClass = self::getAdapterClass($adapter); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return new Session(new $adapterClass(config()->get('session.' . $adapter))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $adapter |
86
|
|
|
* @return string |
87
|
|
|
* @throws BaseException |
88
|
|
|
*/ |
89
|
|
|
private static function getAdapterClass(string $adapter): string |
90
|
|
|
{ |
91
|
|
|
if (!array_key_exists($adapter, self::ADAPTERS)) { |
92
|
|
|
throw SessionException::adapterNotSupported($adapter); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return self::ADAPTERS[$adapter]; |
96
|
|
|
} |
97
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.