summaryrefslogtreecommitdiffstats
path: root/lib/SimpleSAML/Store.php
blob: 5ee6e43760d93df31f4d2651cbbd6c857bac4cc8 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

namespace SimpleSAML;

use SimpleSAML\Error\CriticalConfigurationError;

/**
 * Base class for data stores.
 *
 * @package SimpleSAMLphp
 */
abstract class Store
{
    /**
     * 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|false|null
     */
    private static $instance;


    /**
     * Retrieve our singleton instance.
     *
     * @return false|\SimpleSAML\Store The data store, or false if it isn't enabled.
     *
     * @throws \SimpleSAML\Error\CriticalConfigurationError
     */
    public static function getInstance()
    {
        if (self::$instance !== null) {
            return self::$instance;
        }

        $config = \SimpleSAML_Configuration::getInstance();
        $storeType = $config->getString('store.type', null);
        if ($storeType === null) {
            $storeType = $config->getString('session.handler', 'phpsession');
        }

        switch ($storeType) {
            case 'phpsession':
                // we cannot support advanced features with the PHP session store
                self::$instance = false;
                break;
            case 'memcache':
                self::$instance = new Store\Memcache();
                break;
            case 'sql':
                self::$instance = new Store\SQL();
                break;
            default:
                // datastore from module
                try {
                    $className = Module::resolveClass($storeType, 'Store', '\SimpleSAML\Store');
                } catch (\Exception $e) {
                    $c = $config->toArray();
                    $c['store.type'] = 'phpsession';
                    throw new CriticalConfigurationError(
                        "Invalid 'store.type' configuration option. Cannot find store '$storeType'.",
                        null,
                        $c
                    );
                }
                self::$instance = new $className();
        }

        return self::$instance;
    }


    /**
     * Retrieve a value from the data store.
     *
     * @param string $type The data type.
     * @param string $key The key.
     *
     * @return mixed|null The value.
     */
    abstract public function get($type, $key);


    /**
     * Save a value to the data store.
     *
     * @param string   $type The data type.
     * @param string   $key The key.
     * @param mixed    $value The value.
     * @param int|null $expire The expiration time (unix timestamp), or null if it never expires.
     */
    abstract public function set($type, $key, $value, $expire = null);


    /**
     * Delete a value from the data store.
     *
     * @param string $type The data type.
     * @param string $key The key.
     */
    abstract public function delete($type, $key);
}