1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Providers; |
4
|
|
|
|
5
|
|
|
use Joomla\Cache\AbstractCacheItemPool; |
6
|
|
|
use Joomla\Cache\Adapter as CacheAdapter; |
7
|
|
|
use Joomla\Cache\CacheItemPoolInterface; |
8
|
|
|
use Joomla\DI\Container; |
9
|
|
|
use Joomla\DI\ServiceProviderInterface; |
10
|
|
|
use Psr\Cache\CacheItemPoolInterface as PsrCacheItemPoolInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Cache service provider |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class CacheServiceProvider implements ServiceProviderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Registers the service provider with a DI container. |
21
|
|
|
* |
22
|
|
|
* @param Container $container The DI container. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
* |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
1 |
|
public function register(Container $container) |
29
|
|
|
{ |
30
|
1 |
|
$container->alias('cache', PsrCacheItemPoolInterface::class) |
31
|
1 |
|
->alias(CacheItemPoolInterface::class, PsrCacheItemPoolInterface::class) |
32
|
1 |
|
->alias(AbstractCacheItemPool::class, PsrCacheItemPoolInterface::class) |
33
|
1 |
|
->share(PsrCacheItemPoolInterface::class, [$this, 'getCacheService'], true); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the `cache` service |
38
|
|
|
* |
39
|
|
|
* @param Container $container The DI container. |
40
|
|
|
* |
41
|
|
|
* @return PsrCacheItemPoolInterface |
42
|
|
|
* |
43
|
|
|
* @since 1.0 |
44
|
|
|
*/ |
45
|
6 |
|
public function getCacheService(Container $container) |
46
|
|
|
{ |
47
|
|
|
/** @var \Joomla\Registry\Registry $config */ |
48
|
6 |
|
$config = $container->get('config'); |
49
|
|
|
|
50
|
|
|
// If caching isn't enabled then just return a void cache |
51
|
6 |
|
if (!$config->get('cache.enabled', false)) |
52
|
6 |
|
{ |
53
|
1 |
|
return new CacheAdapter\None; |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
$adapter = $config->get('cache.adapter', 'file'); |
57
|
|
|
|
58
|
|
|
switch ($adapter) |
59
|
|
|
{ |
60
|
5 |
|
case 'filesystem': |
61
|
2 |
|
$path = $config->get('cache.filesystem.path', 'cache'); |
62
|
|
|
|
63
|
|
|
// If no path is given, fall back to the system's temporary directory |
64
|
2 |
|
if (empty($path)) |
65
|
2 |
|
{ |
66
|
1 |
|
$path = sys_get_temp_dir(); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
// If the path is relative, make it absolute... Sorry Windows users, this breaks support for your environment |
70
|
2 |
|
if (substr($path, 0, 1) !== '/') |
71
|
2 |
|
{ |
72
|
1 |
|
$path = APPROOT . '/' . $path; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
$options = [ |
76
|
2 |
|
'file.path' => $path, |
77
|
2 |
|
]; |
78
|
|
|
|
79
|
2 |
|
return new CacheAdapter\File($options); |
80
|
|
|
|
81
|
3 |
|
case 'none': |
82
|
1 |
|
return new CacheAdapter\None; |
83
|
|
|
|
84
|
2 |
|
case 'runtime': |
85
|
1 |
|
return new CacheAdapter\Runtime; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
throw new \InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|