|
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 Shlinkio\Shlink\Common; |
|
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
|
|
|
|
|
15
|
|
|
class CacheFactory implements FactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
const VALID_CACHE_ADAPTERS = [ |
|
18
|
|
|
Cache\ApcuCache::class, |
|
19
|
|
|
Cache\ArrayCache::class, |
|
20
|
|
|
Cache\FilesystemCache::class, |
|
21
|
|
|
Cache\PhpFileCache::class, |
|
22
|
|
|
Cache\MemcachedCache::class, |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create an object |
|
27
|
|
|
* |
|
28
|
|
|
* @param ContainerInterface $container |
|
29
|
|
|
* @param string $requestedName |
|
30
|
|
|
* @param null|array $options |
|
31
|
|
|
* @return object |
|
32
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
33
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
34
|
|
|
* creating a service. |
|
35
|
|
|
* @throws ContainerException if any other error occurs |
|
36
|
|
|
*/ |
|
37
|
6 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
38
|
|
|
{ |
|
39
|
6 |
|
$appOptions = $container->get(AppOptions::class); |
|
40
|
6 |
|
$adapter = $this->getAdapter($container); |
|
41
|
6 |
|
$adapter->setNamespace($appOptions->__toString()); |
|
42
|
|
|
|
|
43
|
6 |
|
return $adapter; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param ContainerInterface $container |
|
48
|
|
|
* @return Cache\CacheProvider |
|
49
|
|
|
*/ |
|
50
|
6 |
|
protected function getAdapter(ContainerInterface $container) |
|
51
|
|
|
{ |
|
52
|
|
|
// Try to get the adapter from config |
|
53
|
6 |
|
$config = $container->get('config'); |
|
54
|
6 |
|
if (isset($config['cache'], $config['cache']['adapter']) |
|
55
|
6 |
|
&& in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS) |
|
56
|
|
|
) { |
|
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 Common\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 = isset($cacheConfig['options']['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
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.