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