|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Cocotte\Console\Documentation; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
|
|
9
|
|
|
final class ArgumentDescriber |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var OutputInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $output; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(OutputInterface $output) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->output = $output; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function describe(InputArgument $argument) |
|
23
|
|
|
{ |
|
24
|
|
|
$description = $this->removeDecoration($argument->getDescription()); |
|
25
|
|
|
$this->write( |
|
26
|
|
|
'#### `'.$this->argumentName($argument)."`\n\n" |
|
27
|
|
|
.$this->formatArgumentDescription($description) |
|
28
|
|
|
.'* Is required: '.$this->argumentIsRequired($argument)."\n" |
|
29
|
|
|
.'* Is array: '.$this->argumentIsArray($argument)."\n" |
|
30
|
|
|
.'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`' |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function removeDecoration(string $string): string |
|
35
|
|
|
{ |
|
36
|
|
|
$f = new OutputFormatter(); |
|
37
|
|
|
|
|
38
|
|
|
return $f->format($string); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Writes content to output. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $content |
|
45
|
|
|
* @param bool $decorated |
|
46
|
|
|
*/ |
|
47
|
|
|
private function write($content, $decorated = false) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->output->write($content, |
|
50
|
|
|
false, |
|
51
|
|
|
$decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param InputArgument $argument |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
private function argumentName(InputArgument $argument): string |
|
59
|
|
|
{ |
|
60
|
|
|
return ($argument->getName() ?: '<none>'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $description |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
private function formatArgumentDescription($description): string |
|
68
|
|
|
{ |
|
69
|
|
|
return ($description ? preg_replace('/\s*[\r\n]\s*/', |
|
70
|
|
|
"\n", |
|
71
|
|
|
$description)."\n\n" : ''); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param InputArgument $argument |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
private function argumentIsRequired(InputArgument $argument): string |
|
79
|
|
|
{ |
|
80
|
|
|
return ($argument->isRequired() ? 'yes' : 'no'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param InputArgument $argument |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
private function argumentIsArray(InputArgument $argument): string |
|
88
|
|
|
{ |
|
89
|
|
|
return ($argument->isArray() ? 'yes' : 'no'); |
|
90
|
|
|
} |
|
91
|
|
|
} |