Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F5890222
D1573.id4012.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
D1573.id4012.diff
View Options
diff --git a/src/Config/DefaultConfiguration.php b/src/Config/DefaultConfiguration.php
new file mode 100644
--- /dev/null
+++ b/src/Config/DefaultConfiguration.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Nasqueron\SaaS\PhpBB\Config;
+
+class DefaultConfiguration {
+
+ ///
+ /// Sites repository
+ ///
+
+ public static function getSqliteRepositoryFilename() : string {
+ return getenv("SAAS_SITES_DB");
+ }
+
+ ///
+ /// MySQL database
+ ///
+
+ public static function getMySQLDatasourceName() : string {
+ $info = self::getMySQLConnectionInformation();
+
+ return "mysql:host=$info[host];port=$info[port];charset=utf8";
+ }
+
+ public static function getMySQLConnectionInformation() : array {
+ return [
+ 'host' => $_ENV['DB_PORT_3306_TCP_ADDR'],
+ 'port' => $_ENV['DB_PORT_3306_TCP_PORT'],
+ 'user' => 'root',
+ 'password' => $_ENV['DB_ENV_MYSQL_ROOT_PASSWORD'],
+ ];
+ }
+
+}
diff --git a/src/Runner/TasksMap.php b/src/Runner/TasksMap.php
--- a/src/Runner/TasksMap.php
+++ b/src/Runner/TasksMap.php
@@ -5,11 +5,13 @@
use InvalidArgumentException;
use Nasqueron\SAAS\PhpBB\Tasks\BootstrapSqliteRepository;
+use Nasqueron\SAAS\PhpBB\Tasks\CreateDatabase;
class TasksMap {
public static function getMap () : array {
return [
+ 'sites:createdb' => CreateDatabase::class,
'sites:bootstrap' => BootstrapSqliteRepository::class,
];
}
diff --git a/src/SitesRepository/Repository.php b/src/SitesRepository/Repository.php
--- a/src/SitesRepository/Repository.php
+++ b/src/SitesRepository/Repository.php
@@ -4,4 +4,6 @@
abstract class Repository {
+ abstract public function get (string $id) : Site;
+
}
diff --git a/src/SitesRepository/Site.php b/src/SitesRepository/Site.php
new file mode 100644
--- /dev/null
+++ b/src/SitesRepository/Site.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\SitesRepository;
+
+class Site {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * @var string
+ */
+ private $id;
+
+ /**
+ * @var string
+ */
+ private $host;
+
+ /**
+ * @var string
+ */
+ private $databaseUser;
+
+ /**
+ * @var string
+ */
+ private $databasePassword;
+
+ /**
+ * @var string
+ */
+ private $databasePrefix;
+
+ /**
+ * @var bool
+ */
+ private $active;
+
+ ///
+ /// Getters and setters
+ ///
+
+ public function getId () : string {
+ return $this->id;
+ }
+
+ public function setId (string $id) : Site {
+ $this->id = $id;
+
+ return $this;
+ }
+
+ public function getHost () : string {
+ return $this->host;
+ }
+
+ public function setHost (string $host) : Site {
+ $this->host = $host;
+
+ return $this;
+ }
+
+ public function getDatabaseUser () : string {
+ return $this->databaseUser;
+ }
+
+ public function setDatabaseUser (string $databaseUser) : Site {
+ $this->databaseUser = $databaseUser;
+
+ return $this;
+ }
+
+ public function getDatabasePassword () : string {
+ return $this->databasePassword;
+ }
+
+ public function setDatabasePassword (string $databasePassword) : Site {
+ $this->databasePassword = $databasePassword;
+
+ return $this;
+ }
+
+ public function getDatabasePrefix () : string {
+ return $this->databasePrefix;
+ }
+
+ public function setDatabasePrefix (string $databasePrefix) : Site {
+ $this->databasePrefix = $databasePrefix;
+
+ return $this;
+ }
+
+ public function isActive () : bool {
+ return $this->active;
+ }
+
+ public function setActive (bool $active) : Site {
+ $this->active = $active;
+
+ return $this;
+ }
+
+}
diff --git a/src/SitesRepository/SqliteRepository.php b/src/SitesRepository/SqliteRepository.php
--- a/src/SitesRepository/SqliteRepository.php
+++ b/src/SitesRepository/SqliteRepository.php
@@ -41,6 +41,26 @@
}
}
+ 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
///
diff --git a/src/Tasks/BootstrapSqliteRepository.php b/src/Tasks/BootstrapSqliteRepository.php
--- a/src/Tasks/BootstrapSqliteRepository.php
+++ b/src/Tasks/BootstrapSqliteRepository.php
@@ -35,12 +35,4 @@
}
- ///
- /// Helper methods
- ///
-
- private function getDefaultFilename() : string {
- return getenv("SAAS_SITES_DB");
- }
-
}
diff --git a/src/Tasks/CreateDatabase.php b/src/Tasks/CreateDatabase.php
new file mode 100644
--- /dev/null
+++ b/src/Tasks/CreateDatabase.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\Tasks;
+
+use Nasqueron\SaaS\PhpBB\Config\DefaultConfiguration;
+use Nasqueron\SAAS\PhpBB\SitesRepository\Site;
+use Nasqueron\SAAS\PhpBB\SitesRepository\SqliteRepository;
+
+use PDO;
+
+class CreateDatabase extends Task {
+
+ /**
+ * @var string
+ */
+ private $id;
+
+ /**
+ * @var Site
+ */
+ private $site;
+
+ /**
+ * @var PDO
+ */
+ private $db;
+
+ public function __construct (string $id) {
+ $this->id = $id;
+ }
+
+ ///
+ /// Public methods
+ ///
+
+ public function run () : void {
+ $this->initialize();
+
+ $statement = $this->getDB()->prepare($this->getQueries());
+ $statement->bindParam(':db', $this->site->getId());
+ $statement->bindParam(':user', $this->site->getDatabaseUser());
+ $statement->bindParam(':pass', $this->site->getDatabasePassword());
+
+ $statement->execute();
+ }
+
+ private function initialize () : void {
+ $this->site = SqliteRepository::fromDefaultFile()->get($this->id);
+ $this->db = $this->getMySQLDatabase();
+ }
+
+ private function getMySQLDatabase() : PDO {
+ $info = DefaultConfiguration::getMySQLConnectionInformation();
+
+ return new PDO(
+ DefaultConfiguration::getMySQLDatasourceName(),
+ $info['user'],
+ $info['password']
+ );
+ }
+
+ private function getQueries () : string {
+ return <<<SQL
+CREATE DATABASE :db;
+CREATE USER :user@'%' IDENTIFIED BY :pass;
+GRANT ALL ON :db.* TO :user@'%';
+FLUSH PRIVILEGES;
+SQL;
+ }
+
+ public static function getUsage () : string {
+ return <<<DOC
+%command% <id>: creates a MySQL database for the specified identifant
+DOC;
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Mar 18, 01:23 (20 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2494043
Default Alt Text
D1573.id4012.diff (6 KB)
Attached To
Mode
D1573: Allow to create MySQL database
Attached
Detach File
Event Timeline
Log In to Comment