Passed
Push — master ( dbc06c...7e3eb0 )
by Andreas
22:22
created

midcom_services_cache_module   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 125
ccs 24
cts 45
cp 0.5333
rs 10
c 1
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B prepare_backend() 0 34 9
A invalidate_all() 0 5 2
A _create_backend() 0 18 3
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Doctrine\Common\Cache;
10
use Doctrine\Common\Cache\CacheProvider;
11
12
/**
13
 * This is the base class for the MidCOM cache modules. It provides a basic infrastructure
14
 * for building your own caching service, providing hooks for initialization.
15
 *
16
 * It provides convenience methods to start up the cache module, for example for the creation
17
 * of a cache backend instance. There is no specific initialization done during startup, to
18
 * allow the modules to do their own magic during startup (it is difficult to generalize such
19
 * stuff).
20
 *
21
 * @package midcom.services
22
 */
23
abstract class midcom_services_cache_module
24
{
25
    /**
26
     * A list of all backends created by _create_backend(). They will be automatically
27
     * shut down when the module shuts down. They are indexed by their name.
28
     *
29
     * @var Doctrine\Common\Cache\CacheProvider[]
30
     */
31
    private $_backends = [];
32
33
    /**
34
     * The cache key prefix.
35
     *
36
     * @var string
37
     */
38
    private $_prefix;
39
40
    /**
41
     * Cache backend instance.
42
     *
43
     * @var Doctrine\Common\Cache\CacheProvider
44
     */
45
    protected $backend;
46
47
    /**
48
     * Initialize the module. This will initialize the class configuration
49
     * and call the corresponding event handler.
50
     */
51 1
    public function __construct()
52
    {
53 1
        $this->_prefix = get_class($this) . $_SERVER['SERVER_NAME'];
54 1
        $this->backend = new Cache\VoidCache();
55 1
    }
56
57
    /**
58
     * Creates an instance of the handler described by the configuration passed to
59
     * the function.
60
     *
61
     * The configuration array must include the configuration parameters driver and
62
     * directory, as outlined in the midcom_services_cache_backend class documentation.
63
     *
64
     * All backends will be collected in the $_backends array, indexed by their name.
65
     *
66
     * Any duplicate instantiation will be intercepted, throwing a critical error.
67
     *
68
     * @param string $name The name of the backend, must be unique throughout the system.
69
     * @param array $config The configuration of the backend to create. It must contain
70
     *     the key 'driver', which indicates which backend to use.
71
     */
72 1
    protected function _create_backend($name, array $config) : CacheProvider
73
    {
74 1
        $name = $this->_prefix . $name;
75
76 1
        if (array_key_exists($name, $this->_backends)) {
77
            throw new midcom_error("Cannot create backend driver instance {$name}: A backend with this name does already exist.");
78
        }
79
80 1
        if (!array_key_exists('driver', $config)) {
81
            throw new midcom_error("Cannot create backend driver instance {$name}: The driver class is not specified in the configuration.");
82
        }
83
84 1
        $backend = $this->prepare_backend($config, $name);
85 1
        $backend->setNamespace($name);
86
87 1
        $this->_backends[$name] = $backend;
88
89 1
        return $backend;
90
    }
91
92 1
    private function prepare_backend(array $config, string $name) : CacheProvider
93
    {
94 1
        $directory = midcom::get()->getCacheDir();
95 1
        if (!empty($config['directory'])) {
96 1
            $directory .= '/' . $config['directory'];
97
        }
98
99 1
        switch ($config['driver']) {
100 1
            case 'apc':
101
                $backend = new Cache\ApcuCache();
102
                break;
103 1
            case 'memcached':
104 1
                if ($memcached = midcom_services_cache_module_memcache::prepare_memcached($config)) {
105 1
                    $backend = new Cache\MemcachedCache();
106 1
                    $backend->setMemcached($memcached);
107 1
                    break;
108
                }
109
                debug_add("memcache: Failed to connect. Falling back to filecache", MIDCOM_LOG_ERROR);
110
                // fall-through
111
            case 'dba':
112
            case 'flatfile':
113
                $backend = new Cache\FilesystemCache($directory . '/' . $name);
114
                break;
115
            case 'sqlite':
116
                $sqlite = new SQLite3("{$directory}/sqlite.db");
117
                $backend = new Cache\SQLite3Cache($sqlite, $name);
118
                break;
119
            case 'null':
120
            default:
121
                $backend = new Cache\VoidCache();
122
                break;
123
        }
124
125 1
        return $backend;
126
    }
127
128
    /**
129
     * Invalidate the cache completely, dropping all entries. The default implementation will
130
     * drop all entries from all registered cache backends using CacheProvider::flushAll().
131
     * Override this function if this behavior doesn't suit your needs.
132
     */
133
    public function invalidate_all()
134
    {
135
        foreach ($this->_backends as $name => $backend) {
136
            debug_add("Invalidating cache backend {$name}...", MIDCOM_LOG_INFO);
137
            $backend->flushAll();
138
        }
139
    }
140
141
    /**
142
     * Invalidate all cache objects related to the given GUID.
143
     *
144
     * @param string $guid The GUID that has to be invalidated.
145
     * @param object $object The object that has to be invalidated (if available).
146
     */
147
    abstract public function invalidate($guid, $object = null);
148
}
149