@@ -7,7 +7,6 @@ |
||
| 7 | 7 | use Tarsana\IO\Interfaces\Resource\Writer as WriterInterface; |
| 8 | 8 | use Tarsana\IO\Resource\Reader; |
| 9 | 9 | use Tarsana\IO\Resource\Writer; |
| 10 | -use Tarsana\Syntax\Syntax; |
|
| 11 | 10 | |
| 12 | 11 | class Console implements ConsoleInterface { |
| 13 | 12 | |
@@ -18,8 +18,9 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | public function print(\Exception $e) : string |
| 20 | 20 | { |
| 21 | - if ($e instanceof ParseException) |
|
| 22 | - return $this->printParseException($e); |
|
| 21 | + if ($e instanceof ParseException) { |
|
| 22 | + return $this->printParseException($e); |
|
| 23 | + } |
|
| 23 | 24 | |
| 24 | 25 | return "<error>{$e}</error>"; |
| 25 | 26 | } |
@@ -36,10 +37,12 @@ discard block |
||
| 36 | 37 | $error = ''; |
| 37 | 38 | if ($syntax instanceof ObjectSyntax) { |
| 38 | 39 | $i = $e->extra(); |
| 39 | - if ($i['type'] == 'invalid-field') |
|
| 40 | - $error = "{$i['field']} is invalid!"; |
|
| 41 | - if ($i['type'] == 'missing-field') |
|
| 42 | - $error = "{$i['field']} is missing!"; |
|
| 40 | + if ($i['type'] == 'invalid-field') { |
|
| 41 | + $error = "{$i['field']} is invalid!"; |
|
| 42 | + } |
|
| 43 | + if ($i['type'] == 'missing-field') { |
|
| 44 | + $error = "{$i['field']} is missing!"; |
|
| 45 | + } |
|
| 43 | 46 | if ($i['type'] == 'additional-items') { |
| 44 | 47 | $items = implode(', ', $i['items']); |
| 45 | 48 | $error = "additional items {$items}"; |
@@ -48,8 +51,9 @@ discard block |
||
| 48 | 51 | $syntax = $this->printSyntax($e->syntax()); |
| 49 | 52 | |
| 50 | 53 | $output = "<reset>Failed to parse <warn>'{$e->input()}'</warn> as <info>{$syntax}</info>"; |
| 51 | - if ('' != $error) |
|
| 52 | - $output .= " <error>{$error}</error>"; |
|
| 54 | + if ('' != $error) { |
|
| 55 | + $output .= " <error>{$error}</error>"; |
|
| 56 | + } |
|
| 53 | 57 | |
| 54 | 58 | $previous = $e->previous(); |
| 55 | 59 | if ($previous) { |
@@ -62,11 +66,15 @@ discard block |
||
| 62 | 66 | protected function printSyntax(Syntax $s, bool $short = false) : string |
| 63 | 67 | { |
| 64 | 68 | if ($s instanceof ObjectSyntax) { |
| 65 | - if ($short) return 'object'; |
|
| 69 | + if ($short) { |
|
| 70 | + return 'object'; |
|
| 71 | + } |
|
| 66 | 72 | return implode($s->separator(), array_keys($s->fields())); |
| 67 | 73 | } |
| 68 | 74 | if ($s instanceof ArraySyntax) { |
| 69 | - if ($short) return 'array'; |
|
| 75 | + if ($short) { |
|
| 76 | + return 'array'; |
|
| 77 | + } |
|
| 70 | 78 | return $this->printSyntax($s->syntax()).$s->separator().'...'; |
| 71 | 79 | } |
| 72 | 80 | return (string) $s; |
@@ -35,8 +35,9 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | public static function create(callable $action = null) { |
| 37 | 37 | $command = new Command; |
| 38 | - if (null !== $action) |
|
| 39 | - $command->action($action); |
|
| 38 | + if (null !== $action) { |
|
| 39 | + $command->action($action); |
|
| 40 | + } |
|
| 40 | 41 | return $command; |
| 41 | 42 | } |
| 42 | 43 | |
@@ -122,8 +123,9 @@ discard block |
||
| 122 | 123 | */ |
| 123 | 124 | public function syntax(string $syntax = null) |
| 124 | 125 | { |
| 125 | - if (null === $syntax) |
|
| 126 | - return $this->syntax; |
|
| 126 | + if (null === $syntax) { |
|
| 127 | + return $this->syntax; |
|
| 128 | + } |
|
| 127 | 129 | |
| 128 | 130 | $this->syntax = S::syntax()->parse("{{$syntax}| }"); |
| 129 | 131 | return $this; |
@@ -142,8 +144,9 @@ discard block |
||
| 142 | 144 | } |
| 143 | 145 | |
| 144 | 146 | $this->options = []; |
| 145 | - foreach($options as $option) |
|
| 146 | - $this->options[$option] = false; |
|
| 147 | + foreach($options as $option) { |
|
| 148 | + $this->options[$option] = false; |
|
| 149 | + } |
|
| 147 | 150 | |
| 148 | 151 | return $this; |
| 149 | 152 | } |
@@ -156,8 +159,9 @@ discard block |
||
| 156 | 159 | */ |
| 157 | 160 | public function option(string $name) |
| 158 | 161 | { |
| 159 | - if (!array_key_exists($name, $this->options)) |
|
| 160 | - throw new \InvalidArgumentException("Unknown option '{$name}'"); |
|
| 162 | + if (!array_key_exists($name, $this->options)) { |
|
| 163 | + throw new \InvalidArgumentException("Unknown option '{$name}'"); |
|
| 164 | + } |
|
| 161 | 165 | return $this->options[$name]; |
| 162 | 166 | } |
| 163 | 167 | |
@@ -205,8 +209,9 @@ discard block |
||
| 205 | 209 | if (null === $value) { |
| 206 | 210 | return $this->fs; |
| 207 | 211 | } |
| 208 | - if (is_string($value)) |
|
| 209 | - $value = new Filesystem($value); |
|
| 212 | + if (is_string($value)) { |
|
| 213 | + $value = new Filesystem($value); |
|
| 214 | + } |
|
| 210 | 215 | $this->fs = $value; |
| 211 | 216 | foreach ($this->commands as $name => $command) { |
| 212 | 217 | $command->fs = $value; |
@@ -235,8 +240,9 @@ discard block |
||
| 235 | 240 | } |
| 236 | 241 | |
| 237 | 242 | public function template(string $name) { |
| 238 | - if (null === $this->templatesLoader) |
|
| 239 | - throw new \Exception("Please initialize the templates loader before trying to load templates!"); |
|
| 243 | + if (null === $this->templatesLoader) { |
|
| 244 | + throw new \Exception("Please initialize the templates loader before trying to load templates!"); |
|
| 245 | + } |
|
| 240 | 246 | return $this->templatesLoader->load($name); |
| 241 | 247 | } |
| 242 | 248 | |
@@ -258,8 +264,9 @@ discard block |
||
| 258 | 264 | public function command(string $name, Command $command = null) |
| 259 | 265 | { |
| 260 | 266 | if (null === $command) { |
| 261 | - if (!array_key_exists($name, $this->commands)) |
|
| 262 | - throw new \InvalidArgumentException("subcommand '{$name}' not found!"); |
|
| 267 | + if (!array_key_exists($name, $this->commands)) { |
|
| 268 | + throw new \InvalidArgumentException("subcommand '{$name}' not found!"); |
|
| 269 | + } |
|
| 263 | 270 | return $this->commands[$name]; |
| 264 | 271 | } |
| 265 | 272 | $this->commands[$name] = $command; |
@@ -274,9 +281,10 @@ discard block |
||
| 274 | 281 | |
| 275 | 282 | public function describe(string $name, string $description = null) |
| 276 | 283 | { |
| 277 | - if (null === $description) |
|
| 278 | - return array_key_exists($name, $this->descriptions) |
|
| 284 | + if (null === $description) { |
|
| 285 | + return array_key_exists($name, $this->descriptions) |
|
| 279 | 286 | ? $this->descriptions[$name] : ''; |
| 287 | + } |
|
| 280 | 288 | if (substr($name, 0, 2) == '--' && array_key_exists($name, $this->options())) { |
| 281 | 289 | $this->descriptions[$name] = $description; |
| 282 | 290 | return $this; |
@@ -312,16 +320,18 @@ discard block |
||
| 312 | 320 | } else { |
| 313 | 321 | $this->args = (object) $args; |
| 314 | 322 | foreach ($options as $name) { |
| 315 | - if (!array_key_exists($name, $this->options)) |
|
| 316 | - throw new \Exception("Unknown option '{$name}'"); |
|
| 323 | + if (!array_key_exists($name, $this->options)) { |
|
| 324 | + throw new \Exception("Unknown option '{$name}'"); |
|
| 325 | + } |
|
| 317 | 326 | $this->options[$name] = true; |
| 318 | 327 | } |
| 319 | 328 | } |
| 320 | 329 | |
| 321 | - if (null === $this->action) |
|
| 322 | - $this->execute(); |
|
| 323 | - else |
|
| 324 | - ($this->action)($this); |
|
| 330 | + if (null === $this->action) { |
|
| 331 | + $this->execute(); |
|
| 332 | + } else { |
|
| 333 | + ($this->action)($this); |
|
| 334 | + } |
|
| 325 | 335 | } catch (\Exception $e) { |
| 326 | 336 | $this->handleError($e); |
| 327 | 337 | } |
@@ -344,10 +354,11 @@ discard block |
||
| 344 | 354 | |
| 345 | 355 | $arguments = []; |
| 346 | 356 | foreach ($args as &$arg) { |
| 347 | - if (array_key_exists($arg, $this->options)) |
|
| 348 | - $this->options[$arg] = true; |
|
| 349 | - else |
|
| 350 | - $arguments[] = $arg; |
|
| 357 | + if (array_key_exists($arg, $this->options)) { |
|
| 358 | + $this->options[$arg] = true; |
|
| 359 | + } else { |
|
| 360 | + $arguments[] = $arg; |
|
| 361 | + } |
|
| 351 | 362 | } |
| 352 | 363 | $arguments = T::join($arguments, ' '); |
| 353 | 364 | $this->args = $this->syntax->parse($arguments); |