diff --git a/lib/Database.php b/lib/Database.php index e29b8337..0b4008d8 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -22,8 +22,8 @@ class Database { public function __construct(RuntimeData $data) { $this->data = $data; - $this->driver = $data->conf->dbDriver; - $this->db = $this->driver::create($data, INSTALL); + $this->driver = $driver = $data->conf->dbDriver; + $this->db = new $driver($data, INSTALL); $ver = $this->db->schemaVersion(); if(!INSTALL && $ver < self::SCHEMA_VERSION) { $this->db->update(self::SCHEMA_VERSION); @@ -298,6 +298,7 @@ class Database { (new PicoFeed\Reader\Favicon)->find($url), $feed->siteUrl, // Convert the date formats to SQL date format before inserting. + // FIXME: Dates should be formatted transparently by the driver's Statement wrapper, not here $this->driver::formatDate($feed->date), $this->driver::formatDate($resource->getLastModified()), $resource->getEtag(), diff --git a/lib/Db/Driver.php b/lib/Db/Driver.php index de05cc69..ad4d5fb2 100644 --- a/lib/Db/Driver.php +++ b/lib/Db/Driver.php @@ -2,9 +2,8 @@ declare(strict_types=1); namespace JKingWeb\NewsSync\Db; -interface Driver { - // returns an instance of a class implementing this interface. Implemented as a static method so that classes may return their PDO equivalents instead of themselves - static function create(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false): Driver; +interface Driver { + function __construct(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false); // returns a human-friendly name for the driver (for display in installer, for example) static function driverName(): string; // returns the version of the scheme of the opened database; if uninitialized should return 0 diff --git a/lib/Db/DriverSQLite3.php b/lib/Db/DriverSQLite3.php index babf994a..bed2f35b 100644 --- a/lib/Db/DriverSQLite3.php +++ b/lib/Db/DriverSQLite3.php @@ -9,6 +9,8 @@ class DriverSQLite3 implements Driver { protected $data; private function __construct(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false) { + // check to make sure required extension is loaded + if(!class_exists("SQLite3")) throw new Exception("extMissing", self::driverName()); $this->data = $data; $file = $data->conf->dbSQLite3File; // if the file exists (or we're initializing the database), try to open it and set initial options @@ -32,21 +34,10 @@ class DriverSQLite3 implements Driver { } public function __destruct() { - $this->db->close(); + try{$this->db->close();} catch(\Exception $e) {} unset($this->db); } - static public function create(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false): Driver { - // check to make sure required extensions are loaded - if(class_exists("SQLite3")) { - return new self($data, $install); - } else if(class_exists("PDO") && in_array("sqlite",\PDO::getAvailableDrivers())) { - return new DriverSQLite3PDO($data, $install); - } else { - throw new Exception("extMissing", self::driverName()); - } - } - static public function driverName(): string { return "SQLite 3"; diff --git a/lib/User.php b/lib/User.php index 7d0a9ef2..7b29b54c 100644 --- a/lib/User.php +++ b/lib/User.php @@ -28,7 +28,7 @@ class User { public function __construct(\JKingWeb\NewsSync\RuntimeData $data) { $this->data = $data; $driver = $data->conf->userDriver; - $this->u = $driver::create($data); + $this->u = new $driver($data); $this->authzSupported = $this->u->driverFunctions("authorize"); } diff --git a/lib/User/Driver.php b/lib/User/Driver.php index 36b02352..1f39f447 100644 --- a/lib/User/Driver.php +++ b/lib/User/Driver.php @@ -14,7 +14,7 @@ Interface Driver { const RIGHTS_GLOBAL_ADMIN = 100; // is completely unrestricted // returns an instance of a class implementing this interface. Implemented as a static method for consistency with database classes - static function create(\JKingWeb\NewsSync\RuntimeData $data): Driver; + function __construct(\JKingWeb\NewsSync\RuntimeData $data); // returns a human-friendly name for the driver (for display in installer, for example) static function driverName(): string; // returns an array (or single queried member of same) of methods defined by this interface and whether the class implements the internal function or a custom version