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