Page MenuHomeDevCentral

SqliteRepository.php
No OneTemporary

SqliteRepository.php

<?php
namespace Nasqueron\SAAS\PhpBB\SitesRepository;
use Nasqueron\SAAS\PhpBB\Config\DefaultConfiguration;
use PDO;
class SqliteRepository extends Repository {
///
/// Private members
///
/**
* @var string
*/
private $filename;
/**
* @var PDO
*/
private $db = null;
///
/// Constructor
///
public function __construct (string $filename = null) {
$this->filename = $filename ?? DefaultConfiguration::getSqliteRepositoryFilename();
}
///
/// Tasks methods
///
public function bootstrap () : void {
$db = $this->getDB();
$queries = $this->getSchema();
foreach ($queries as $query) {
$db->query($query);
}
}
public function get (string $id) : Site {
$statement = $this->getDB()->prepare(
"SELECT * FROM sites WHERE id = ?"
);
$result = $statement->execute([ $id ]);
$row = $statement->fetch();
if ($row === false) {
throw new \InvalidArgumentException("Site $id not found.");
}
return (new Site())
->setId($row['id'])
->setHost($row['host'])
->setDatabaseUser($row['db_user'])
->setDatabasePassword($row['db_password'])
->setDatabasePrefix($row['db_prefix'])
->setActive((bool)$row['active']);
}
///
/// Helper methods
///
public function getDB() : PDO {
if ($this->db === null) {
if (!file_exists($this->filename)) {
touch($this->filename);
}
$dsn = self::getDataSourceName($this->filename);
$this->db = new PDO($dsn);
}
return $this->db;
}
private static function getDataSourceName (string $filename) : string {
return "sqlite:" . realpath($filename);
}
private function getSchema () : iterable {
yield <<<SQL
CREATE TABLE IF NOT EXISTS sites (
id text,
host text,
db_user text,
db_password text,
db_prefix text,
active boolean
);
SQL;
}
}

File Metadata

Mime Type
text/x-php
Expires
Fri, Nov 13, 19:25 (4 d, 22 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4258093
Default Alt Text
SqliteRepository.php (2 KB)

Event Timeline