forked from simplesamlphp/simplesamlphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreFactory.php
More file actions
88 lines (78 loc) · 2.57 KB
/
StoreFactory.php
File metadata and controls
88 lines (78 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace SimpleSAML\Store;
use Exception;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Module;
use SimpleSAML\Utils;
/**
* Base class for data stores.
*
* @package simplesamlphp/simplesamlphp
*/
abstract class StoreFactory implements Utils\ClearableState
{
/**
* Our singleton instance.
*
* This is false if the data store isn't enabled, and null if we haven't attempted to initialize it.
*
* @var \SimpleSAML\Store\StoreInterface|false|null
*/
private static StoreInterface|false|null $instance = null;
/**
* Retrieve our singleton instance.
*
* @param string $storeType The type of store we need to instantiate
* @return \SimpleSAML\Store\StoreInterface|false The data store, or false if it isn't enabled.
*
* @throws \SimpleSAML\Error\CriticalConfigurationError
* @throws \Exception
*/
public static function getInstance(string $storeType): StoreInterface|false
{
if (self::$instance !== null) {
return self::$instance;
}
switch ($storeType) {
case 'phpsession':
// we cannot support advanced features with the PHP session store
self::$instance = false;
break;
case 'memcache':
self::$instance = new MemcacheStore();
break;
case 'sql':
self::$instance = new SQLStore();
break;
case 'redis':
self::$instance = new RedisStore();
break;
default:
// datastore from module
try {
$className = Module::resolveClass($storeType, 'StoreInterface');
} catch (Exception $e) {
$config = Configuration::getInstance();
$c = $config->toArray();
$c['store.type'] = 'phpsession';
throw new Error\CriticalConfigurationError(
"Invalid 'store.type' configuration option. Cannot find store '$storeType'.",
null,
$c,
);
}
/** @var \SimpleSAML\Store\StoreInterface|false */
self::$instance = new $className();
}
return self::$instance;
}
/**
* Clear any SSP specific state, such as SSP environmental variables or cached internals.
*/
public static function clearInternalState(): void
{
self::$instance = null;
}
}