diff --git a/src/Legacy/Cache/APCu.php b/src/Legacy/Cache/APCu.php index 471c2b7ff..a580bb871 100644 --- a/src/Legacy/Cache/APCu.php +++ b/src/Legacy/Cache/APCu.php @@ -13,6 +13,8 @@ use TorrentPier\Dev; use MatthiasMullie\Scrapbook\Adapters\Apc; +use Exception; + /** * Class APCu * @package TorrentPier\Legacy\Cache @@ -54,6 +56,9 @@ class APCu extends Common */ public function __construct(string $prefix) { + if (!$this->isInstalled()) { + throw new Exception('ext-apcu not installed. Check out php.ini file'); + } $this->apcu = new Apc(); $this->prefix = $prefix; $this->dbg_enabled = Dev::sqlDebugAllowed(); @@ -127,4 +132,14 @@ class APCu extends Common return $result; } + + /** + * Checking if APCu is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('apcu') && function_exists('apcu_fetch'); + } } diff --git a/src/Legacy/Cache/Memcached.php b/src/Legacy/Cache/Memcached.php index 12ee93594..acbc80e14 100644 --- a/src/Legacy/Cache/Memcached.php +++ b/src/Legacy/Cache/Memcached.php @@ -14,6 +14,8 @@ use TorrentPier\Dev; use Memcached as MemcachedClient; use MatthiasMullie\Scrapbook\Adapters\Memcached as MemcachedCache; +use Exception; + /** * Class Memcached * @package TorrentPier\Legacy\Cache @@ -77,6 +79,9 @@ class Memcached extends Common */ public function __construct(array $cfg, string $prefix) { + if (!$this->isInstalled()) { + throw new Exception('ext-memcached not installed. Check out php.ini file'); + } $this->client = new MemcachedClient(); $this->cfg = $cfg; $this->prefix = $prefix; @@ -98,7 +103,7 @@ class Memcached extends Common } if (!$this->connected) { - die("Could not connect to $this->engine server"); + throw new Exception("Could not connect to $this->engine server"); } $this->memcached = new MemcachedCache($this->client); @@ -187,4 +192,14 @@ class Memcached extends Common return $result; } + + /** + * Checking if Memcached is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('memcached') && class_exists('Memcached'); + } } diff --git a/src/Legacy/Cache/Redis.php b/src/Legacy/Cache/Redis.php index 24e9b4e19..84b3d5e47 100644 --- a/src/Legacy/Cache/Redis.php +++ b/src/Legacy/Cache/Redis.php @@ -14,6 +14,8 @@ use TorrentPier\Dev; use Redis as RedisClient; use MatthiasMullie\Scrapbook\Adapters\Redis as RedisCache; +use Exception; + /** * Class Redis * @package TorrentPier\Legacy\Cache @@ -77,6 +79,9 @@ class Redis extends Common */ public function __construct(array $cfg, string $prefix) { + if (!$this->isInstalled()) { + throw new Exception('ext-redis not installed. Check out php.ini file'); + } $this->client = new RedisClient(); $this->cfg = $cfg; $this->prefix = $prefix; @@ -100,7 +105,7 @@ class Redis extends Common } if (!$this->connected) { - die("Could not connect to $this->engine server"); + throw new Exception("Could not connect to $this->engine server"); } $this->redis = new RedisCache($this->client); @@ -189,4 +194,14 @@ class Redis extends Common return $result; } + + /** + * Checking if Redis is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('redis') && class_exists('Redis'); + } } diff --git a/src/Legacy/Cache/Sqlite.php b/src/Legacy/Cache/Sqlite.php index e345410e6..219872b01 100644 --- a/src/Legacy/Cache/Sqlite.php +++ b/src/Legacy/Cache/Sqlite.php @@ -14,6 +14,8 @@ use TorrentPier\Dev; use MatthiasMullie\Scrapbook\Adapters\SQLite as SQLiteCache; use PDO; +use Exception; + /** * Class Sqlite * @package TorrentPier\Legacy\Cache @@ -34,6 +36,13 @@ class Sqlite extends Common */ public string $engine = 'SQLite'; + /** + * SQLite DB file extension + * + * @var string + */ + public string $dbExtension = '.db'; + /** * Cache prefix * @@ -56,7 +65,10 @@ class Sqlite extends Common */ public function __construct(string $dir, string $prefix) { - $client = new PDO("sqlite:$dir.db"); + if (!$this->isInstalled()) { + throw new Exception('ext-pdo_sqlite not installed. Check out php.ini file'); + } + $client = new PDO('sqlite:' . $dir . $this->dbExtension); $this->sqlite = new SQLiteCache($client); $this->prefix = $prefix; $this->dbg_enabled = Dev::sqlDebugAllowed(); @@ -130,4 +142,14 @@ class Sqlite extends Common return $result; } + + /** + * Checking if PDO SQLite is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('pdo_sqlite') && class_exists('PDO'); + } } diff --git a/src/Legacy/Datastore/APCu.php b/src/Legacy/Datastore/APCu.php index 46a0c0e44..c1a55a124 100644 --- a/src/Legacy/Datastore/APCu.php +++ b/src/Legacy/Datastore/APCu.php @@ -13,6 +13,8 @@ use TorrentPier\Dev; use MatthiasMullie\Scrapbook\Adapters\Apc; +use Exception; + /** * Class APCu * @package TorrentPier\Legacy\Datastore @@ -47,6 +49,9 @@ class APCu extends Common */ public function __construct(string $prefix) { + if (!$this->isInstalled()) { + throw new Exception('ext-apcu not installed. Check out php.ini file'); + } $this->apcu = new Apc(); $this->prefix = $prefix; $this->dbg_enabled = Dev::sqlDebugAllowed(); @@ -121,4 +126,14 @@ class APCu extends Common $this->num_queries++; } } + + /** + * Checking if APCu is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('apcu') && function_exists('apcu_fetch'); + } } diff --git a/src/Legacy/Datastore/Memcached.php b/src/Legacy/Datastore/Memcached.php index 6660d2add..0757069ba 100644 --- a/src/Legacy/Datastore/Memcached.php +++ b/src/Legacy/Datastore/Memcached.php @@ -14,6 +14,8 @@ use TorrentPier\Dev; use Memcached as MemcachedClient; use MatthiasMullie\Scrapbook\Adapters\Memcached as MemcachedCache; +use Exception; + /** * Class Memcached * @package TorrentPier\Legacy\Datastore @@ -70,6 +72,9 @@ class Memcached extends Common */ public function __construct(array $cfg, string $prefix) { + if (!$this->isInstalled()) { + throw new Exception('ext-memcached not installed. Check out php.ini file'); + } $this->client = new MemcachedClient(); $this->cfg = $cfg; $this->prefix = $prefix; @@ -91,7 +96,7 @@ class Memcached extends Common } if (!$this->connected) { - die("Could not connect to $this->engine server"); + throw new Exception("Could not connect to $this->engine server"); } $this->memcached = new MemcachedCache($this->client); @@ -181,4 +186,14 @@ class Memcached extends Common $this->num_queries++; } } + + /** + * Checking if Memcached is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('memcached') && class_exists('Memcached'); + } } diff --git a/src/Legacy/Datastore/Redis.php b/src/Legacy/Datastore/Redis.php index 96749bb7a..3b5909867 100644 --- a/src/Legacy/Datastore/Redis.php +++ b/src/Legacy/Datastore/Redis.php @@ -14,6 +14,8 @@ use TorrentPier\Dev; use Redis as RedisClient; use MatthiasMullie\Scrapbook\Adapters\Redis as RedisCache; +use Exception; + /** * Class Redis * @package TorrentPier\Legacy\Datastore @@ -70,6 +72,9 @@ class Redis extends Common */ public function __construct(array $cfg, string $prefix) { + if (!$this->isInstalled()) { + throw new Exception('ext-redis not installed. Check out php.ini file'); + } $this->client = new RedisClient(); $this->cfg = $cfg; $this->prefix = $prefix; @@ -93,7 +98,7 @@ class Redis extends Common } if (!$this->connected) { - die("Could not connect to $this->engine server"); + throw new Exception("Could not connect to $this->engine server"); } $this->redis = new RedisCache($this->client); @@ -183,4 +188,14 @@ class Redis extends Common $this->num_queries++; } } + + /** + * Checking if Redis is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('redis') && class_exists('Redis'); + } } diff --git a/src/Legacy/Datastore/Sqlite.php b/src/Legacy/Datastore/Sqlite.php index f6e766e01..6adac7f2a 100644 --- a/src/Legacy/Datastore/Sqlite.php +++ b/src/Legacy/Datastore/Sqlite.php @@ -14,6 +14,8 @@ use TorrentPier\Dev; use MatthiasMullie\Scrapbook\Adapters\SQLite as SQLiteCache; use PDO; +use Exception; + /** * Class Sqlite * @package TorrentPier\Legacy\Datastore @@ -27,6 +29,13 @@ class Sqlite extends Common */ public string $engine = 'SQLite'; + /** + * SQLite DB file extension + * + * @var string + */ + public string $dbExtension = '.db'; + /** * Cache prefix * @@ -49,7 +58,10 @@ class Sqlite extends Common */ public function __construct(string $dir, string $prefix) { - $client = new PDO("sqlite:$dir.db"); + if (!$this->isInstalled()) { + throw new Exception('ext-pdo_sqlite not installed. Check out php.ini file'); + } + $client = new PDO('sqlite:' . $dir . $this->dbExtension); $this->sqlite = new SQLiteCache($client); $this->prefix = $prefix; $this->dbg_enabled = Dev::sqlDebugAllowed(); @@ -124,4 +136,14 @@ class Sqlite extends Common $this->num_queries++; } } + + /** + * Checking if PDO SQLite is installed + * + * @return bool + */ + private function isInstalled(): bool + { + return extension_loaded('pdo_sqlite') && class_exists('PDO'); + } }