Completed
Push — release-2.1 ( 4bdd41...11eb43 )
by Mathias
07:36
created

xcache_cache::cleanCache()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 5
nop 1
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines http://www.simplemachines.org
8
 * @copyright 2017 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 4
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * Our Cache API class
19
 * @package cacheAPI
20
 */
21
class xcache_cache extends cache_api
22
{
23
	/**
24
	 * {@inheritDoc}
25
	 */
26
	public function __construct()
27
	{
28
		global $modSettings;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
29
30
		parent::__construct();
31
32
		// Xcache requuires a admin username and password in order to issue a clear.
33
		if (!empty($modSettings['xcache_adminuser']) && !empty($modSettings['xcache_adminpass']))
34
		{
35
			ini_set('xcache.admin.user', $modSettings['xcache_adminuser']);
36
			ini_set('xcache.admin.pass', md5($modSettings['xcache_adminpass']));
37
		}
38
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function isSupported($test = false)
44
	{
45
		$supported = function_exists('xcache_get') && function_exists('xcache_set') && ini_get('xcache.var_size') > 0;
46
47
		if ($test)
48
			return $supported;
49
		return parent::isSupported() && $supported;
50
	}
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55
	public function getData($key, $ttl = null)
56
	{
57
		$key = $this->prefix . strtr($key, ':/', '-_');
58
59
		return xcache_get($key);
60
	}
61
62
	/**
63
	 * {@inheritDoc}
64
	 */
65
	public function putData($key, $value, $ttl = null)
66
	{
67
		$key = $this->prefix . strtr($key, ':/', '-_');
68
69
		if ($value === null)
70
			return xcache_unset($key);
71
		else
72
			return xcache_set($key, $value, $ttl);
73
	}
74
75
	/**
76
	 * {@inheritDoc}
77
	 */
78
	public function cleanCache($type = '')
79
	{
80
		global $modSettings;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
81
82
		// Xcache requuires a admin username and password in order to issue a clear. Ideally this would log an error, but it seems like something that could fill up the error log quickly.
83
		if (empty($modSettings['xcache_adminuser']) || empty($modSettings['xcache_adminpass']))
84
		{
85
			// We are going to at least invalidate it.
86
			$this->invalidateCache();
87
			return false;
88
		}
89
90
		// if passed a type, clear that type out
91
		if ($type === '' || $type === 'user')
92
			xcache_clear_cache(XC_TYPE_VAR, 0);
93
		if ($type === '' || $type === 'data')
94
			xcache_clear_cache(XC_TYPE_PHP, 0);
95
96
		$this->invalidateCache();
97
		return true;
98
	}
99
100
	/**
101
	 * {@inheritDoc}
102
	 */
103
	public function cacheSettings(array &$config_vars)
104
	{
105
		global $context, $txt;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
106
107
		$config_vars[] = $txt['cache_xcache_settings'];
108
		$config_vars[] = array('xcache_adminuser', $txt['cache_xcache_adminuser'], 'db', 'text', 0, 'xcache_adminuser');
109
110
		// While we could md5 this when saving, this could be tricky to be sure it doesn't get corrupted on additional saves.
111
		$config_vars[] = array('xcache_adminpass', $txt['cache_xcache_adminpass'], 'db', 'text', 0);
112
113
		if (!isset($context['settings_post_javascript']))
114
			$context['settings_post_javascript'] = '';
115
116
		$context['settings_post_javascript'] .= '
117
			$("#cache_accelerator").change(function (e) {
118
				var cache_type = e.currentTarget.value;
119
				$("#xcache_adminuser").prop("disabled", cache_type != "xcache");
120
				$("#xcache_adminpass").prop("disabled", cache_type != "xcache");
121
			});';
122
	}
123
124
	/**
125
	 * {@inheritDoc}
126
	 */
127
	public function getVersion()
128
	{
129
		return XCACHE_VERSION;
130
	}
131
}
132
133
?>