Changeset View
Changeset View
Standalone View
Standalone View
src/Tasks/CreateDatabase.php
- This file was added.
<?php | |||||
namespace Nasqueron\SAAS\PhpBB\Tasks; | |||||
use Nasqueron\SAAS\PhpBB\SitesRepository\Site; | |||||
use Nasqueron\SAAS\PhpBB\SitesRepository\SqliteRepository; | |||||
class CreateDatabase extends Task { | |||||
use WithMySQL; | |||||
/// | |||||
/// Private members | |||||
/// | |||||
/** | |||||
* @var string | |||||
*/ | |||||
private $id; | |||||
/** | |||||
* @var Site | |||||
*/ | |||||
private $site; | |||||
/** | |||||
* @var \PDO | |||||
*/ | |||||
private $db; | |||||
/// | |||||
/// Constructor | |||||
/// | |||||
public function __construct (string $id) { | |||||
$this->id = $id; | |||||
} | |||||
/// | |||||
/// Task methods | |||||
/// | |||||
public function run () : void { | |||||
$this->initialize(); | |||||
$this->createDatabase(); | |||||
} | |||||
private function initialize () : void { | |||||
$this->site = (new SqliteRepository)->get($this->id); | |||||
$this->db = $this->getMySQLDatabase(); | |||||
} | |||||
private function createDatabase () : void { | |||||
$this->getMySQLDatabase() | |||||
->prepare($this->getQueries()) | |||||
->execute([ | |||||
'db' => $this->site->getId(), | |||||
'user' => $this->site->getDatabaseUser(), | |||||
'pass' => $this->site->getDatabasePassword(), | |||||
]); | |||||
} | |||||
/// | |||||
/// Helper methods | |||||
/// | |||||
private function getQueries () : string { | |||||
$queries = <<<SQL | |||||
CREATE DATABASE IF NOT EXISTS %db%; | |||||
CREATE USER :user@'%' IDENTIFIED BY :pass; | |||||
GRANT ALL ON %db%.* TO :user@'%'; | |||||
FLUSH PRIVILEGES; | |||||
SQL; | |||||
return str_replace("%db%", $this->id, $queries); | |||||
} | |||||
public static function getUsage () : string { | |||||
return <<<DOC | |||||
%command% <id>: creates a MySQL database for the specified identifant | |||||
DOC; | |||||
} | |||||
} |
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator