Changeset View
Changeset View
Standalone View
Standalone View
src/Runner/Application.php
<?php | <?php | ||||
namespace Nasqueron\SAAS\PhpBB\Runner; | namespace Nasqueron\SAAS\PhpBB\Runner; | ||||
use Keruald\Commands\Command; | use Keruald\Commands\Command; | ||||
use Keruald\Commands\ExitCode; | use Keruald\Commands\ExitCode; | ||||
use Dotenv\Dotenv; | use Dotenv\Dotenv; | ||||
use InvalidArgumentException; | |||||
use BadMethodCallException; | |||||
use Exception; | |||||
class Application extends Command { | class Application extends Command { | ||||
/** | |||||
* @var string | |||||
*/ | |||||
private $taskName; | |||||
/** | |||||
* @var string | |||||
*/ | |||||
private $taskClassName = ""; | |||||
public function main () : int { | public function main () : int { | ||||
self::loadEnvironment(); | 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 { | private static function loadEnvironment () : void { | ||||
$dotenv = new Dotenv(__DIR__); | $dotenv = new Dotenv(__DIR__); | ||||
$dotenv->safeLoad(); | $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"]); | |||||
} | |||||
} | } |
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator