Page MenuHomeDevCentral

Application.php
No OneTemporary

Application.php

<?php
namespace Nasqueron\SAAS\PhpBB\Runner;
use Keruald\Commands\Command;
use Keruald\Commands\ExitCode;
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();
if ($this->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 <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"]);
}
}

File Metadata

Mime Type
text/x-php
Expires
Wed, Mar 18, 11:26 (15 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3535285
Default Alt Text
Application.php (3 KB)

Event Timeline