diff --git a/flextype/cache/AcpuAdapter.php b/flextype/cache/AcpuAdapter.php new file mode 100644 index 00000000..a7a325e3 --- /dev/null +++ b/flextype/cache/AcpuAdapter.php @@ -0,0 +1,19 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + return new AcpuCache(); + } +} diff --git a/flextype/cache/ArrayAdapter.php b/flextype/cache/ArrayAdapter.php new file mode 100644 index 00000000..4df068c8 --- /dev/null +++ b/flextype/cache/ArrayAdapter.php @@ -0,0 +1,19 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + return new ArrayCache(); + } +} diff --git a/flextype/cache/CacheAdapterInterface.php b/flextype/cache/CacheAdapterInterface.php new file mode 100644 index 00000000..c27e3b8b --- /dev/null +++ b/flextype/cache/CacheAdapterInterface.php @@ -0,0 +1,23 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + $cache_directory = PATH['cache'] . '/doctrine/'; + + if (! Filesystem::has($cache_directory)) { + Filesystem::createDir($cache_directory); + } + + return new FilesystemCache($cache_directory); + } +} diff --git a/flextype/cache/MemcachedAdapter.php b/flextype/cache/MemcachedAdapter.php new file mode 100644 index 00000000..149999d1 --- /dev/null +++ b/flextype/cache/MemcachedAdapter.php @@ -0,0 +1,29 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + $memcached = new Memecached(); + $memcached->addServer( + $this->flextype['registry']->get('settings.cache.memcached.server', 'localhost'), + $this->flextype['registry']->get('settings.cache.memcache.port', 11211) + ); + + $driver = new MemcachedCache(); + $driver->setMemcached($memcached); + + return $driver; + } +} diff --git a/flextype/cache/RedisAdapter.php b/flextype/cache/RedisAdapter.php new file mode 100644 index 00000000..a8b7ba21 --- /dev/null +++ b/flextype/cache/RedisAdapter.php @@ -0,0 +1,42 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + $redis = new Redis(); + $socket = $this->flextype['registry']->get('settings.cache.redis.socket', false); + $password = $this->flextype['registry']->get('settings.cache.redis.password', false); + + if ($socket) { + $redis->connect($socket); + } else { + $redis->connect( + $this->flextype['registry']->get('settings.cache.redis.server', 'localhost'), + $this->flextype['registry']->get('settings.cache.redis.port', 6379) + ); + } + + // Authenticate with password if set + if ($password && ! $redis->auth($password)) { + throw new RedisException('Redis authentication failed'); + } + + $driver = new RedisCache(); + $driver->setRedis($redis); + + return $driver; + } +} diff --git a/flextype/cache/SQLite3Adapter.php b/flextype/cache/SQLite3Adapter.php new file mode 100644 index 00000000..ff225a15 --- /dev/null +++ b/flextype/cache/SQLite3Adapter.php @@ -0,0 +1,29 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + $cache_directory = PATH['cache'] . '/doctrine/'; + + if (! Filesystem::has($cache_directory)) { + Filesystem::createDir($cache_directory); + } + + $db = new SQLite3($cache_directory . $this->flextype['registry']->get('settings.cache.sqlite3.database', 'flextype') . '.db'); + + return new SQLite3Cache($db, $this->flextype['registry']->get('settings.cache.sqlite3.table', 'flextype')); + } +} diff --git a/flextype/cache/WinCacheAdapter.php b/flextype/cache/WinCacheAdapter.php new file mode 100644 index 00000000..0fb7cf89 --- /dev/null +++ b/flextype/cache/WinCacheAdapter.php @@ -0,0 +1,19 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + return new WinCacheCache(); + } +} diff --git a/flextype/cache/ZendDataCacheAdapter.php b/flextype/cache/ZendDataCacheAdapter.php new file mode 100644 index 00000000..9661b026 --- /dev/null +++ b/flextype/cache/ZendDataCacheAdapter.php @@ -0,0 +1,19 @@ +flextype = $flextype; + } + + public function getDriver() : object + { + return new ZendDataCache(); + } +} diff --git a/flextype/core/Cache.php b/flextype/core/Cache.php index c5fb1fef..d3ee658a 100755 --- a/flextype/core/Cache.php +++ b/flextype/core/Cache.php @@ -9,14 +9,8 @@ declare(strict_types=1); namespace Flextype; -use Doctrine\Common\Cache as DoctrineCache; use Flextype\Component\Filesystem\Filesystem; -use Memcached; -use Redis; -use RedisException; -use SQLite3; use function clearstatcache; -use function extension_loaded; use function function_exists; use function md5; use function opcache_reset; @@ -89,194 +83,7 @@ class Cache */ public function getCacheDriver() : object { - // Try to set default cache driver name - $driver_name = $this->setDefaultCacheDriverName($this->flextype['registry']->get('settings.cache.driver')); - - // Set cache driver - return $this->setCacheDriver($driver_name); - } - - protected function setCacheDriver(string $driver_name) - { - switch ($driver_name) { - case 'apcu': - $driver = $this->setApcuCacheDriver(); - break; - case 'array': - $driver = $this->setArrayCacheDriver(); - break; - case 'wincache': - $driver = $this->setWinCacheDriver(); - break; - case 'memcached': - $driver = $this->setMemcachedCacheDriver(); - break; - case 'sqlite3': - $driver = $this->setSQLite3CacheDriver(); - break; - case 'zend': - $driver = $this->setZendDataCacheDriver(); - break; - case 'redis': - $driver = $this->setRedisCacheDriver(); - break; - default: - $driver = $this->setFilesystemCacheDriver(); - break; - } - - return $driver; - } - - /** - * The ZendDataCache driver uses the Zend Data Cache API available in the Zend Platform. - * - * @access protected - */ - protected function setZendDataCacheDriver() - { - return new DoctrineCache\ZendDataCache(); - } - - /** - * The SQLite3Cache driver stores the cache data in a SQLite database and depends on the sqlite3 extension - * http://php.net/manual/en/book.sqlite3.php - * - * @access protected - */ - protected function setSQLite3CacheDriver() - { - // Cache directory - $cache_directory = PATH['cache'] . '/doctrine/'; - - // Create doctrine cache directory if its not exists - ! Filesystem::has($cache_directory) and Filesystem::createDir($cache_directory); - - $db = new SQLite3($cache_directory . $this->flextype['registry']->get('settings.cache.sqlite3.database', 'flextype') . '.db'); - - return new DoctrineCache\SQLite3Cache($db, $this->flextype['registry']->get('settings.cache.sqlite3.table', 'flextype')); - } - - /** - * The MemcachedCache drivers stores the cache data in Memcached. - * - * @access protected - */ - protected function setMemcachedCacheDriver() - { - $memcached = new Memcached(); - $memcached->addServer( - $this->flextype['registry']->get('settings.cache.memcached.server', 'localhost'), - $this->flextype['registry']->get('settings.cache.memcache.port', 11211) - ); - $driver = new DoctrineCache\MemcachedCache(); - $driver->setMemcached($memcached); - - return $driver; - } - - /** - * The WinCacheCache driver uses the wincache_ucache_get, wincache_ucache_exists, etc. functions - * that come with the wincache extension - * http://php.net/manual/en/book.wincache.php - * - * @access protected - */ - protected function setWinCacheDriver() - { - return new DoctrineCache\WinCacheCache(); - } - - /** - * The ArrayCache driver stores the cache data in PHPs memory and is not persisted anywhere. - * This can be useful for caching things in memory for a single process when you don't need the cache to be persistent across processes. - * - * @access protected - */ - protected function setArrayCacheDriver() - { - return new DoctrineCache\ArrayCache(); - } - - /** - * The ApcuCache driver uses the apcu_fetch, apcu_exists, etc. functions - * that come with PHP so no additional setup is required in order to use it. - * - * @access protected - */ - protected function setApcuCacheDriver() - { - return new DoctrineCache\ApcuCache(); - } - - /** - * The RedisCache driver stores the cache data in Redis and depends on the phpredis extension - * https://github.com/phpredis/phpredis - * - * @access protected - */ - protected function setRedisCacheDriver() - { - $redis = new Redis(); - $socket = $this->flextype['registry']->get('settings.cache.redis.socket', false); - $password = $this->flextype['registry']->get('settings.cache.redis.password', false); - - if ($socket) { - $redis->connect($socket); - } else { - $redis->connect( - $this->flextype['registry']->get('settings.cache.redis.server', 'localhost'), - $this->flextype['registry']->get('settings.cache.redis.port', 6379) - ); - } - - // Authenticate with password if set - if ($password && ! $redis->auth($password)) { - throw new RedisException('Redis authentication failed'); - } - - $driver = new DoctrineCache\RedisCache(); - $driver->setRedis($redis); - - return $driver; - } - - /** - * Filesystem cache Driver - * - * @access protected - */ - protected function setFilesystemCacheDriver() - { - // Cache directory - $cache_directory = PATH['cache'] . '/doctrine/'; - - // Create doctrine cache directory if its not exists - ! Filesystem::has($cache_directory) and Filesystem::createDir($cache_directory); - - return new DoctrineCache\FilesystemCache($cache_directory); - } - - /** - * Set Default Cache Driver Name - * - * @param string $driver_name Driver name. - * - * @access protected - */ - protected function setDefaultCacheDriverName(string $driver_name) : string - { - if (! $driver_name || $driver_name === 'auto') { - if (extension_loaded('apcu')) { - $driver_name = 'apcu'; - } elseif (extension_loaded('wincache')) { - $driver_name = 'wincache'; - } else { - $driver_name = 'file'; - } - } - - return $driver_name; + return $this->flextype['cache_adapter']->getDriver(); } /** diff --git a/flextype/dependencies.php b/flextype/dependencies.php index 88c81568..2fdf5d4f 100644 --- a/flextype/dependencies.php +++ b/flextype/dependencies.php @@ -43,6 +43,8 @@ use Slim\Views\TwigExtension; use Thunder\Shortcode\ShortcodeFacade; use Twig\Extension\DebugExtension; use function date; +use function ucfirst; +use function extension_loaded; /** * Supply a custom callable resolver, which resolves PSR-15 middlewares. @@ -104,6 +106,28 @@ $flextype['flash'] = static function ($container) { return new Messages(); }; +/** + * Adds the cache adapter to the Flextype container + */ +$flextype['cache_adapter'] = static function ($container) use ($flextype) { + $driver_name = $container['registry']->get('settings.cache.driver'); + + if (! $driver_name || $driver_name === 'auto') { + if (extension_loaded('apcu')) { + $driver_name = 'apcu'; + } elseif (extension_loaded('wincache')) { + $driver_name = 'wincache'; + } else { + $driver_name = 'filesystem'; + } + } + + $class = ucfirst($driver_name); + $adapter = "Flextype\\Cache\\{$class}Adapter"; + + return new $adapter($flextype); +}; + /** * Add cache service to Flextype container */