1 | <?php |
||
13 | class CacheFactory implements FactoryInterface |
||
14 | { |
||
15 | const VALID_CACHE_ADAPTERS = [ |
||
16 | Cache\ApcuCache::class, |
||
17 | Cache\ArrayCache::class, |
||
18 | Cache\FilesystemCache::class, |
||
19 | Cache\PhpFileCache::class, |
||
20 | Cache\MemcachedCache::class, |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * Create an object |
||
25 | * |
||
26 | * @param ContainerInterface $container |
||
27 | * @param string $requestedName |
||
28 | * @param null|array $options |
||
29 | * @return object |
||
30 | * @throws ServiceNotFoundException if unable to resolve the service. |
||
31 | * @throws ServiceNotCreatedException if an exception is raised when |
||
32 | * creating a service. |
||
33 | * @throws ContainerException if any other error occurs |
||
34 | */ |
||
35 | 6 | public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
36 | { |
||
37 | 6 | $appOptions = $container->get(AppOptions::class); |
|
38 | 6 | $adapter = $this->getAdapter($container); |
|
39 | 6 | $adapter->setNamespace($appOptions->__toString()); |
|
40 | |||
41 | 6 | return $adapter; |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param ContainerInterface $container |
||
46 | * @return Cache\CacheProvider |
||
47 | */ |
||
48 | 6 | protected function getAdapter(ContainerInterface $container) |
|
49 | { |
||
50 | // Try to get the adapter from config |
||
51 | 6 | $config = $container->get('config'); |
|
52 | 6 | if (isset($config['cache'], $config['cache']['adapter']) |
|
53 | 6 | && in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS) |
|
54 | ) { |
||
55 | 3 | return $this->resolveCacheAdapter($config['cache']); |
|
56 | } |
||
57 | |||
58 | // If the adapter has not been set in config, create one based on environment |
||
59 | 3 | return Common\env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache(); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param array $cacheConfig |
||
64 | * @return Cache\CacheProvider |
||
65 | */ |
||
66 | 3 | protected function resolveCacheAdapter(array $cacheConfig) |
|
95 | } |
||
96 |