get(AppOptions::class); $adapter = $this->getAdapter($container); $adapter->setNamespace($appOptions->__toString()); return $adapter; } /** * @param ContainerInterface $container * @return Cache\CacheProvider */ protected function getAdapter(ContainerInterface $container) { // Try to get the adapter from config $config = $container->get('config'); if (isset($config['cache']['adapter']) && contains(self::VALID_CACHE_ADAPTERS, $config['cache']['adapter'])) { return $this->resolveCacheAdapter($config['cache']); } // If the adapter has not been set in config, create one based on environment return env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache(); } /** * @param array $cacheConfig * @return Cache\CacheProvider */ protected function resolveCacheAdapter(array $cacheConfig) { switch ($cacheConfig['adapter']) { case Cache\ArrayCache::class: case Cache\ApcuCache::class: return new $cacheConfig['adapter'](); case Cache\FilesystemCache::class: case Cache\PhpFileCache::class: return new $cacheConfig['adapter']($cacheConfig['options']['dir']); case Cache\MemcachedCache::class: $memcached = new Memcached(); $servers = $cacheConfig['options']['servers'] ?? []; foreach ($servers as $server) { if (! isset($server['host'])) { continue; } $port = isset($server['port']) ? (int) $server['port'] : 11211; $memcached->addServer($server['host'], $port); } $cache = new Cache\MemcachedCache(); $cache->setMemcached($memcached); return $cache; default: return new Cache\ArrayCache(); } } }