aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipCacheFactory.class.php
blob: 5332e067881afbc65988b6f98217f1128c9c44b1 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
 * This factory retrieves the instance of StudipCache configured for use in
 * this Stud.IP installation.
 *
 * @package    studip
 * @subpackage lib
 *
 * @author    Marco Diedrich (mdiedric@uos)
 * @author    Marcus Lunzenauer (mlunzena@uos.de)
 * @copyright 2007 (c) Authors
 * @since     1.6
 * @license   GPL2 or any later version
 */

class StudipCacheFactory
{
    /**
     * the default cache class
     *
     * @var string
     */
    const DEFAULT_CACHE_CLASS = StudipDbCache::class;

    /**
     * singleton instance
     *
     * @var StudipCache
     */
    private static $cache;


    /**
     * config instance
     *
     * @var Config
     */
    private static $config = null;


    /**
     * Returns the currently used config instance
     *
     * @return Config        an instance of class Config used by this factory to
     *                       determine the class of the actual implementation of
     *                       the StudipCache interface; if no config was set, it
     *                       returns the instance returned by Config#getInstance
     * @see Config
     */
    public static function getConfig()
    {
        return is_null(self::$config) ? Config::getInstance() : self::$config;
    }


    /**
     * @param    Config       an instance of class Config which will be used to
     *                        determine the class of the implementation of interface
     *                        StudipCache
     *
     * @return void
     */
    public static function setConfig($config)
    {
        self::$config = $config;
        self::$cache = NULL;
    }

    /**
     * Resets the configuration and voids the cache instance.
     *
     * @return void
     */
    public static function unconfigure()
    {
        self::$cache = NULL;
    }

    /**
     * Returns a cache instance.
     *
     * @param bool $apply_proxied_operations Whether or not to apply any
     *                                       proxied (disable this in tests!)
     * @return StudipCache the cache instance
     */
    public static function getCache($apply_proxied_operations = true)
    {
        if (is_null(self::$cache)) {
            $proxied = false;

            if (!$GLOBALS['CACHING_ENABLE']) {
                self::$cache = new StudipMemoryCache();

                // Proxy cache operations if CACHING_ENABLE is different from the globally set
                // caching value. This should only be the case in cli mode.
                if (isset($GLOBALS['GLOBAL_CACHING_ENABLE']) && $GLOBALS['GLOBAL_CACHING_ENABLE']) {
                    $proxied = true;
                }
            } else {
                try {
                    $class = self::loadCacheClass();
                    $args = self::retrieveConstructorArguments();

                    self::$cache = self::instantiateCache($class, $args);
                } catch (Exception $e) {
                    error_log(__METHOD__ . ': ' . $e->getMessage());
                    PageLayout::addBodyElements(MessageBox::error(__METHOD__ . ': ' . $e->getMessage()));
                    $class = self::DEFAULT_CACHE_CLASS;
                    self::$cache = new $class();
                }
            }

            // If proxy should be used, inject it. Otherwise apply pending
            // operations, if any.
            if ($proxied) {
                self::$cache = new StudipCacheProxy(self::$cache);
            } elseif ($GLOBALS['CACHING_ENABLE'] && $apply_proxied_operations) {
                // Even if the above condition will try to eliminate most
                // failures, the following operation still needs to be wrapped
                // in a try/catch block. Otherwise there are no means to
                // execute migration 166 which creates the neccessary tables
                // for said operation.
                try {
                    StudipCacheOperation::apply(self::$cache);
                } catch (Exception $e) {
                }
            }
        }

        return self::$cache;
    }


    /**
     * Load configured cache class and return its name.
     *
     * @return string  the name of the configured cache class
     */
    public static function loadCacheClass()
    {
        $cacheConfig = self::getConfig()->SYSTEMCACHE;

        $cache_class = $cacheConfig['type'] ?: null;

        # default class
        if ($cache_class === null) {
            $version = new DBSchemaVersion();
            if ($version->get(1) < 224) {
                // db cache is not yet available, use StudipMemoryCache
                return 'StudipMemoryCache';
            }

            return self::DEFAULT_CACHE_CLASS;
        }

        if (!class_exists($cache_class)) {
            throw new UnexpectedValueException("Could not find class: '$cache_class'");
        }

        return $cache_class;
    }

    /**
     * Return an array of arguments required for instantiation of the cache
     * class.
     *
     * @return array  the array of arguments
     */
    public static function retrieveConstructorArguments()
    {
        $cacheConfig = self::getConfig()->SYSTEMCACHE;

        return $cacheConfig ?: [];
    }

    /**
     * Return an instance of a given class using some arguments
     *
     * @param  string  the name of the class
     * @param  array   an array of arguments to be used by the constructor
     *
     * @return StudipCache  an instance of the specified class
     */
    public static function instantiateCache($class, $arguments)
    {
        $reflection_class = new ReflectionClass($class);
        return (is_array($arguments['config']) && count($arguments['config']) > 0)
               ? $reflection_class->newInstanceArgs($arguments['config'])
               : $reflection_class->newInstance();
    }
}