diff --git a/src/Runner/Application.php b/src/Runner/Application.php index e142742..0a19d0f 100644 --- a/src/Runner/Application.php +++ b/src/Runner/Application.php @@ -1,23 +1,139 @@ getArgc() > 1) { + $this->taskName = $this->getArgv()[1]; + return $this->runTask(); + } + + $this->printUsage(); + return ExitCode::ILLEGAL_OPTION; } private static function loadEnvironment () : void { $dotenv = new Dotenv(__DIR__); $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 [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 [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 index 0000000..27bbae8 --- /dev/null +++ b/src/Runner/TasksMap.php @@ -0,0 +1,27 @@ + 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 index f131b4d..9bd6f58 100644 --- a/src/Tasks/BootstrapSqliteRepository.php +++ b/src/Tasks/BootstrapSqliteRepository.php @@ -1,26 +1,46 @@ filename = $filename ?? $this->getDefaultFilename(); } + /// + /// Public methods + /// + public function run () : void { (new SqliteRepository($this->filename))->bootstrap(); } + public static function getUsage () : string { + + return <<run(); + } + } diff --git a/tests/Tasks/BootstrapSqliteRepositoryTest.php b/tests/Tasks/BootstrapSqliteRepositoryTest.php index 6e02aa1..ba0cccc 100644 --- a/tests/Tasks/BootstrapSqliteRepositoryTest.php +++ b/tests/Tasks/BootstrapSqliteRepositoryTest.php @@ -1,50 +1,51 @@ 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; + putenv("SAAS_SITES_DB=$this->filename"); // default file + $task = new BootstrapSqliteRepository(); $task->run(); $this->assertDatabaseHasBeenCreated($this->filename); } }