Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F5889740
D1572.id4009.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D1572.id4009.diff
View Options
diff --git a/src/Runner/Application.php b/src/Runner/Application.php
--- a/src/Runner/Application.php
+++ b/src/Runner/Application.php
@@ -7,12 +7,33 @@
use Dotenv\Dotenv;
+use InvalidArgumentException;
+use BadMethodCallException;
+use Exception;
+
class Application extends Command {
+ /**
+ * @var string
+ */
+ private $taskName;
+
+ /**
+ * @var string
+ */
+ private $taskClassName = "";
+
public function main () : int {
+
self::loadEnvironment();
- return ExitCode::SUCCESS;
+ if ($this->getArgc() > 1) {
+ $this->taskName = $this->getArgv()[1];
+ return $this->runTask();
+ }
+
+ $this->printUsage();
+ return ExitCode::ILLEGAL_OPTION;
}
private static function loadEnvironment () : void {
@@ -20,4 +41,99 @@
$dotenv->safeLoad();
}
+ ///
+ /// Runner helper methods
+ ///
+
+ private function runTask () : int {
+ try {
+ $this->resolveTaskName();
+ } catch (InvalidArgumentException $exception) {
+ $this->getDisplay()->error("Unknown command " . $this->taskName);
+ return ExitCode::ILLEGAL_OPTION;
+ }
+
+ try {
+ $this->callTaskRunner();
+ } catch (BadMethodCallException $ex) {
+ $this->printUsageForTask();
+ return ExitCode::ILLEGAL_OPTION;
+ } catch (Exception $ex) {
+ $this->handleTaskException($ex);
+ return ExitCode::FAILURE;
+ }
+
+ return ExitCode::SUCCESS;
+ }
+
+ /**
+ * Finds if a tasks with this name exists, and resolves class name.
+ *
+ * @throws \InvalidArgumentException
+ */
+ private function resolveTaskName () : void {
+ $this->taskClassName = TasksMap::getTaskClassName($this->taskName);
+ }
+
+ /**
+ * @throws \BadMethodCallException
+ * @throws \Exception
+ */
+ private function callTaskRunner () : void {
+ forward_static_call(
+ [$this->taskClassName, "runWithArgs"],
+ $this->getTaskArguments()
+ );
+ }
+
+ private function getTaskArguments () : array {
+ // At this stage, the argv array contains
+ // saas-phpbb <task name> [arguments for the tasks]
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~
+ // ^ the part we return
+
+ return array_slice($this->getArgv(), 2);
+ }
+
+ ///
+ /// Task errors methods
+ ///
+
+ private function handleTaskException (Exception $ex) : void {
+ $this->printTaskError($ex->getMessage());
+ }
+
+ private function printTaskError (string $message) : void {
+ $this->getDisplay()->error(
+ $this->getTaskPrefix() . ": " . $message
+ );
+ }
+
+ private function getTaskPrefix () : string {
+ return $this->getCommandName() . " " . $this->taskName;
+ }
+
+ ///
+ /// Usage helper methods
+ ///
+
+ private function printUsage () {
+ $name = $this->getCommandName();
+ $this->getDisplay()->error("Usage: $name <task> [arguments]");
+ }
+
+ private function printUsageForTask () {
+ $usage = $this->getUsageForTask();
+ $usage = str_replace('%command%', $this->taskName, $usage);
+ $this->getDisplay()->error($usage);
+ }
+
+ private function getUsageForTask () : string {
+ if ($this->taskClassName === "") {
+ $this->resolveTaskName();
+ }
+
+ return forward_static_call([$this->taskClassName, "getUsage"]);
+ }
+
}
diff --git a/src/Runner/TasksMap.php b/src/Runner/TasksMap.php
new file mode 100644
--- /dev/null
+++ b/src/Runner/TasksMap.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Nasqueron\SAAS\PhpBB\Runner;
+
+use InvalidArgumentException;
+
+use Nasqueron\SAAS\PhpBB\Tasks\BootstrapSqliteRepository;
+
+class TasksMap {
+
+ public static function getMap () : array {
+ return [
+ 'sites:bootstrap' => BootstrapSqliteRepository::class,
+ ];
+ }
+
+ public static function getTaskClassName (string $command) {
+ foreach (self::getMap() as $taskCommand => $taskClassName) {
+ if ($command === $taskCommand) {
+ return $taskClassName;
+ }
+ }
+
+ throw new InvalidArgumentException;
+ }
+
+}
diff --git a/src/Tasks/BootstrapSqliteRepository.php b/src/Tasks/BootstrapSqliteRepository.php
--- a/src/Tasks/BootstrapSqliteRepository.php
+++ b/src/Tasks/BootstrapSqliteRepository.php
@@ -15,13 +15,32 @@
$this->filename = $filename ?? $this->getDefaultFilename();
}
+ ///
+ /// Public methods
+ ///
+
public function run () : void {
(new SqliteRepository($this->filename))->bootstrap();
}
+ public static function getUsage () : string {
+
+ return <<<DOC
+%command% [path to site database]: creates a database with the required table
+
+If the path is omitted, the path is extracted from the environment.
+
+You can set the default path setting SAAS_SITES_DB environment variable.
+DOC;
+
+ }
+
+ ///
+ /// Helper methods
+ ///
private function getDefaultFilename() : string {
- return $_ENV["SAAS_SITES_DB"];
+ return getenv("SAAS_SITES_DB");
}
}
diff --git a/src/Tasks/Task.php b/src/Tasks/Task.php
--- a/src/Tasks/Task.php
+++ b/src/Tasks/Task.php
@@ -11,7 +11,7 @@
/// Usage methods
///
- abstract public function getUsage() : string;
+ abstract public static function getUsage () : string;
///
/// Runner methods
diff --git a/tests/Tasks/BootstrapSqliteRepositoryTest.php b/tests/Tasks/BootstrapSqliteRepositoryTest.php
--- a/tests/Tasks/BootstrapSqliteRepositoryTest.php
+++ b/tests/Tasks/BootstrapSqliteRepositoryTest.php
@@ -40,7 +40,8 @@
}
public function testBootstrapWithDefaultFile () : void {
- $_ENV["SAAS_SITES_DB"] = $this->filename;
+ putenv("SAAS_SITES_DB=$this->filename"); // default file
+
$task = new BootstrapSqliteRepository();
$task->run();
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Mar 17, 23:29 (16 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2493915
Default Alt Text
D1572.id4009.diff (5 KB)
Attached To
Mode
D1572: Implement task runner
Attached
Detach File
Event Timeline
Log In to Comment