1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Common\Factory; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache; |
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
use Interop\Container\Exception\ContainerException; |
9
|
|
|
use Memcached; |
10
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions; |
11
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
12
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
13
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
14
|
|
|
use function Functional\contains; |
15
|
|
|
use function Shlinkio\Shlink\Common\env; |
16
|
|
|
|
17
|
|
|
class CacheFactory implements FactoryInterface |
18
|
|
|
{ |
19
|
|
|
private const VALID_CACHE_ADAPTERS = [ |
20
|
|
|
Cache\ApcuCache::class, |
21
|
|
|
Cache\ArrayCache::class, |
22
|
|
|
Cache\FilesystemCache::class, |
23
|
|
|
Cache\PhpFileCache::class, |
24
|
|
|
Cache\MemcachedCache::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create an object |
29
|
|
|
* |
30
|
|
|
* @param ContainerInterface $container |
31
|
|
|
* @param string $requestedName |
32
|
|
|
* @param null|array $options |
33
|
|
|
* @return object |
34
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
35
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
36
|
|
|
* creating a service. |
37
|
|
|
* @throws ContainerException if any other error occurs |
38
|
|
|
*/ |
39
|
6 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
40
|
|
|
{ |
41
|
6 |
|
$appOptions = $container->get(AppOptions::class); |
42
|
6 |
|
$adapter = $this->getAdapter($container); |
43
|
6 |
|
$adapter->setNamespace($appOptions->__toString()); |
44
|
|
|
|
45
|
6 |
|
return $adapter; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ContainerInterface $container |
50
|
|
|
* @return Cache\CacheProvider |
51
|
|
|
*/ |
52
|
6 |
|
protected function getAdapter(ContainerInterface $container) |
53
|
|
|
{ |
54
|
|
|
// Try to get the adapter from config |
55
|
6 |
|
$config = $container->get('config'); |
56
|
6 |
|
if (isset($config['cache']['adapter']) && contains(self::VALID_CACHE_ADAPTERS, $config['cache']['adapter'])) { |
57
|
3 |
|
return $this->resolveCacheAdapter($config['cache']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// If the adapter has not been set in config, create one based on environment |
61
|
3 |
|
return env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $cacheConfig |
66
|
|
|
* @return Cache\CacheProvider |
67
|
|
|
*/ |
68
|
3 |
|
protected function resolveCacheAdapter(array $cacheConfig) |
69
|
|
|
{ |
70
|
3 |
|
switch ($cacheConfig['adapter']) { |
71
|
|
|
case Cache\ArrayCache::class: |
72
|
|
|
case Cache\ApcuCache::class: |
73
|
1 |
|
return new $cacheConfig['adapter'](); |
74
|
|
|
case Cache\FilesystemCache::class: |
75
|
|
|
case Cache\PhpFileCache::class: |
76
|
1 |
|
return new $cacheConfig['adapter']($cacheConfig['options']['dir']); |
77
|
|
|
case Cache\MemcachedCache::class: |
78
|
1 |
|
$memcached = new Memcached(); |
79
|
1 |
|
$servers = $cacheConfig['options']['servers'] ?? []; |
80
|
|
|
|
81
|
1 |
|
foreach ($servers as $server) { |
82
|
1 |
|
if (! isset($server['host'])) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
1 |
|
$port = isset($server['port']) ? (int) $server['port'] : 11211; |
86
|
|
|
|
87
|
1 |
|
$memcached->addServer($server['host'], $port); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$cache = new Cache\MemcachedCache(); |
91
|
1 |
|
$cache->setMemcached($memcached); |
92
|
1 |
|
return $cache; |
93
|
|
|
default: |
94
|
|
|
return new Cache\ArrayCache(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|