-
Notifications
You must be signed in to change notification settings - Fork 9
/
CacheProvider.php
89 lines (76 loc) · 2.48 KB
/
CacheProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Joomla! Help Site
*
* @copyright Copyright (C) 2016 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*
* Portions of this code are derived from the previous help screen proxy component,
* please see https://github.com/joomla-projects/help-proxy for attribution
*/
namespace Joomla\Help\Service;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
/**
* Cache service provider
*/
class CacheProvider implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*/
public function register(Container $container)
{
$container->alias(AdapterInterface::class, CacheItemPoolInterface::class)
->share(
CacheItemPoolInterface::class,
static function (Container $container): CacheItemPoolInterface
{
/** @var \Joomla\Registry\Registry $config */
$config = $container->get('config');
// If caching isn't enabled then just return a void cache
if (!$config->get('cache.enabled', false))
{
return new NullAdapter;
}
$adapter = $config->get('cache.adapter', 'file');
$lifetime = $config->get('cache.lifetime', 900);
$namespace = $config->get('cache.namespace', 'jhelp');
switch ($adapter)
{
case 'file':
$path = $config->get('cache.filesystem.path', 'cache');
// If no path is given, fall back to the system's temporary directory
if (empty($path))
{
$path = sys_get_temp_dir();
}
// If the path is relative, make it absolute... Sorry Windows users, this breaks support for your environment
if (substr($path, 0, 1) !== '/')
{
$path = JPATH_ROOT . '/' . $path;
}
$options = [
'file.path' => $path,
];
return new FilesystemAdapter($namespace, $lifetime, $path);
case 'none':
return new NullAdapter;
case 'runtime':
return new ArrayAdapter($lifetime);
}
throw new InvalidArgumentException(sprintf('The "%s" cache adapter is not supported.', $adapter));
}
);
}
}