1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Providers; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache; |
6
|
|
|
use Doctrine\Common\Cache\Cache as CacheInterface; |
7
|
|
|
use Joomla\DI\Container; |
8
|
|
|
use Joomla\DI\ServiceProviderInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Cache service provider |
12
|
|
|
* |
13
|
|
|
* @since 1.0 |
14
|
|
|
*/ |
15
|
|
|
class CacheServiceProvider implements ServiceProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Registers the service provider with a DI container. |
19
|
|
|
* |
20
|
|
|
* @param Container $container The DI container. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
1 |
|
public function register(Container $container) |
27
|
|
|
{ |
28
|
1 |
|
$container->alias('cache', CacheInterface::class) |
29
|
1 |
|
->share( |
30
|
1 |
|
CacheInterface::class, |
31
|
1 |
|
function (Container $container) |
32
|
|
|
{ |
33
|
|
|
/** @var \Joomla\Registry\Registry $config */ |
34
|
|
|
$config = $container->get('config'); |
35
|
|
|
|
36
|
|
|
// If caching isn't enabled then just return a void cache |
37
|
|
|
if (!$config->get('cache.enabled', false)) |
38
|
|
|
{ |
39
|
|
|
return new Cache\VoidCache; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$adapter = $config->get('cache.adapter', 'filesystem'); |
43
|
|
|
|
44
|
|
|
switch ($adapter) |
45
|
|
|
{ |
46
|
|
|
case 'array': |
47
|
|
|
$handler = new Cache\ArrayCache; |
48
|
|
|
|
49
|
|
|
break; |
50
|
|
|
|
51
|
|
View Code Duplication |
case 'filesystem': |
|
|
|
|
52
|
|
|
$path = $config->get('cache.filesystem.path', 'cache'); |
53
|
|
|
|
54
|
|
|
// If no path is given, fall back to the system's temporary directory |
55
|
|
|
if (empty($path)) |
56
|
|
|
{ |
57
|
|
|
$path = sys_get_temp_dir(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// If the path is relative, make it absolute |
61
|
|
|
if (substr($path, 0, 1) !== '/') |
62
|
|
|
{ |
63
|
|
|
$path = APPROOT . '/' . $path; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$handler = new Cache\FilesystemCache($path); |
67
|
|
|
|
68
|
|
|
break; |
69
|
|
|
|
70
|
|
View Code Duplication |
case 'phpfile': |
|
|
|
|
71
|
|
|
$path = $config->get('cache.phpfile.path', 'cache'); |
72
|
|
|
|
73
|
|
|
// If no path is given, fall back to the system's temporary directory |
74
|
|
|
if (empty($path)) |
75
|
|
|
{ |
76
|
|
|
$path = sys_get_temp_dir(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// If the path is relative, make it absolute |
80
|
|
|
if (substr($path, 0, 1) !== '/') |
81
|
|
|
{ |
82
|
|
|
$path = APPROOT . '/' . $path; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$handler = new Cache\PhpFileCache($path); |
86
|
|
|
|
87
|
|
|
break; |
88
|
|
|
|
89
|
|
|
default: |
90
|
|
|
throw new \InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $handler; |
94
|
1 |
|
}, |
95
|
|
|
true |
96
|
1 |
|
); |
97
|
1 |
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.