@@ -11,15 +11,15 @@ discard block |
||
| 11 | 11 | AppParser::run( |
| 12 | 12 | new class extends App { |
| 13 | 13 | |
| 14 | - public function setup(): Cmd { |
|
| 14 | + public function setup (): Cmd { |
|
| 15 | 15 | return Cmd::root() |
| 16 | - ->setCallable(static function (Cmd $cmd) { |
|
| 16 | + ->setCallable(static function(Cmd $cmd) { |
|
| 17 | 17 | $cmd->help(); |
| 18 | 18 | }) |
| 19 | 19 | ->addSubCmd( |
| 20 | 20 | Cmd::extend('show') |
| 21 | 21 | ->setDescription('This command is used to show something. Take a look at the subcommands.') |
| 22 | - ->setCallable(static function (Cmd $cmd) { |
|
| 22 | + ->setCallable(static function(Cmd $cmd) { |
|
| 23 | 23 | error_reporting(E_ALL); |
| 24 | 24 | $cmd->help(); |
| 25 | 25 | }) |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | 'isa' => 'string', |
| 32 | 32 | 'default' => 'World' |
| 33 | 33 | ]) |
| 34 | - ->setCallable(static function (Cmd $cmd) { |
|
| 34 | + ->setCallable(static function(Cmd $cmd) { |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | 37 | $name = $cmd->getProvidedOption('name'); |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | ->addSubCmd( |
| 46 | 46 | Cmd::extend('phpversion') |
| 47 | 47 | ->setDescription('Displays the current php version of your cli.') |
| 48 | - ->setCallable(static function (Cmd $cmd) { |
|
| 48 | + ->setCallable(static function(Cmd $cmd) { |
|
| 49 | 49 | self::eol(); |
| 50 | 50 | self::echo(' Your PHP version is:', Color::FOREGROUND_COLOR_YELLOW); |
| 51 | 51 | self::eol(); |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | AppParser::run( |
| 12 | 12 | (new class extends App { |
| 13 | 13 | |
| 14 | - public function setup(): Cmd { |
|
| 14 | + public function setup (): Cmd { |
|
| 15 | 15 | return Cmd::root() |
| 16 | 16 | ->addOption('v|verbose', [ |
| 17 | 17 | 'description' => 'Flag to enable verbose output' |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | 'isa' => 'string', |
| 22 | 22 | 'default' => 'World' |
| 23 | 23 | ]) |
| 24 | - ->setCallable(static function (Cmd $cmd) { |
|
| 24 | + ->setCallable(static function(Cmd $cmd) { |
|
| 25 | 25 | |
| 26 | 26 | $name = $cmd->getProvidedOption('name'); |
| 27 | 27 | |
@@ -15,19 +15,19 @@ |
||
| 15 | 15 | * CmdRunner constructor. |
| 16 | 16 | * @param Cmd $cmd |
| 17 | 17 | */ |
| 18 | - public function __construct(Cmd $cmd) { |
|
| 18 | + public function __construct (Cmd $cmd) { |
|
| 19 | 19 | $this->cmd = $cmd; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @return Cmd |
| 24 | 24 | */ |
| 25 | - public function cmd(): Cmd { |
|
| 25 | + public function cmd (): Cmd { |
|
| 26 | 26 | return $this->cmd; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Run the cmd |
| 31 | 31 | */ |
| 32 | - abstract public function run(): void; |
|
| 32 | + abstract public function run (): void; |
|
| 33 | 33 | } |
| 34 | 34 | \ No newline at end of file |
@@ -11,13 +11,13 @@ |
||
| 11 | 11 | /** |
| 12 | 12 | * @return Cmd |
| 13 | 13 | */ |
| 14 | - abstract public function setup(): Cmd; |
|
| 14 | + abstract public function setup (): Cmd; |
|
| 15 | 15 | |
| 16 | - public static function eol(): void { |
|
| 16 | + public static function eol (): void { |
|
| 17 | 17 | echo self::EOL; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - public static function echo(string $str, ?string $foreground_color = null): void { |
|
| 20 | + public static function echo(string $str, ?string $foreground_color = null) : void { |
|
| 21 | 21 | |
| 22 | 22 | $lines = preg_split("/\r\n|\n|\r/", $str); |
| 23 | 23 | |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | class AppParser { |
| 12 | 12 | |
| 13 | - public static function run(App $app): void { |
|
| 13 | + public static function run (App $app): void { |
|
| 14 | 14 | global $argv; |
| 15 | 15 | |
| 16 | 16 | try { |
@@ -22,11 +22,11 @@ discard block |
||
| 22 | 22 | return; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - if(self::callCallable($cmd)) { |
|
| 25 | + if (self::callCallable($cmd)) { |
|
| 26 | 26 | return; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - if(self::callMethod($app, $cmd)) { |
|
| 29 | + if (self::callMethod($app, $cmd)) { |
|
| 30 | 30 | return; |
| 31 | 31 | } |
| 32 | 32 | |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @return bool |
| 48 | 48 | * @throws Exception |
| 49 | 49 | */ |
| 50 | - private static function callCallable(Cmd $cmd): bool { |
|
| 50 | + private static function callCallable (Cmd $cmd): bool { |
|
| 51 | 51 | |
| 52 | 52 | $callable = $cmd->getCallable(); |
| 53 | 53 | |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * @return bool |
| 66 | 66 | * @throws Exception |
| 67 | 67 | */ |
| 68 | - private static function callMethod(App $app, Cmd $cmd): bool { |
|
| 68 | + private static function callMethod (App $app, Cmd $cmd): bool { |
|
| 69 | 69 | |
| 70 | 70 | $methodName = $cmd->getMethodName(); |
| 71 | 71 | |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | * @param array $argv |
| 83 | 83 | * @return Cmd |
| 84 | 84 | */ |
| 85 | - public static function route(App $app, array $argv): Cmd { |
|
| 85 | + public static function route (App $app, array $argv): Cmd { |
|
| 86 | 86 | |
| 87 | 87 | $cmd = $app->setup(); |
| 88 | 88 | |
@@ -105,12 +105,12 @@ discard block |
||
| 105 | 105 | if ($cmd->hasSubCmd($currentArgument)) { |
| 106 | 106 | $_cmd = $cmd->getSubCmd($currentArgument); |
| 107 | 107 | |
| 108 | - if($_cmd !== null) { |
|
| 108 | + if ($_cmd !== null) { |
|
| 109 | 109 | $subcommand = $_cmd; |
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - if($subcommand !== null) { |
|
| 113 | + if ($subcommand !== null) { |
|
| 114 | 114 | |
| 115 | 115 | $cmd = $subcommand; |
| 116 | 116 | $parser->advance(); |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | class HelpRunner extends CmdRunner { |
| 8 | 8 | |
| 9 | 9 | |
| 10 | - public function run(): void { |
|
| 10 | + public function run (): void { |
|
| 11 | 11 | $this->displayHeader(); |
| 12 | 12 | $this->displayUsage(); |
| 13 | 13 | $this->displayOptions(); |
| 14 | 14 | $this->displaySubcommands(); |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | - public function displayHeader(): void { |
|
| 17 | + public function displayHeader (): void { |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | $help = <<<EOT |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | App::eol(); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | - public function displayUsage(): void { |
|
| 35 | + public function displayUsage (): void { |
|
| 36 | 36 | |
| 37 | 37 | $cmd = $this->cmd(); |
| 38 | 38 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | App::eol(); |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - public function displayOptions(): void { |
|
| 58 | + public function displayOptions (): void { |
|
| 59 | 59 | |
| 60 | 60 | $cmd = $this->cmd(); |
| 61 | 61 | |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | foreach ($oc->options as $option) { |
| 72 | 72 | |
| 73 | 73 | $s = ' '; |
| 74 | - if(!empty($option->short)) { |
|
| 74 | + if (!empty($option->short)) { |
|
| 75 | 75 | $s = '-' . $option->short . ', '; |
| 76 | 76 | } |
| 77 | 77 | $s .= '--' . $option->long; |
@@ -95,13 +95,13 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - public function displaySubcommands(): void { |
|
| 98 | + public function displaySubcommands (): void { |
|
| 99 | 99 | |
| 100 | 100 | $cmd = $this->cmd(); |
| 101 | 101 | |
| 102 | 102 | $has_subcommands = !empty($cmd->subcommands); |
| 103 | 103 | |
| 104 | - if($has_subcommands) { |
|
| 104 | + if ($has_subcommands) { |
|
| 105 | 105 | |
| 106 | 106 | App::eol(); |
| 107 | 107 | App::echo('Available commands: ', Color::FOREGROUND_COLOR_YELLOW); |
@@ -62,27 +62,27 @@ discard block |
||
| 62 | 62 | private $callable; |
| 63 | 63 | |
| 64 | 64 | |
| 65 | - public function __construct(string $cmd) { |
|
| 65 | + public function __construct (string $cmd) { |
|
| 66 | 66 | $this->cmd = $cmd; |
| 67 | 67 | if ($cmd !== 'help') { |
| 68 | 68 | $this->addSubCmd( |
| 69 | 69 | self::extend('help') |
| 70 | 70 | ->setDescription('Displays help for this command.') |
| 71 | - ->setCallable(static function (Cmd $cmd) { |
|
| 71 | + ->setCallable(static function(Cmd $cmd) { |
|
| 72 | 72 | $cmd->parent->help(); |
| 73 | 73 | }) |
| 74 | 74 | ); |
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - public function addOption(string $specString, array $config): self { |
|
| 78 | + public function addOption (string $specString, array $config): self { |
|
| 79 | 79 | |
| 80 | 80 | $this->optionSpecs[$specString] = $config; |
| 81 | 81 | |
| 82 | 82 | return $this; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public function addSubCmd(Cmd $cmd): self { |
|
| 85 | + public function addSubCmd (Cmd $cmd): self { |
|
| 86 | 86 | |
| 87 | 87 | $cmd->parent = $this; |
| 88 | 88 | $this->subcommands[$cmd->cmd] = $cmd; |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | return $this; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - public function setDescription(string $descr): self { |
|
| 93 | + public function setDescription (string $descr): self { |
|
| 94 | 94 | |
| 95 | 95 | $this->descr = $descr; |
| 96 | 96 | |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | * @param callable $callable |
| 102 | 102 | * @return $this |
| 103 | 103 | */ |
| 104 | - public function setCallable(callable $callable): self { |
|
| 104 | + public function setCallable (callable $callable): self { |
|
| 105 | 105 | |
| 106 | 106 | try { |
| 107 | 107 | $this->callable = $this->validateCallable($callable); |
@@ -114,29 +114,29 @@ discard block |
||
| 114 | 114 | return $this; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - public function hasSubCmd(string $cmd): bool { |
|
| 117 | + public function hasSubCmd (string $cmd): bool { |
|
| 118 | 118 | return array_key_exists($cmd, $this->subcommands); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - public function hasProvidedOption(string $key): bool { |
|
| 121 | + public function hasProvidedOption (string $key): bool { |
|
| 122 | 122 | return $this->optionResult !== null && $this->optionResult->has($key); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - public function getProvidedOption(string $key) { |
|
| 125 | + public function getProvidedOption (string $key) { |
|
| 126 | 126 | if ($this->optionResult !== null) { |
| 127 | 127 | return $this->optionResult->get($key); |
| 128 | 128 | } |
| 129 | 129 | return null; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - public function getSubCmd(string $cmd): ?Cmd { |
|
| 132 | + public function getSubCmd (string $cmd): ?Cmd { |
|
| 133 | 133 | if ($this->hasSubCmd($cmd)) { |
| 134 | 134 | return $this->subcommands[$cmd]; |
| 135 | 135 | } |
| 136 | 136 | return null; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - public function getMethodName(): string { |
|
| 139 | + public function getMethodName (): string { |
|
| 140 | 140 | $cmd = $this; |
| 141 | 141 | $pwd = []; |
| 142 | 142 | |
@@ -155,11 +155,11 @@ discard block |
||
| 155 | 155 | return lcfirst($pwd_str); |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - public function getCallable(): ?callable { |
|
| 158 | + public function getCallable (): ?callable { |
|
| 159 | 159 | return $this->callable; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - public function getOptionCollection(): OptionCollection { |
|
| 162 | + public function getOptionCollection (): OptionCollection { |
|
| 163 | 163 | |
| 164 | 164 | if ($this->optionCollection !== null) { |
| 165 | 165 | return $this->optionCollection; |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | return $this->optionCollection; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | - public function handleOptionParseException(): bool { |
|
| 186 | + public function handleOptionParseException (): bool { |
|
| 187 | 187 | |
| 188 | 188 | if ($this->optionParseException === null) { |
| 189 | 189 | return false; |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | return true; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - public function help(): void { |
|
| 203 | + public function help (): void { |
|
| 204 | 204 | (new HelpRunner($this))->run(); |
| 205 | 205 | } |
| 206 | 206 | |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | * @return callable |
| 211 | 211 | * @throws Exception |
| 212 | 212 | */ |
| 213 | - private function validateCallable(callable $callable): callable { |
|
| 213 | + private function validateCallable (callable $callable): callable { |
|
| 214 | 214 | |
| 215 | 215 | $check = new ReflectionFunction($callable); |
| 216 | 216 | $parameters = $check->getParameters(); |
@@ -235,12 +235,12 @@ discard block |
||
| 235 | 235 | return $callable; |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - public static function extend(string $cmd): Cmd { |
|
| 238 | + public static function extend (string $cmd): Cmd { |
|
| 239 | 239 | return new class($cmd) extends Cmd { |
| 240 | 240 | }; |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | - public static function root(): Cmd { |
|
| 243 | + public static function root (): Cmd { |
|
| 244 | 244 | return self::extend('__root'); |
| 245 | 245 | } |
| 246 | 246 | } |
| 247 | 247 | \ No newline at end of file |