public function __construct($db, $table = 'pop_cache', $pdo = false)
{
$this->db = $db;
$this->table = $table;
$dir = dirname($this->db);
// If the database file doesn't exist, create it.
if (!file_exists($this->db)) {
if (is_writable($dir)) {
touch($db);
} else {
throw new Exception('Error: That cache db file and/or directory is not writable.');
}
}
// Make it writable.
chmod($this->db, 0777);
// Check the permissions, access the database and check for the cache table.
if (!is_writable($dir) || !is_writable($this->db)) {
throw new Exception('Error: That cache db file and/or directory is not writable.');
}
$pdoDrivers = class_exists('Pdo') ? \PDO::getAvailableDrivers() : array();
if (!class_exists('Sqlite3') && !in_array('sqlite', $pdoDrivers)) {
throw new Exception('Error: SQLite is not available.');
} else {
if ($pdo && !in_array('sqlite', $pdoDrivers)) {
$pdo = false;
} else {
if (!$pdo && !class_exists('Sqlite3')) {
$pdo = true;
}
}
}
if ($pdo) {
$this->sqlite = new \Pop\Db\Sql(\Pop\Db\Db::factory('Pdo', array('type' => 'sqlite', 'database' => $this->db)), $table);
} else {
$this->sqlite = new \Pop\Db\Sql(\Pop\Db\Db::factory('Sqlite', array('database' => $this->db)), $table);
}
// If the cache table doesn't exist, create it.
if (!in_array($this->table, $this->sqlite->adapter()->getTables())) {
$this->sqlite->adapter()->query('CREATE TABLE IF NOT EXISTS "' . $this->table . '" ("id" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "value" BLOB, "time" INTEGER)');
}
}