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