Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3783552
D1571.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D1571.diff
View Options
diff --git a/.env.example b/.env.example
new file mode 100644
--- /dev/null
+++ b/.env.example
@@ -0,0 +1 @@
+SAAS_SITES_DB=/tmp/sites.db
diff --git a/.phan/config.php b/.phan/config.php
--- a/.phan/config.php
+++ b/.phan/config.php
@@ -273,6 +273,7 @@
'vendor/keruald/commands/src',
'vendor/phan/phan/src/Phan',
'vendor/phpunit/phpunit/src',
+ 'vendor/vlucas/phpdotenv/src',
],
// A list of individual files to include in analysis
diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -10,7 +10,8 @@
}
],
"require": {
- "keruald/commands": "^0.0.1"
+ "keruald/commands": "^0.0.1",
+ "vlucas/phpdotenv": "dev-master"
},
"require-dev": {
"nasqueron/codestyle": "0.0.1",
diff --git a/src/Runner/Application.php b/src/Runner/Application.php
--- a/src/Runner/Application.php
+++ b/src/Runner/Application.php
@@ -5,10 +5,19 @@
use Keruald\Commands\Command;
use Keruald\Commands\ExitCode;
+use Dotenv\Dotenv;
+
class Application extends Command {
public function main () : int {
+ self::loadEnvironment();
+
return ExitCode::SUCCESS;
}
+ private static function loadEnvironment () : void {
+ $dotenv = new Dotenv(__DIR__);
+ $dotenv->safeLoad();
+ }
+
}
diff --git a/src/SitesRepository/Repository.php b/src/SitesRepository/Repository.php
new file mode 100644
--- /dev/null
+++ b/src/SitesRepository/Repository.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\SitesRepository;
+
+abstract class Repository {
+
+}
diff --git a/src/SitesRepository/SqliteRepository.php b/src/SitesRepository/SqliteRepository.php
new file mode 100644
--- /dev/null
+++ b/src/SitesRepository/SqliteRepository.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\SitesRepository;
+
+use PDO;
+
+class SqliteRepository extends Repository {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * @var string
+ */
+ private $filename;
+
+ /**
+ * @var PDO
+ */
+ private $db = null;
+
+ ///
+ /// Constructor
+ ///
+
+ public function __construct (string $filename) {
+ $this->filename = $filename;
+ }
+
+ ///
+ /// Tasks methods
+ ///
+
+ public function bootstrap () : void {
+ $db = $this->getDB();
+
+ $queries = $this->getSchema();
+ foreach ($queries as $query) {
+ $db->query($query);
+ }
+ }
+
+ ///
+ /// 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;
+
+ }
+
+}
diff --git a/src/Tasks/BootstrapSqliteRepository.php b/src/Tasks/BootstrapSqliteRepository.php
new file mode 100644
--- /dev/null
+++ b/src/Tasks/BootstrapSqliteRepository.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\Tasks;
+
+use Nasqueron\SAAS\PhpBB\SitesRepository\SqliteRepository;
+
+class BootstrapSqliteRepository extends Task {
+
+ /**
+ * @var string
+ */
+ private $filename;
+
+ public function __construct (?string $filename = null) {
+ $this->filename = $filename ?? $this->getDefaultFilename();
+ }
+
+ public function run () : void {
+ (new SqliteRepository($this->filename))->bootstrap();
+ }
+
+ private function getDefaultFilename() : string {
+ return $_ENV["SAAS_SITES_DB"];
+ }
+
+}
diff --git a/src/Tasks/Task.php b/src/Tasks/Task.php
new file mode 100644
--- /dev/null
+++ b/src/Tasks/Task.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\Tasks;
+
+abstract class Task {
+
+ abstract public function run () : void;
+
+}
diff --git a/tests/SitesRepository/SqliteRepositoryTest.php b/tests/SitesRepository/SqliteRepositoryTest.php
new file mode 100644
--- /dev/null
+++ b/tests/SitesRepository/SqliteRepositoryTest.php
@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+
+namespace Nasqueron\SAAS\PhpBB\Tests\SitesRepository;
+
+use Nasqueron\SAAS\PhpBB\SitesRepository\SqliteRepository;
+use Nasqueron\SAAS\PhpBB\Tests\Utilities\WithSqliteAssertions;
+use PHPUnit\Framework\TestCase;
+
+final class SqliteRepositoryTest extends TestCase {
+
+ use WithSqliteAssertions;
+
+ public function testBootstrap () : void {
+ $filename = tempnam("", "test-db-");
+ $repository = new SqliteRepository($filename);
+ $repository->bootstrap();
+
+ $this->assertDatabaseHasBeenCreated($filename);
+
+ unlink($filename);
+ }
+
+}
diff --git a/tests/Tasks/BootstrapSqliteRepositoryTest.php b/tests/Tasks/BootstrapSqliteRepositoryTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Tasks/BootstrapSqliteRepositoryTest.php
@@ -0,0 +1,50 @@
+<?php
+declare(strict_types=1);
+
+namespace Nasqueron\SAAS\PhpBB\Tests\Tasks;
+
+use Nasqueron\SAAS\PhpBB\Tasks\BootstrapSqliteRepository;
+use Nasqueron\SAAS\PhpBB\Tests\Utilities\WithSqliteAssertions;
+use PHPUnit\Framework\TestCase;
+
+final class BootstrapSqliteRepositoryTest extends TestCase {
+
+ use WithSqliteAssertions;
+
+ /**
+ * @var string
+ */
+ private $filename;
+
+ ///
+ /// Fixtures
+ ///
+
+ public function setUp () {
+ $this->filename = tempnam("", "test-db-");
+ }
+
+ public function tearDown () {
+ unlink($this->filename);
+ }
+
+ ///
+ /// Tests
+ ///
+
+ public function testBootstrap () : void {
+ $task = new BootstrapSqliteRepository($this->filename);
+ $task->run();
+
+ $this->assertDatabaseHasBeenCreated($this->filename);
+ }
+
+ public function testBootstrapWithDefaultFile () : void {
+ $_ENV["SAAS_SITES_DB"] = $this->filename;
+ $task = new BootstrapSqliteRepository();
+ $task->run();
+
+ $this->assertDatabaseHasBeenCreated($this->filename);
+ }
+
+}
diff --git a/tests/Utilities/WithSqliteAssertions.php b/tests/Utilities/WithSqliteAssertions.php
new file mode 100644
--- /dev/null
+++ b/tests/Utilities/WithSqliteAssertions.php
@@ -0,0 +1,21 @@
+<?php
+declare(strict_types=1);
+
+namespace Nasqueron\SAAS\PhpBB\Tests\Utilities;
+
+use PHPUnit\Framework\Assert;
+
+trait WithSqliteAssertions {
+
+ public function assertDatabaseHasBeenCreated (string $filename) : void {
+ Assert::assertFileExists($filename);
+
+ // Checks the file isn't empty (ie some tables and perhaps data exist)
+ Assert::assertStringNotEqualsFile(
+ $filename,
+ '',
+ "Database file has been created but schema wasn't applied."
+ );
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 27, 03:45 (18 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2265801
Default Alt Text
D1571.diff (6 KB)
Attached To
Mode
D1571: Provide task to create a new sites SQLite database
Attached
Detach File
Event Timeline
Log In to Comment