@@ -21,42 +21,42 @@ |
||
21 | 21 | */ |
22 | 22 | class TrimmedBufferOutput extends Output |
23 | 23 | { |
24 | - private $maxLength; |
|
25 | - private $buffer = ''; |
|
26 | - |
|
27 | - public function __construct(int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null) { |
|
28 | - if ($maxLength <= 0) { |
|
29 | - throw new InvalidArgumentException(sprintf('"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength)); |
|
30 | - } |
|
31 | - |
|
32 | - parent::__construct($verbosity, $decorated, $formatter); |
|
33 | - $this->maxLength = $maxLength; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Empties buffer and returns its content. |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function fetch() |
|
42 | - { |
|
43 | - $content = $this->buffer; |
|
44 | - $this->buffer = ''; |
|
45 | - |
|
46 | - return $content; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - protected function doWrite(string $message, bool $newline) |
|
53 | - { |
|
54 | - $this->buffer .= $message; |
|
55 | - |
|
56 | - if ($newline) { |
|
57 | - $this->buffer .= \PHP_EOL; |
|
58 | - } |
|
59 | - |
|
60 | - $this->buffer = substr($this->buffer, 0 - $this->maxLength); |
|
61 | - } |
|
24 | + private $maxLength; |
|
25 | + private $buffer = ''; |
|
26 | + |
|
27 | + public function __construct(int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null) { |
|
28 | + if ($maxLength <= 0) { |
|
29 | + throw new InvalidArgumentException(sprintf('"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength)); |
|
30 | + } |
|
31 | + |
|
32 | + parent::__construct($verbosity, $decorated, $formatter); |
|
33 | + $this->maxLength = $maxLength; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Empties buffer and returns its content. |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function fetch() |
|
42 | + { |
|
43 | + $content = $this->buffer; |
|
44 | + $this->buffer = ''; |
|
45 | + |
|
46 | + return $content; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + protected function doWrite(string $message, bool $newline) |
|
53 | + { |
|
54 | + $this->buffer .= $message; |
|
55 | + |
|
56 | + if ($newline) { |
|
57 | + $this->buffer .= \PHP_EOL; |
|
58 | + } |
|
59 | + |
|
60 | + $this->buffer = substr($this->buffer, 0 - $this->maxLength); |
|
61 | + } |
|
62 | 62 | } |
@@ -24,12 +24,12 @@ discard block |
||
24 | 24 | private $maxLength; |
25 | 25 | private $buffer = ''; |
26 | 26 | |
27 | - public function __construct(int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null) { |
|
28 | - if ($maxLength <= 0) { |
|
29 | - throw new InvalidArgumentException(sprintf('"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength)); |
|
27 | + public function __construct( int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null ) { |
|
28 | + if ( $maxLength <= 0 ) { |
|
29 | + throw new InvalidArgumentException( sprintf( '"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength ) ); |
|
30 | 30 | } |
31 | 31 | |
32 | - parent::__construct($verbosity, $decorated, $formatter); |
|
32 | + parent::__construct( $verbosity, $decorated, $formatter ); |
|
33 | 33 | $this->maxLength = $maxLength; |
34 | 34 | } |
35 | 35 | |
@@ -49,14 +49,14 @@ discard block |
||
49 | 49 | /** |
50 | 50 | * {@inheritdoc} |
51 | 51 | */ |
52 | - protected function doWrite(string $message, bool $newline) |
|
52 | + protected function doWrite( string $message, bool $newline ) |
|
53 | 53 | { |
54 | 54 | $this->buffer .= $message; |
55 | 55 | |
56 | - if ($newline) { |
|
56 | + if ( $newline ) { |
|
57 | 57 | $this->buffer .= \PHP_EOL; |
58 | 58 | } |
59 | 59 | |
60 | - $this->buffer = substr($this->buffer, 0 - $this->maxLength); |
|
60 | + $this->buffer = substr( $this->buffer, 0 - $this->maxLength ); |
|
61 | 61 | } |
62 | 62 | } |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @author Jérémy Derussé <[email protected]> |
21 | 21 | */ |
22 | -class TrimmedBufferOutput extends Output |
|
23 | -{ |
|
22 | +class TrimmedBufferOutput extends Output { |
|
24 | 23 | private $maxLength; |
25 | 24 | private $buffer = ''; |
26 | 25 | |
@@ -38,8 +37,7 @@ discard block |
||
38 | 37 | * |
39 | 38 | * @return string |
40 | 39 | */ |
41 | - public function fetch() |
|
42 | - { |
|
40 | + public function fetch() { |
|
43 | 41 | $content = $this->buffer; |
44 | 42 | $this->buffer = ''; |
45 | 43 | |
@@ -49,8 +47,7 @@ discard block |
||
49 | 47 | /** |
50 | 48 | * {@inheritdoc} |
51 | 49 | */ |
52 | - protected function doWrite(string $message, bool $newline) |
|
53 | - { |
|
50 | + protected function doWrite(string $message, bool $newline) { |
|
54 | 51 | $this->buffer .= $message; |
55 | 52 | |
56 | 53 | if ($newline) { |
@@ -21,123 +21,123 @@ |
||
21 | 21 | */ |
22 | 22 | class ConsoleSectionOutput extends StreamOutput |
23 | 23 | { |
24 | - private $content = []; |
|
25 | - private $lines = 0; |
|
26 | - private $sections; |
|
27 | - private $terminal; |
|
28 | - |
|
29 | - /** |
|
30 | - * @param resource $stream |
|
31 | - * @param ConsoleSectionOutput[] $sections |
|
32 | - */ |
|
33 | - public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) |
|
34 | - { |
|
35 | - parent::__construct($stream, $verbosity, $decorated, $formatter); |
|
36 | - array_unshift($sections, $this); |
|
37 | - $this->sections = &$sections; |
|
38 | - $this->terminal = new Terminal(); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Clears previous output for this section. |
|
43 | - * |
|
44 | - * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared |
|
45 | - */ |
|
46 | - public function clear(int $lines = null) |
|
47 | - { |
|
48 | - if (empty($this->content) || !$this->isDecorated()) { |
|
49 | - return; |
|
50 | - } |
|
51 | - |
|
52 | - if ($lines) { |
|
53 | - array_splice($this->content, -($lines * 2)); // Multiply lines by 2 to cater for each new line added between content |
|
54 | - } else { |
|
55 | - $lines = $this->lines; |
|
56 | - $this->content = []; |
|
57 | - } |
|
58 | - |
|
59 | - $this->lines -= $lines; |
|
60 | - |
|
61 | - parent::doWrite($this->popStreamContentUntilCurrentSection($lines), false); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Overwrites the previous output with a new message. |
|
66 | - * |
|
67 | - * @param array|string $message |
|
68 | - */ |
|
69 | - public function overwrite($message) |
|
70 | - { |
|
71 | - $this->clear(); |
|
72 | - $this->writeln($message); |
|
73 | - } |
|
74 | - |
|
75 | - public function getContent(): string |
|
76 | - { |
|
77 | - return implode('', $this->content); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @internal |
|
82 | - */ |
|
83 | - public function addContent(string $input) |
|
84 | - { |
|
85 | - foreach (explode(\PHP_EOL, $input) as $lineContent) { |
|
86 | - $this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1; |
|
87 | - $this->content[] = $lineContent; |
|
88 | - $this->content[] = \PHP_EOL; |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * {@inheritdoc} |
|
94 | - */ |
|
95 | - protected function doWrite(string $message, bool $newline) |
|
96 | - { |
|
97 | - if (!$this->isDecorated()) { |
|
98 | - parent::doWrite($message, $newline); |
|
99 | - |
|
100 | - return; |
|
101 | - } |
|
102 | - |
|
103 | - $erasedContent = $this->popStreamContentUntilCurrentSection(); |
|
104 | - |
|
105 | - $this->addContent($message); |
|
106 | - |
|
107 | - parent::doWrite($message, true); |
|
108 | - parent::doWrite($erasedContent, false); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits |
|
113 | - * current section. Then it erases content it crawled through. Optionally, it erases part of current section too. |
|
114 | - */ |
|
115 | - private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string |
|
116 | - { |
|
117 | - $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection; |
|
118 | - $erasedContent = []; |
|
119 | - |
|
120 | - foreach ($this->sections as $section) { |
|
121 | - if ($section === $this) { |
|
122 | - break; |
|
123 | - } |
|
124 | - |
|
125 | - $numberOfLinesToClear += $section->lines; |
|
126 | - $erasedContent[] = $section->getContent(); |
|
127 | - } |
|
128 | - |
|
129 | - if ($numberOfLinesToClear > 0) { |
|
130 | - // move cursor up n lines |
|
131 | - parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false); |
|
132 | - // erase to end of screen |
|
133 | - parent::doWrite("\x1b[0J", false); |
|
134 | - } |
|
135 | - |
|
136 | - return implode('', array_reverse($erasedContent)); |
|
137 | - } |
|
138 | - |
|
139 | - private function getDisplayLength(string $text): int |
|
140 | - { |
|
141 | - return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text))); |
|
142 | - } |
|
24 | + private $content = []; |
|
25 | + private $lines = 0; |
|
26 | + private $sections; |
|
27 | + private $terminal; |
|
28 | + |
|
29 | + /** |
|
30 | + * @param resource $stream |
|
31 | + * @param ConsoleSectionOutput[] $sections |
|
32 | + */ |
|
33 | + public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) |
|
34 | + { |
|
35 | + parent::__construct($stream, $verbosity, $decorated, $formatter); |
|
36 | + array_unshift($sections, $this); |
|
37 | + $this->sections = &$sections; |
|
38 | + $this->terminal = new Terminal(); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Clears previous output for this section. |
|
43 | + * |
|
44 | + * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared |
|
45 | + */ |
|
46 | + public function clear(int $lines = null) |
|
47 | + { |
|
48 | + if (empty($this->content) || !$this->isDecorated()) { |
|
49 | + return; |
|
50 | + } |
|
51 | + |
|
52 | + if ($lines) { |
|
53 | + array_splice($this->content, -($lines * 2)); // Multiply lines by 2 to cater for each new line added between content |
|
54 | + } else { |
|
55 | + $lines = $this->lines; |
|
56 | + $this->content = []; |
|
57 | + } |
|
58 | + |
|
59 | + $this->lines -= $lines; |
|
60 | + |
|
61 | + parent::doWrite($this->popStreamContentUntilCurrentSection($lines), false); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Overwrites the previous output with a new message. |
|
66 | + * |
|
67 | + * @param array|string $message |
|
68 | + */ |
|
69 | + public function overwrite($message) |
|
70 | + { |
|
71 | + $this->clear(); |
|
72 | + $this->writeln($message); |
|
73 | + } |
|
74 | + |
|
75 | + public function getContent(): string |
|
76 | + { |
|
77 | + return implode('', $this->content); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @internal |
|
82 | + */ |
|
83 | + public function addContent(string $input) |
|
84 | + { |
|
85 | + foreach (explode(\PHP_EOL, $input) as $lineContent) { |
|
86 | + $this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1; |
|
87 | + $this->content[] = $lineContent; |
|
88 | + $this->content[] = \PHP_EOL; |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * {@inheritdoc} |
|
94 | + */ |
|
95 | + protected function doWrite(string $message, bool $newline) |
|
96 | + { |
|
97 | + if (!$this->isDecorated()) { |
|
98 | + parent::doWrite($message, $newline); |
|
99 | + |
|
100 | + return; |
|
101 | + } |
|
102 | + |
|
103 | + $erasedContent = $this->popStreamContentUntilCurrentSection(); |
|
104 | + |
|
105 | + $this->addContent($message); |
|
106 | + |
|
107 | + parent::doWrite($message, true); |
|
108 | + parent::doWrite($erasedContent, false); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits |
|
113 | + * current section. Then it erases content it crawled through. Optionally, it erases part of current section too. |
|
114 | + */ |
|
115 | + private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string |
|
116 | + { |
|
117 | + $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection; |
|
118 | + $erasedContent = []; |
|
119 | + |
|
120 | + foreach ($this->sections as $section) { |
|
121 | + if ($section === $this) { |
|
122 | + break; |
|
123 | + } |
|
124 | + |
|
125 | + $numberOfLinesToClear += $section->lines; |
|
126 | + $erasedContent[] = $section->getContent(); |
|
127 | + } |
|
128 | + |
|
129 | + if ($numberOfLinesToClear > 0) { |
|
130 | + // move cursor up n lines |
|
131 | + parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false); |
|
132 | + // erase to end of screen |
|
133 | + parent::doWrite("\x1b[0J", false); |
|
134 | + } |
|
135 | + |
|
136 | + return implode('', array_reverse($erasedContent)); |
|
137 | + } |
|
138 | + |
|
139 | + private function getDisplayLength(string $text): int |
|
140 | + { |
|
141 | + return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text))); |
|
142 | + } |
|
143 | 143 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | class ConsoleSectionOutput extends StreamOutput |
23 | 23 | { |
24 | - private $content = []; |
|
24 | + private $content = [ ]; |
|
25 | 25 | private $lines = 0; |
26 | 26 | private $sections; |
27 | 27 | private $terminal; |
@@ -30,10 +30,10 @@ discard block |
||
30 | 30 | * @param resource $stream |
31 | 31 | * @param ConsoleSectionOutput[] $sections |
32 | 32 | */ |
33 | - public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) |
|
33 | + public function __construct( $stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter ) |
|
34 | 34 | { |
35 | - parent::__construct($stream, $verbosity, $decorated, $formatter); |
|
36 | - array_unshift($sections, $this); |
|
35 | + parent::__construct( $stream, $verbosity, $decorated, $formatter ); |
|
36 | + array_unshift( $sections, $this ); |
|
37 | 37 | $this->sections = &$sections; |
38 | 38 | $this->terminal = new Terminal(); |
39 | 39 | } |
@@ -43,22 +43,22 @@ discard block |
||
43 | 43 | * |
44 | 44 | * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared |
45 | 45 | */ |
46 | - public function clear(int $lines = null) |
|
46 | + public function clear( int $lines = null ) |
|
47 | 47 | { |
48 | - if (empty($this->content) || !$this->isDecorated()) { |
|
48 | + if ( empty( $this->content ) || ! $this->isDecorated() ) { |
|
49 | 49 | return; |
50 | 50 | } |
51 | 51 | |
52 | - if ($lines) { |
|
53 | - array_splice($this->content, -($lines * 2)); // Multiply lines by 2 to cater for each new line added between content |
|
52 | + if ( $lines ) { |
|
53 | + array_splice( $this->content, -( $lines * 2 ) ); // Multiply lines by 2 to cater for each new line added between content |
|
54 | 54 | } else { |
55 | 55 | $lines = $this->lines; |
56 | - $this->content = []; |
|
56 | + $this->content = [ ]; |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | $this->lines -= $lines; |
60 | 60 | |
61 | - parent::doWrite($this->popStreamContentUntilCurrentSection($lines), false); |
|
61 | + parent::doWrite( $this->popStreamContentUntilCurrentSection( $lines ), false ); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | /** |
@@ -66,78 +66,78 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @param array|string $message |
68 | 68 | */ |
69 | - public function overwrite($message) |
|
69 | + public function overwrite( $message ) |
|
70 | 70 | { |
71 | 71 | $this->clear(); |
72 | - $this->writeln($message); |
|
72 | + $this->writeln( $message ); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | public function getContent(): string |
76 | 76 | { |
77 | - return implode('', $this->content); |
|
77 | + return implode( '', $this->content ); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
81 | 81 | * @internal |
82 | 82 | */ |
83 | - public function addContent(string $input) |
|
83 | + public function addContent( string $input ) |
|
84 | 84 | { |
85 | - foreach (explode(\PHP_EOL, $input) as $lineContent) { |
|
86 | - $this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1; |
|
87 | - $this->content[] = $lineContent; |
|
88 | - $this->content[] = \PHP_EOL; |
|
85 | + foreach ( explode( \PHP_EOL, $input ) as $lineContent ) { |
|
86 | + $this->lines += ceil( $this->getDisplayLength( $lineContent ) / $this->terminal->getWidth() ) ?: 1; |
|
87 | + $this->content[ ] = $lineContent; |
|
88 | + $this->content[ ] = \PHP_EOL; |
|
89 | 89 | } |
90 | 90 | } |
91 | 91 | |
92 | 92 | /** |
93 | 93 | * {@inheritdoc} |
94 | 94 | */ |
95 | - protected function doWrite(string $message, bool $newline) |
|
95 | + protected function doWrite( string $message, bool $newline ) |
|
96 | 96 | { |
97 | - if (!$this->isDecorated()) { |
|
98 | - parent::doWrite($message, $newline); |
|
97 | + if ( ! $this->isDecorated() ) { |
|
98 | + parent::doWrite( $message, $newline ); |
|
99 | 99 | |
100 | 100 | return; |
101 | 101 | } |
102 | 102 | |
103 | 103 | $erasedContent = $this->popStreamContentUntilCurrentSection(); |
104 | 104 | |
105 | - $this->addContent($message); |
|
105 | + $this->addContent( $message ); |
|
106 | 106 | |
107 | - parent::doWrite($message, true); |
|
108 | - parent::doWrite($erasedContent, false); |
|
107 | + parent::doWrite( $message, true ); |
|
108 | + parent::doWrite( $erasedContent, false ); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
112 | 112 | * At initial stage, cursor is at the end of stream output. This method makes cursor crawl upwards until it hits |
113 | 113 | * current section. Then it erases content it crawled through. Optionally, it erases part of current section too. |
114 | 114 | */ |
115 | - private function popStreamContentUntilCurrentSection(int $numberOfLinesToClearFromCurrentSection = 0): string |
|
115 | + private function popStreamContentUntilCurrentSection( int $numberOfLinesToClearFromCurrentSection = 0 ): string |
|
116 | 116 | { |
117 | 117 | $numberOfLinesToClear = $numberOfLinesToClearFromCurrentSection; |
118 | - $erasedContent = []; |
|
118 | + $erasedContent = [ ]; |
|
119 | 119 | |
120 | - foreach ($this->sections as $section) { |
|
121 | - if ($section === $this) { |
|
120 | + foreach ( $this->sections as $section ) { |
|
121 | + if ( $section === $this ) { |
|
122 | 122 | break; |
123 | 123 | } |
124 | 124 | |
125 | 125 | $numberOfLinesToClear += $section->lines; |
126 | - $erasedContent[] = $section->getContent(); |
|
126 | + $erasedContent[ ] = $section->getContent(); |
|
127 | 127 | } |
128 | 128 | |
129 | - if ($numberOfLinesToClear > 0) { |
|
129 | + if ( $numberOfLinesToClear > 0 ) { |
|
130 | 130 | // move cursor up n lines |
131 | - parent::doWrite(sprintf("\x1b[%dA", $numberOfLinesToClear), false); |
|
131 | + parent::doWrite( sprintf( "\x1b[%dA", $numberOfLinesToClear ), false ); |
|
132 | 132 | // erase to end of screen |
133 | - parent::doWrite("\x1b[0J", false); |
|
133 | + parent::doWrite( "\x1b[0J", false ); |
|
134 | 134 | } |
135 | 135 | |
136 | - return implode('', array_reverse($erasedContent)); |
|
136 | + return implode( '', array_reverse( $erasedContent ) ); |
|
137 | 137 | } |
138 | 138 | |
139 | - private function getDisplayLength(string $text): int |
|
139 | + private function getDisplayLength( string $text ): int |
|
140 | 140 | { |
141 | - return Helper::width(Helper::removeDecoration($this->getFormatter(), str_replace("\t", ' ', $text))); |
|
141 | + return Helper::width( Helper::removeDecoration( $this->getFormatter(), str_replace( "\t", ' ', $text ) ) ); |
|
142 | 142 | } |
143 | 143 | } |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | * @author Pierre du Plessis <[email protected]> |
20 | 20 | * @author Gabriel Ostrolucký <[email protected]> |
21 | 21 | */ |
22 | -class ConsoleSectionOutput extends StreamOutput |
|
23 | -{ |
|
22 | +class ConsoleSectionOutput extends StreamOutput { |
|
24 | 23 | private $content = []; |
25 | 24 | private $lines = 0; |
26 | 25 | private $sections; |
@@ -30,8 +29,7 @@ discard block |
||
30 | 29 | * @param resource $stream |
31 | 30 | * @param ConsoleSectionOutput[] $sections |
32 | 31 | */ |
33 | - public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) |
|
34 | - { |
|
32 | + public function __construct($stream, array &$sections, int $verbosity, bool $decorated, OutputFormatterInterface $formatter) { |
|
35 | 33 | parent::__construct($stream, $verbosity, $decorated, $formatter); |
36 | 34 | array_unshift($sections, $this); |
37 | 35 | $this->sections = &$sections; |
@@ -43,8 +41,7 @@ discard block |
||
43 | 41 | * |
44 | 42 | * @param int $lines Number of lines to clear. If null, then the entire output of this section is cleared |
45 | 43 | */ |
46 | - public function clear(int $lines = null) |
|
47 | - { |
|
44 | + public function clear(int $lines = null) { |
|
48 | 45 | if (empty($this->content) || !$this->isDecorated()) { |
49 | 46 | return; |
50 | 47 | } |
@@ -66,8 +63,7 @@ discard block |
||
66 | 63 | * |
67 | 64 | * @param array|string $message |
68 | 65 | */ |
69 | - public function overwrite($message) |
|
70 | - { |
|
66 | + public function overwrite($message) { |
|
71 | 67 | $this->clear(); |
72 | 68 | $this->writeln($message); |
73 | 69 | } |
@@ -80,8 +76,7 @@ discard block |
||
80 | 76 | /** |
81 | 77 | * @internal |
82 | 78 | */ |
83 | - public function addContent(string $input) |
|
84 | - { |
|
79 | + public function addContent(string $input) { |
|
85 | 80 | foreach (explode(\PHP_EOL, $input) as $lineContent) { |
86 | 81 | $this->lines += ceil($this->getDisplayLength($lineContent) / $this->terminal->getWidth()) ?: 1; |
87 | 82 | $this->content[] = $lineContent; |
@@ -92,8 +87,7 @@ discard block |
||
92 | 87 | /** |
93 | 88 | * {@inheritdoc} |
94 | 89 | */ |
95 | - protected function doWrite(string $message, bool $newline) |
|
96 | - { |
|
90 | + protected function doWrite(string $message, bool $newline) { |
|
97 | 91 | if (!$this->isDecorated()) { |
98 | 92 | parent::doWrite($message, $newline); |
99 | 93 |
@@ -24,105 +24,105 @@ |
||
24 | 24 | */ |
25 | 25 | class NullOutput implements OutputInterface |
26 | 26 | { |
27 | - private $formatter; |
|
28 | - |
|
29 | - /** |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - public function setFormatter(OutputFormatterInterface $formatter) |
|
33 | - { |
|
34 | - // do nothing |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function getFormatter() |
|
41 | - { |
|
42 | - if ($this->formatter) { |
|
43 | - return $this->formatter; |
|
44 | - } |
|
45 | - // to comply with the interface we must return a OutputFormatterInterface |
|
46 | - return $this->formatter = new NullOutputFormatter(); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - public function setDecorated(bool $decorated) |
|
53 | - { |
|
54 | - // do nothing |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * {@inheritdoc} |
|
59 | - */ |
|
60 | - public function isDecorated() |
|
61 | - { |
|
62 | - return false; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * {@inheritdoc} |
|
67 | - */ |
|
68 | - public function setVerbosity(int $level) |
|
69 | - { |
|
70 | - // do nothing |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * {@inheritdoc} |
|
75 | - */ |
|
76 | - public function getVerbosity() |
|
77 | - { |
|
78 | - return self::VERBOSITY_QUIET; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * {@inheritdoc} |
|
83 | - */ |
|
84 | - public function isQuiet() |
|
85 | - { |
|
86 | - return true; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * {@inheritdoc} |
|
91 | - */ |
|
92 | - public function isVerbose() |
|
93 | - { |
|
94 | - return false; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * {@inheritdoc} |
|
99 | - */ |
|
100 | - public function isVeryVerbose() |
|
101 | - { |
|
102 | - return false; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * {@inheritdoc} |
|
107 | - */ |
|
108 | - public function isDebug() |
|
109 | - { |
|
110 | - return false; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - public function writeln($messages, int $options = self::OUTPUT_NORMAL) |
|
117 | - { |
|
118 | - // do nothing |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * {@inheritdoc} |
|
123 | - */ |
|
124 | - public function write($messages, bool $newline = false, int $options = self::OUTPUT_NORMAL) |
|
125 | - { |
|
126 | - // do nothing |
|
127 | - } |
|
27 | + private $formatter; |
|
28 | + |
|
29 | + /** |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + public function setFormatter(OutputFormatterInterface $formatter) |
|
33 | + { |
|
34 | + // do nothing |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function getFormatter() |
|
41 | + { |
|
42 | + if ($this->formatter) { |
|
43 | + return $this->formatter; |
|
44 | + } |
|
45 | + // to comply with the interface we must return a OutputFormatterInterface |
|
46 | + return $this->formatter = new NullOutputFormatter(); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + public function setDecorated(bool $decorated) |
|
53 | + { |
|
54 | + // do nothing |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * {@inheritdoc} |
|
59 | + */ |
|
60 | + public function isDecorated() |
|
61 | + { |
|
62 | + return false; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * {@inheritdoc} |
|
67 | + */ |
|
68 | + public function setVerbosity(int $level) |
|
69 | + { |
|
70 | + // do nothing |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * {@inheritdoc} |
|
75 | + */ |
|
76 | + public function getVerbosity() |
|
77 | + { |
|
78 | + return self::VERBOSITY_QUIET; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * {@inheritdoc} |
|
83 | + */ |
|
84 | + public function isQuiet() |
|
85 | + { |
|
86 | + return true; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * {@inheritdoc} |
|
91 | + */ |
|
92 | + public function isVerbose() |
|
93 | + { |
|
94 | + return false; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * {@inheritdoc} |
|
99 | + */ |
|
100 | + public function isVeryVerbose() |
|
101 | + { |
|
102 | + return false; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * {@inheritdoc} |
|
107 | + */ |
|
108 | + public function isDebug() |
|
109 | + { |
|
110 | + return false; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + public function writeln($messages, int $options = self::OUTPUT_NORMAL) |
|
117 | + { |
|
118 | + // do nothing |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * {@inheritdoc} |
|
123 | + */ |
|
124 | + public function write($messages, bool $newline = false, int $options = self::OUTPUT_NORMAL) |
|
125 | + { |
|
126 | + // do nothing |
|
127 | + } |
|
128 | 128 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | /** |
30 | 30 | * {@inheritdoc} |
31 | 31 | */ |
32 | - public function setFormatter(OutputFormatterInterface $formatter) |
|
32 | + public function setFormatter( OutputFormatterInterface $formatter ) |
|
33 | 33 | { |
34 | 34 | // do nothing |
35 | 35 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | */ |
40 | 40 | public function getFormatter() |
41 | 41 | { |
42 | - if ($this->formatter) { |
|
42 | + if ( $this->formatter ) { |
|
43 | 43 | return $this->formatter; |
44 | 44 | } |
45 | 45 | // to comply with the interface we must return a OutputFormatterInterface |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | /** |
50 | 50 | * {@inheritdoc} |
51 | 51 | */ |
52 | - public function setDecorated(bool $decorated) |
|
52 | + public function setDecorated( bool $decorated ) |
|
53 | 53 | { |
54 | 54 | // do nothing |
55 | 55 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | /** |
66 | 66 | * {@inheritdoc} |
67 | 67 | */ |
68 | - public function setVerbosity(int $level) |
|
68 | + public function setVerbosity( int $level ) |
|
69 | 69 | { |
70 | 70 | // do nothing |
71 | 71 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | /** |
114 | 114 | * {@inheritdoc} |
115 | 115 | */ |
116 | - public function writeln($messages, int $options = self::OUTPUT_NORMAL) |
|
116 | + public function writeln( $messages, int $options = self::OUTPUT_NORMAL ) |
|
117 | 117 | { |
118 | 118 | // do nothing |
119 | 119 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | /** |
122 | 122 | * {@inheritdoc} |
123 | 123 | */ |
124 | - public function write($messages, bool $newline = false, int $options = self::OUTPUT_NORMAL) |
|
124 | + public function write( $messages, bool $newline = false, int $options = self::OUTPUT_NORMAL ) |
|
125 | 125 | { |
126 | 126 | // do nothing |
127 | 127 | } |
@@ -22,23 +22,20 @@ discard block |
||
22 | 22 | * @author Fabien Potencier <[email protected]> |
23 | 23 | * @author Tobias Schultze <http://tobion.de> |
24 | 24 | */ |
25 | -class NullOutput implements OutputInterface |
|
26 | -{ |
|
25 | +class NullOutput implements OutputInterface { |
|
27 | 26 | private $formatter; |
28 | 27 | |
29 | 28 | /** |
30 | 29 | * {@inheritdoc} |
31 | 30 | */ |
32 | - public function setFormatter(OutputFormatterInterface $formatter) |
|
33 | - { |
|
31 | + public function setFormatter(OutputFormatterInterface $formatter) { |
|
34 | 32 | // do nothing |
35 | 33 | } |
36 | 34 | |
37 | 35 | /** |
38 | 36 | * {@inheritdoc} |
39 | 37 | */ |
40 | - public function getFormatter() |
|
41 | - { |
|
38 | + public function getFormatter() { |
|
42 | 39 | if ($this->formatter) { |
43 | 40 | return $this->formatter; |
44 | 41 | } |
@@ -49,80 +46,70 @@ discard block |
||
49 | 46 | /** |
50 | 47 | * {@inheritdoc} |
51 | 48 | */ |
52 | - public function setDecorated(bool $decorated) |
|
53 | - { |
|
49 | + public function setDecorated(bool $decorated) { |
|
54 | 50 | // do nothing |
55 | 51 | } |
56 | 52 | |
57 | 53 | /** |
58 | 54 | * {@inheritdoc} |
59 | 55 | */ |
60 | - public function isDecorated() |
|
61 | - { |
|
56 | + public function isDecorated() { |
|
62 | 57 | return false; |
63 | 58 | } |
64 | 59 | |
65 | 60 | /** |
66 | 61 | * {@inheritdoc} |
67 | 62 | */ |
68 | - public function setVerbosity(int $level) |
|
69 | - { |
|
63 | + public function setVerbosity(int $level) { |
|
70 | 64 | // do nothing |
71 | 65 | } |
72 | 66 | |
73 | 67 | /** |
74 | 68 | * {@inheritdoc} |
75 | 69 | */ |
76 | - public function getVerbosity() |
|
77 | - { |
|
70 | + public function getVerbosity() { |
|
78 | 71 | return self::VERBOSITY_QUIET; |
79 | 72 | } |
80 | 73 | |
81 | 74 | /** |
82 | 75 | * {@inheritdoc} |
83 | 76 | */ |
84 | - public function isQuiet() |
|
85 | - { |
|
77 | + public function isQuiet() { |
|
86 | 78 | return true; |
87 | 79 | } |
88 | 80 | |
89 | 81 | /** |
90 | 82 | * {@inheritdoc} |
91 | 83 | */ |
92 | - public function isVerbose() |
|
93 | - { |
|
84 | + public function isVerbose() { |
|
94 | 85 | return false; |
95 | 86 | } |
96 | 87 | |
97 | 88 | /** |
98 | 89 | * {@inheritdoc} |
99 | 90 | */ |
100 | - public function isVeryVerbose() |
|
101 | - { |
|
91 | + public function isVeryVerbose() { |
|
102 | 92 | return false; |
103 | 93 | } |
104 | 94 | |
105 | 95 | /** |
106 | 96 | * {@inheritdoc} |
107 | 97 | */ |
108 | - public function isDebug() |
|
109 | - { |
|
98 | + public function isDebug() { |
|
110 | 99 | return false; |
111 | 100 | } |
112 | 101 | |
113 | 102 | /** |
114 | 103 | * {@inheritdoc} |
115 | 104 | */ |
116 | - public function writeln($messages, int $options = self::OUTPUT_NORMAL) |
|
117 | - { |
|
105 | + public function writeln($messages, int $options = self::OUTPUT_NORMAL) { |
|
118 | 106 | // do nothing |
119 | 107 | } |
120 | 108 | |
121 | 109 | /** |
122 | 110 | * {@inheritdoc} |
123 | 111 | */ |
124 | - public function write($messages, bool $newline = false, int $options = self::OUTPUT_NORMAL) |
|
125 | - { |
|
112 | + public function write($messages, bool $newline = false, int $options = self::OUTPUT_NORMAL) { |
|
126 | 113 | // do nothing |
127 | 114 | } |
128 | 115 | } |
@@ -21,213 +21,213 @@ |
||
21 | 21 | */ |
22 | 22 | class InputOption |
23 | 23 | { |
24 | - /** |
|
25 | - * Do not accept input for the option (e.g. --yell). This is the default behavior of options. |
|
26 | - */ |
|
27 | - public const VALUE_NONE = 1; |
|
28 | - |
|
29 | - /** |
|
30 | - * A value must be passed when the option is used (e.g. --iterations=5 or -i5). |
|
31 | - */ |
|
32 | - public const VALUE_REQUIRED = 2; |
|
33 | - |
|
34 | - /** |
|
35 | - * The option may or may not have a value (e.g. --yell or --yell=loud). |
|
36 | - */ |
|
37 | - public const VALUE_OPTIONAL = 4; |
|
38 | - |
|
39 | - /** |
|
40 | - * The option accepts multiple values (e.g. --dir=/foo --dir=/bar). |
|
41 | - */ |
|
42 | - public const VALUE_IS_ARRAY = 8; |
|
43 | - |
|
44 | - /** |
|
45 | - * The option may have either positive or negative value (e.g. --ansi or --no-ansi). |
|
46 | - */ |
|
47 | - public const VALUE_NEGATABLE = 16; |
|
48 | - |
|
49 | - private $name; |
|
50 | - private $shortcut; |
|
51 | - private $mode; |
|
52 | - private $default; |
|
53 | - private $description; |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $name The option name |
|
57 | - * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
|
58 | - * @param int|null $mode The option mode: One of the VALUE_* constants |
|
59 | - * @param string $description A description text |
|
60 | - * @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE) |
|
61 | - * |
|
62 | - * @throws InvalidArgumentException If option mode is invalid or incompatible |
|
63 | - */ |
|
64 | - public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) |
|
65 | - { |
|
66 | - if (str_starts_with($name, '--')) { |
|
67 | - $name = substr($name, 2); |
|
68 | - } |
|
69 | - |
|
70 | - if (empty($name)) { |
|
71 | - throw new InvalidArgumentException('An option name cannot be empty.'); |
|
72 | - } |
|
73 | - |
|
74 | - if (empty($shortcut)) { |
|
75 | - $shortcut = null; |
|
76 | - } |
|
77 | - |
|
78 | - if (null !== $shortcut) { |
|
79 | - if (\is_array($shortcut)) { |
|
80 | - $shortcut = implode('|', $shortcut); |
|
81 | - } |
|
82 | - $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-')); |
|
83 | - $shortcuts = array_filter($shortcuts); |
|
84 | - $shortcut = implode('|', $shortcuts); |
|
85 | - |
|
86 | - if (empty($shortcut)) { |
|
87 | - throw new InvalidArgumentException('An option shortcut cannot be empty.'); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - if (null === $mode) { |
|
92 | - $mode = self::VALUE_NONE; |
|
93 | - } elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) { |
|
94 | - throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); |
|
95 | - } |
|
96 | - |
|
97 | - $this->name = $name; |
|
98 | - $this->shortcut = $shortcut; |
|
99 | - $this->mode = $mode; |
|
100 | - $this->description = $description; |
|
101 | - |
|
102 | - if ($this->isArray() && !$this->acceptValue()) { |
|
103 | - throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.'); |
|
104 | - } |
|
105 | - if ($this->isNegatable() && $this->acceptValue()) { |
|
106 | - throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.'); |
|
107 | - } |
|
108 | - |
|
109 | - $this->setDefault($default); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Returns the option shortcut. |
|
114 | - * |
|
115 | - * @return string|null The shortcut |
|
116 | - */ |
|
117 | - public function getShortcut() |
|
118 | - { |
|
119 | - return $this->shortcut; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the option name. |
|
124 | - * |
|
125 | - * @return string The name |
|
126 | - */ |
|
127 | - public function getName() |
|
128 | - { |
|
129 | - return $this->name; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns true if the option accepts a value. |
|
134 | - * |
|
135 | - * @return bool true if value mode is not self::VALUE_NONE, false otherwise |
|
136 | - */ |
|
137 | - public function acceptValue() |
|
138 | - { |
|
139 | - return $this->isValueRequired() || $this->isValueOptional(); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Returns true if the option requires a value. |
|
144 | - * |
|
145 | - * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise |
|
146 | - */ |
|
147 | - public function isValueRequired() |
|
148 | - { |
|
149 | - return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Returns true if the option takes an optional value. |
|
154 | - * |
|
155 | - * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise |
|
156 | - */ |
|
157 | - public function isValueOptional() |
|
158 | - { |
|
159 | - return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Returns true if the option can take multiple values. |
|
164 | - * |
|
165 | - * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise |
|
166 | - */ |
|
167 | - public function isArray() |
|
168 | - { |
|
169 | - return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); |
|
170 | - } |
|
171 | - |
|
172 | - public function isNegatable(): bool |
|
173 | - { |
|
174 | - return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string|bool|int|float|array|null $default |
|
179 | - */ |
|
180 | - public function setDefault($default = null) |
|
181 | - { |
|
182 | - if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { |
|
183 | - throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); |
|
184 | - } |
|
185 | - |
|
186 | - if ($this->isArray()) { |
|
187 | - if (null === $default) { |
|
188 | - $default = []; |
|
189 | - } elseif (!\is_array($default)) { |
|
190 | - throw new LogicException('A default value for an array option must be an array.'); |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - $this->default = $this->acceptValue() || $this->isNegatable() ? $default : false; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Returns the default value. |
|
199 | - * |
|
200 | - * @return string|bool|int|float|array|null |
|
201 | - */ |
|
202 | - public function getDefault() |
|
203 | - { |
|
204 | - return $this->default; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Returns the description text. |
|
209 | - * |
|
210 | - * @return string The description text |
|
211 | - */ |
|
212 | - public function getDescription() |
|
213 | - { |
|
214 | - return $this->description; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Checks whether the given option equals this one. |
|
219 | - * |
|
220 | - * @return bool |
|
221 | - */ |
|
222 | - public function equals(self $option) |
|
223 | - { |
|
224 | - return $option->getName() === $this->getName() |
|
225 | - && $option->getShortcut() === $this->getShortcut() |
|
226 | - && $option->getDefault() === $this->getDefault() |
|
227 | - && $option->isNegatable() === $this->isNegatable() |
|
228 | - && $option->isArray() === $this->isArray() |
|
229 | - && $option->isValueRequired() === $this->isValueRequired() |
|
230 | - && $option->isValueOptional() === $this->isValueOptional() |
|
231 | - ; |
|
232 | - } |
|
24 | + /** |
|
25 | + * Do not accept input for the option (e.g. --yell). This is the default behavior of options. |
|
26 | + */ |
|
27 | + public const VALUE_NONE = 1; |
|
28 | + |
|
29 | + /** |
|
30 | + * A value must be passed when the option is used (e.g. --iterations=5 or -i5). |
|
31 | + */ |
|
32 | + public const VALUE_REQUIRED = 2; |
|
33 | + |
|
34 | + /** |
|
35 | + * The option may or may not have a value (e.g. --yell or --yell=loud). |
|
36 | + */ |
|
37 | + public const VALUE_OPTIONAL = 4; |
|
38 | + |
|
39 | + /** |
|
40 | + * The option accepts multiple values (e.g. --dir=/foo --dir=/bar). |
|
41 | + */ |
|
42 | + public const VALUE_IS_ARRAY = 8; |
|
43 | + |
|
44 | + /** |
|
45 | + * The option may have either positive or negative value (e.g. --ansi or --no-ansi). |
|
46 | + */ |
|
47 | + public const VALUE_NEGATABLE = 16; |
|
48 | + |
|
49 | + private $name; |
|
50 | + private $shortcut; |
|
51 | + private $mode; |
|
52 | + private $default; |
|
53 | + private $description; |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $name The option name |
|
57 | + * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
|
58 | + * @param int|null $mode The option mode: One of the VALUE_* constants |
|
59 | + * @param string $description A description text |
|
60 | + * @param string|bool|int|float|array|null $default The default value (must be null for self::VALUE_NONE) |
|
61 | + * |
|
62 | + * @throws InvalidArgumentException If option mode is invalid or incompatible |
|
63 | + */ |
|
64 | + public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) |
|
65 | + { |
|
66 | + if (str_starts_with($name, '--')) { |
|
67 | + $name = substr($name, 2); |
|
68 | + } |
|
69 | + |
|
70 | + if (empty($name)) { |
|
71 | + throw new InvalidArgumentException('An option name cannot be empty.'); |
|
72 | + } |
|
73 | + |
|
74 | + if (empty($shortcut)) { |
|
75 | + $shortcut = null; |
|
76 | + } |
|
77 | + |
|
78 | + if (null !== $shortcut) { |
|
79 | + if (\is_array($shortcut)) { |
|
80 | + $shortcut = implode('|', $shortcut); |
|
81 | + } |
|
82 | + $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-')); |
|
83 | + $shortcuts = array_filter($shortcuts); |
|
84 | + $shortcut = implode('|', $shortcuts); |
|
85 | + |
|
86 | + if (empty($shortcut)) { |
|
87 | + throw new InvalidArgumentException('An option shortcut cannot be empty.'); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + if (null === $mode) { |
|
92 | + $mode = self::VALUE_NONE; |
|
93 | + } elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) { |
|
94 | + throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); |
|
95 | + } |
|
96 | + |
|
97 | + $this->name = $name; |
|
98 | + $this->shortcut = $shortcut; |
|
99 | + $this->mode = $mode; |
|
100 | + $this->description = $description; |
|
101 | + |
|
102 | + if ($this->isArray() && !$this->acceptValue()) { |
|
103 | + throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.'); |
|
104 | + } |
|
105 | + if ($this->isNegatable() && $this->acceptValue()) { |
|
106 | + throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.'); |
|
107 | + } |
|
108 | + |
|
109 | + $this->setDefault($default); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Returns the option shortcut. |
|
114 | + * |
|
115 | + * @return string|null The shortcut |
|
116 | + */ |
|
117 | + public function getShortcut() |
|
118 | + { |
|
119 | + return $this->shortcut; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the option name. |
|
124 | + * |
|
125 | + * @return string The name |
|
126 | + */ |
|
127 | + public function getName() |
|
128 | + { |
|
129 | + return $this->name; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns true if the option accepts a value. |
|
134 | + * |
|
135 | + * @return bool true if value mode is not self::VALUE_NONE, false otherwise |
|
136 | + */ |
|
137 | + public function acceptValue() |
|
138 | + { |
|
139 | + return $this->isValueRequired() || $this->isValueOptional(); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Returns true if the option requires a value. |
|
144 | + * |
|
145 | + * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise |
|
146 | + */ |
|
147 | + public function isValueRequired() |
|
148 | + { |
|
149 | + return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Returns true if the option takes an optional value. |
|
154 | + * |
|
155 | + * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise |
|
156 | + */ |
|
157 | + public function isValueOptional() |
|
158 | + { |
|
159 | + return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Returns true if the option can take multiple values. |
|
164 | + * |
|
165 | + * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise |
|
166 | + */ |
|
167 | + public function isArray() |
|
168 | + { |
|
169 | + return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); |
|
170 | + } |
|
171 | + |
|
172 | + public function isNegatable(): bool |
|
173 | + { |
|
174 | + return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string|bool|int|float|array|null $default |
|
179 | + */ |
|
180 | + public function setDefault($default = null) |
|
181 | + { |
|
182 | + if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { |
|
183 | + throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); |
|
184 | + } |
|
185 | + |
|
186 | + if ($this->isArray()) { |
|
187 | + if (null === $default) { |
|
188 | + $default = []; |
|
189 | + } elseif (!\is_array($default)) { |
|
190 | + throw new LogicException('A default value for an array option must be an array.'); |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + $this->default = $this->acceptValue() || $this->isNegatable() ? $default : false; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Returns the default value. |
|
199 | + * |
|
200 | + * @return string|bool|int|float|array|null |
|
201 | + */ |
|
202 | + public function getDefault() |
|
203 | + { |
|
204 | + return $this->default; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Returns the description text. |
|
209 | + * |
|
210 | + * @return string The description text |
|
211 | + */ |
|
212 | + public function getDescription() |
|
213 | + { |
|
214 | + return $this->description; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Checks whether the given option equals this one. |
|
219 | + * |
|
220 | + * @return bool |
|
221 | + */ |
|
222 | + public function equals(self $option) |
|
223 | + { |
|
224 | + return $option->getName() === $this->getName() |
|
225 | + && $option->getShortcut() === $this->getShortcut() |
|
226 | + && $option->getDefault() === $this->getDefault() |
|
227 | + && $option->isNegatable() === $this->isNegatable() |
|
228 | + && $option->isArray() === $this->isArray() |
|
229 | + && $option->isValueRequired() === $this->isValueRequired() |
|
230 | + && $option->isValueOptional() === $this->isValueOptional() |
|
231 | + ; |
|
232 | + } |
|
233 | 233 | } |
@@ -61,37 +61,37 @@ discard block |
||
61 | 61 | * |
62 | 62 | * @throws InvalidArgumentException If option mode is invalid or incompatible |
63 | 63 | */ |
64 | - public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) |
|
64 | + public function __construct( string $name, $shortcut = null, int $mode = null, string $description = '', $default = null ) |
|
65 | 65 | { |
66 | - if (str_starts_with($name, '--')) { |
|
67 | - $name = substr($name, 2); |
|
66 | + if ( str_starts_with( $name, '--' ) ) { |
|
67 | + $name = substr( $name, 2 ); |
|
68 | 68 | } |
69 | 69 | |
70 | - if (empty($name)) { |
|
71 | - throw new InvalidArgumentException('An option name cannot be empty.'); |
|
70 | + if ( empty( $name ) ) { |
|
71 | + throw new InvalidArgumentException( 'An option name cannot be empty.' ); |
|
72 | 72 | } |
73 | 73 | |
74 | - if (empty($shortcut)) { |
|
74 | + if ( empty( $shortcut ) ) { |
|
75 | 75 | $shortcut = null; |
76 | 76 | } |
77 | 77 | |
78 | - if (null !== $shortcut) { |
|
79 | - if (\is_array($shortcut)) { |
|
80 | - $shortcut = implode('|', $shortcut); |
|
78 | + if ( null !== $shortcut ) { |
|
79 | + if ( \is_array( $shortcut ) ) { |
|
80 | + $shortcut = implode( '|', $shortcut ); |
|
81 | 81 | } |
82 | - $shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-')); |
|
83 | - $shortcuts = array_filter($shortcuts); |
|
84 | - $shortcut = implode('|', $shortcuts); |
|
82 | + $shortcuts = preg_split( '{(\|)-?}', ltrim( $shortcut, '-' ) ); |
|
83 | + $shortcuts = array_filter( $shortcuts ); |
|
84 | + $shortcut = implode( '|', $shortcuts ); |
|
85 | 85 | |
86 | - if (empty($shortcut)) { |
|
87 | - throw new InvalidArgumentException('An option shortcut cannot be empty.'); |
|
86 | + if ( empty( $shortcut ) ) { |
|
87 | + throw new InvalidArgumentException( 'An option shortcut cannot be empty.' ); |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
91 | - if (null === $mode) { |
|
91 | + if ( null === $mode ) { |
|
92 | 92 | $mode = self::VALUE_NONE; |
93 | - } elseif ($mode >= (self::VALUE_NEGATABLE << 1) || $mode < 1) { |
|
94 | - throw new InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); |
|
93 | + } elseif ( $mode >= ( self::VALUE_NEGATABLE << 1 ) || $mode < 1 ) { |
|
94 | + throw new InvalidArgumentException( sprintf( 'Option mode "%s" is not valid.', $mode ) ); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | $this->name = $name; |
@@ -99,14 +99,14 @@ discard block |
||
99 | 99 | $this->mode = $mode; |
100 | 100 | $this->description = $description; |
101 | 101 | |
102 | - if ($this->isArray() && !$this->acceptValue()) { |
|
103 | - throw new InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.'); |
|
102 | + if ( $this->isArray() && ! $this->acceptValue() ) { |
|
103 | + throw new InvalidArgumentException( 'Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.' ); |
|
104 | 104 | } |
105 | - if ($this->isNegatable() && $this->acceptValue()) { |
|
106 | - throw new InvalidArgumentException('Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.'); |
|
105 | + if ( $this->isNegatable() && $this->acceptValue() ) { |
|
106 | + throw new InvalidArgumentException( 'Impossible to have an option mode VALUE_NEGATABLE if the option also accepts a value.' ); |
|
107 | 107 | } |
108 | 108 | |
109 | - $this->setDefault($default); |
|
109 | + $this->setDefault( $default ); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | /** |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | */ |
147 | 147 | public function isValueRequired() |
148 | 148 | { |
149 | - return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); |
|
149 | + return self::VALUE_REQUIRED === ( self::VALUE_REQUIRED & $this->mode ); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | public function isValueOptional() |
158 | 158 | { |
159 | - return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); |
|
159 | + return self::VALUE_OPTIONAL === ( self::VALUE_OPTIONAL & $this->mode ); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
@@ -166,28 +166,28 @@ discard block |
||
166 | 166 | */ |
167 | 167 | public function isArray() |
168 | 168 | { |
169 | - return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); |
|
169 | + return self::VALUE_IS_ARRAY === ( self::VALUE_IS_ARRAY & $this->mode ); |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | public function isNegatable(): bool |
173 | 173 | { |
174 | - return self::VALUE_NEGATABLE === (self::VALUE_NEGATABLE & $this->mode); |
|
174 | + return self::VALUE_NEGATABLE === ( self::VALUE_NEGATABLE & $this->mode ); |
|
175 | 175 | } |
176 | 176 | |
177 | 177 | /** |
178 | 178 | * @param string|bool|int|float|array|null $default |
179 | 179 | */ |
180 | - public function setDefault($default = null) |
|
180 | + public function setDefault( $default = null ) |
|
181 | 181 | { |
182 | - if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { |
|
183 | - throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); |
|
182 | + if ( self::VALUE_NONE === ( self::VALUE_NONE & $this->mode ) && null !== $default ) { |
|
183 | + throw new LogicException( 'Cannot set a default value when using InputOption::VALUE_NONE mode.' ); |
|
184 | 184 | } |
185 | 185 | |
186 | - if ($this->isArray()) { |
|
187 | - if (null === $default) { |
|
188 | - $default = []; |
|
189 | - } elseif (!\is_array($default)) { |
|
190 | - throw new LogicException('A default value for an array option must be an array.'); |
|
186 | + if ( $this->isArray() ) { |
|
187 | + if ( null === $default ) { |
|
188 | + $default = [ ]; |
|
189 | + } elseif ( ! \is_array( $default ) ) { |
|
190 | + throw new LogicException( 'A default value for an array option must be an array.' ); |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | * |
220 | 220 | * @return bool |
221 | 221 | */ |
222 | - public function equals(self $option) |
|
222 | + public function equals( self $option ) |
|
223 | 223 | { |
224 | 224 | return $option->getName() === $this->getName() |
225 | 225 | && $option->getShortcut() === $this->getShortcut() |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @author Fabien Potencier <[email protected]> |
21 | 21 | */ |
22 | -class InputOption |
|
23 | -{ |
|
22 | +class InputOption { |
|
24 | 23 | /** |
25 | 24 | * Do not accept input for the option (e.g. --yell). This is the default behavior of options. |
26 | 25 | */ |
@@ -61,8 +60,7 @@ discard block |
||
61 | 60 | * |
62 | 61 | * @throws InvalidArgumentException If option mode is invalid or incompatible |
63 | 62 | */ |
64 | - public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) |
|
65 | - { |
|
63 | + public function __construct(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) { |
|
66 | 64 | if (str_starts_with($name, '--')) { |
67 | 65 | $name = substr($name, 2); |
68 | 66 | } |
@@ -114,8 +112,7 @@ discard block |
||
114 | 112 | * |
115 | 113 | * @return string|null The shortcut |
116 | 114 | */ |
117 | - public function getShortcut() |
|
118 | - { |
|
115 | + public function getShortcut() { |
|
119 | 116 | return $this->shortcut; |
120 | 117 | } |
121 | 118 | |
@@ -124,8 +121,7 @@ discard block |
||
124 | 121 | * |
125 | 122 | * @return string The name |
126 | 123 | */ |
127 | - public function getName() |
|
128 | - { |
|
124 | + public function getName() { |
|
129 | 125 | return $this->name; |
130 | 126 | } |
131 | 127 | |
@@ -134,8 +130,7 @@ discard block |
||
134 | 130 | * |
135 | 131 | * @return bool true if value mode is not self::VALUE_NONE, false otherwise |
136 | 132 | */ |
137 | - public function acceptValue() |
|
138 | - { |
|
133 | + public function acceptValue() { |
|
139 | 134 | return $this->isValueRequired() || $this->isValueOptional(); |
140 | 135 | } |
141 | 136 | |
@@ -144,8 +139,7 @@ discard block |
||
144 | 139 | * |
145 | 140 | * @return bool true if value mode is self::VALUE_REQUIRED, false otherwise |
146 | 141 | */ |
147 | - public function isValueRequired() |
|
148 | - { |
|
142 | + public function isValueRequired() { |
|
149 | 143 | return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); |
150 | 144 | } |
151 | 145 | |
@@ -154,8 +148,7 @@ discard block |
||
154 | 148 | * |
155 | 149 | * @return bool true if value mode is self::VALUE_OPTIONAL, false otherwise |
156 | 150 | */ |
157 | - public function isValueOptional() |
|
158 | - { |
|
151 | + public function isValueOptional() { |
|
159 | 152 | return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); |
160 | 153 | } |
161 | 154 | |
@@ -164,8 +157,7 @@ discard block |
||
164 | 157 | * |
165 | 158 | * @return bool true if mode is self::VALUE_IS_ARRAY, false otherwise |
166 | 159 | */ |
167 | - public function isArray() |
|
168 | - { |
|
160 | + public function isArray() { |
|
169 | 161 | return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); |
170 | 162 | } |
171 | 163 | |
@@ -177,8 +169,7 @@ discard block |
||
177 | 169 | /** |
178 | 170 | * @param string|bool|int|float|array|null $default |
179 | 171 | */ |
180 | - public function setDefault($default = null) |
|
181 | - { |
|
172 | + public function setDefault($default = null) { |
|
182 | 173 | if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { |
183 | 174 | throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); |
184 | 175 | } |
@@ -199,8 +190,7 @@ discard block |
||
199 | 190 | * |
200 | 191 | * @return string|bool|int|float|array|null |
201 | 192 | */ |
202 | - public function getDefault() |
|
203 | - { |
|
193 | + public function getDefault() { |
|
204 | 194 | return $this->default; |
205 | 195 | } |
206 | 196 | |
@@ -209,8 +199,7 @@ discard block |
||
209 | 199 | * |
210 | 200 | * @return string The description text |
211 | 201 | */ |
212 | - public function getDescription() |
|
213 | - { |
|
202 | + public function getDescription() { |
|
214 | 203 | return $this->description; |
215 | 204 | } |
216 | 205 | |
@@ -219,8 +208,7 @@ discard block |
||
219 | 208 | * |
220 | 209 | * @return bool |
221 | 210 | */ |
222 | - public function equals(self $option) |
|
223 | - { |
|
211 | + public function equals(self $option) { |
|
224 | 212 | return $option->getName() === $this->getName() |
225 | 213 | && $option->getShortcut() === $this->getShortcut() |
226 | 214 | && $option->getDefault() === $this->getDefault() |
@@ -24,45 +24,45 @@ |
||
24 | 24 | */ |
25 | 25 | class StringInput extends ArgvInput |
26 | 26 | { |
27 | - public const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)'; |
|
28 | - public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')'; |
|
27 | + public const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)'; |
|
28 | + public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $input A string representing the parameters from the CLI |
|
32 | - */ |
|
33 | - public function __construct(string $input) |
|
34 | - { |
|
35 | - parent::__construct([]); |
|
30 | + /** |
|
31 | + * @param string $input A string representing the parameters from the CLI |
|
32 | + */ |
|
33 | + public function __construct(string $input) |
|
34 | + { |
|
35 | + parent::__construct([]); |
|
36 | 36 | |
37 | - $this->setTokens($this->tokenize($input)); |
|
38 | - } |
|
37 | + $this->setTokens($this->tokenize($input)); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Tokenizes a string. |
|
42 | - * |
|
43 | - * @throws InvalidArgumentException When unable to parse input (should never happen) |
|
44 | - */ |
|
45 | - private function tokenize(string $input): array |
|
46 | - { |
|
47 | - $tokens = []; |
|
48 | - $length = \strlen($input); |
|
49 | - $cursor = 0; |
|
50 | - while ($cursor < $length) { |
|
51 | - if (preg_match('/\s+/A', $input, $match, 0, $cursor)) { |
|
52 | - } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, 0, $cursor)) { |
|
53 | - $tokens[] = $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1))); |
|
54 | - } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, 0, $cursor)) { |
|
55 | - $tokens[] = stripcslashes(substr($match[0], 1, -1)); |
|
56 | - } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, 0, $cursor)) { |
|
57 | - $tokens[] = stripcslashes($match[1]); |
|
58 | - } else { |
|
59 | - // should never happen |
|
60 | - throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10))); |
|
61 | - } |
|
40 | + /** |
|
41 | + * Tokenizes a string. |
|
42 | + * |
|
43 | + * @throws InvalidArgumentException When unable to parse input (should never happen) |
|
44 | + */ |
|
45 | + private function tokenize(string $input): array |
|
46 | + { |
|
47 | + $tokens = []; |
|
48 | + $length = \strlen($input); |
|
49 | + $cursor = 0; |
|
50 | + while ($cursor < $length) { |
|
51 | + if (preg_match('/\s+/A', $input, $match, 0, $cursor)) { |
|
52 | + } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, 0, $cursor)) { |
|
53 | + $tokens[] = $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1))); |
|
54 | + } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, 0, $cursor)) { |
|
55 | + $tokens[] = stripcslashes(substr($match[0], 1, -1)); |
|
56 | + } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, 0, $cursor)) { |
|
57 | + $tokens[] = stripcslashes($match[1]); |
|
58 | + } else { |
|
59 | + // should never happen |
|
60 | + throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10))); |
|
61 | + } |
|
62 | 62 | |
63 | - $cursor += \strlen($match[0]); |
|
64 | - } |
|
63 | + $cursor += \strlen($match[0]); |
|
64 | + } |
|
65 | 65 | |
66 | - return $tokens; |
|
67 | - } |
|
66 | + return $tokens; |
|
67 | + } |
|
68 | 68 | } |
@@ -30,11 +30,11 @@ discard block |
||
30 | 30 | /** |
31 | 31 | * @param string $input A string representing the parameters from the CLI |
32 | 32 | */ |
33 | - public function __construct(string $input) |
|
33 | + public function __construct( string $input ) |
|
34 | 34 | { |
35 | - parent::__construct([]); |
|
35 | + parent::__construct( [ ] ); |
|
36 | 36 | |
37 | - $this->setTokens($this->tokenize($input)); |
|
37 | + $this->setTokens( $this->tokenize( $input ) ); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -42,25 +42,25 @@ discard block |
||
42 | 42 | * |
43 | 43 | * @throws InvalidArgumentException When unable to parse input (should never happen) |
44 | 44 | */ |
45 | - private function tokenize(string $input): array |
|
45 | + private function tokenize( string $input ): array |
|
46 | 46 | { |
47 | - $tokens = []; |
|
48 | - $length = \strlen($input); |
|
47 | + $tokens = [ ]; |
|
48 | + $length = \strlen( $input ); |
|
49 | 49 | $cursor = 0; |
50 | - while ($cursor < $length) { |
|
51 | - if (preg_match('/\s+/A', $input, $match, 0, $cursor)) { |
|
52 | - } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, 0, $cursor)) { |
|
53 | - $tokens[] = $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1))); |
|
54 | - } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, 0, $cursor)) { |
|
55 | - $tokens[] = stripcslashes(substr($match[0], 1, -1)); |
|
56 | - } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, 0, $cursor)) { |
|
57 | - $tokens[] = stripcslashes($match[1]); |
|
50 | + while ( $cursor < $length ) { |
|
51 | + if ( preg_match( '/\s+/A', $input, $match, 0, $cursor ) ) { |
|
52 | + } elseif ( preg_match( '/([^="\'\s]+?)(=?)(' . self::REGEX_QUOTED_STRING . '+)/A', $input, $match, 0, $cursor ) ) { |
|
53 | + $tokens[ ] = $match[ 1 ] . $match[ 2 ] . stripcslashes( str_replace( [ '"\'', '\'"', '\'\'', '""' ], '', substr( $match[ 3 ], 1, -1 ) ) ); |
|
54 | + } elseif ( preg_match( '/' . self::REGEX_QUOTED_STRING . '/A', $input, $match, 0, $cursor ) ) { |
|
55 | + $tokens[ ] = stripcslashes( substr( $match[ 0 ], 1, -1 ) ); |
|
56 | + } elseif ( preg_match( '/' . self::REGEX_STRING . '/A', $input, $match, 0, $cursor ) ) { |
|
57 | + $tokens[ ] = stripcslashes( $match[ 1 ] ); |
|
58 | 58 | } else { |
59 | 59 | // should never happen |
60 | - throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10))); |
|
60 | + throw new InvalidArgumentException( sprintf( 'Unable to parse input near "... %s ...".', substr( $input, $cursor, 10 ) ) ); |
|
61 | 61 | } |
62 | 62 | |
63 | - $cursor += \strlen($match[0]); |
|
63 | + $cursor += \strlen( $match[ 0 ] ); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | return $tokens; |
@@ -22,16 +22,14 @@ |
||
22 | 22 | * |
23 | 23 | * @author Fabien Potencier <[email protected]> |
24 | 24 | */ |
25 | -class StringInput extends ArgvInput |
|
26 | -{ |
|
25 | +class StringInput extends ArgvInput { |
|
27 | 26 | public const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)'; |
28 | 27 | public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')'; |
29 | 28 | |
30 | 29 | /** |
31 | 30 | * @param string $input A string representing the parameters from the CLI |
32 | 31 | */ |
33 | - public function __construct(string $input) |
|
34 | - { |
|
32 | + public function __construct(string $input) { |
|
35 | 33 | parent::__construct([]); |
36 | 34 | |
37 | 35 | $this->setTokens($this->tokenize($input)); |
@@ -21,109 +21,109 @@ |
||
21 | 21 | */ |
22 | 22 | class InputArgument |
23 | 23 | { |
24 | - public const REQUIRED = 1; |
|
25 | - public const OPTIONAL = 2; |
|
26 | - public const IS_ARRAY = 4; |
|
27 | - |
|
28 | - private $name; |
|
29 | - private $mode; |
|
30 | - private $default; |
|
31 | - private $description; |
|
32 | - |
|
33 | - /** |
|
34 | - * @param string $name The argument name |
|
35 | - * @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL |
|
36 | - * @param string $description A description text |
|
37 | - * @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only) |
|
38 | - * |
|
39 | - * @throws InvalidArgumentException When argument mode is not valid |
|
40 | - */ |
|
41 | - public function __construct(string $name, int $mode = null, string $description = '', $default = null) |
|
42 | - { |
|
43 | - if (null === $mode) { |
|
44 | - $mode = self::OPTIONAL; |
|
45 | - } elseif ($mode > 7 || $mode < 1) { |
|
46 | - throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode)); |
|
47 | - } |
|
48 | - |
|
49 | - $this->name = $name; |
|
50 | - $this->mode = $mode; |
|
51 | - $this->description = $description; |
|
52 | - |
|
53 | - $this->setDefault($default); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Returns the argument name. |
|
58 | - * |
|
59 | - * @return string The argument name |
|
60 | - */ |
|
61 | - public function getName() |
|
62 | - { |
|
63 | - return $this->name; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Returns true if the argument is required. |
|
68 | - * |
|
69 | - * @return bool true if parameter mode is self::REQUIRED, false otherwise |
|
70 | - */ |
|
71 | - public function isRequired() |
|
72 | - { |
|
73 | - return self::REQUIRED === (self::REQUIRED & $this->mode); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns true if the argument can take multiple values. |
|
78 | - * |
|
79 | - * @return bool true if mode is self::IS_ARRAY, false otherwise |
|
80 | - */ |
|
81 | - public function isArray() |
|
82 | - { |
|
83 | - return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Sets the default value. |
|
88 | - * |
|
89 | - * @param string|bool|int|float|array|null $default |
|
90 | - * |
|
91 | - * @throws LogicException When incorrect default value is given |
|
92 | - */ |
|
93 | - public function setDefault($default = null) |
|
94 | - { |
|
95 | - if (self::REQUIRED === $this->mode && null !== $default) { |
|
96 | - throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.'); |
|
97 | - } |
|
98 | - |
|
99 | - if ($this->isArray()) { |
|
100 | - if (null === $default) { |
|
101 | - $default = []; |
|
102 | - } elseif (!\is_array($default)) { |
|
103 | - throw new LogicException('A default value for an array argument must be an array.'); |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - $this->default = $default; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Returns the default value. |
|
112 | - * |
|
113 | - * @return string|bool|int|float|array|null |
|
114 | - */ |
|
115 | - public function getDefault() |
|
116 | - { |
|
117 | - return $this->default; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns the description text. |
|
122 | - * |
|
123 | - * @return string The description text |
|
124 | - */ |
|
125 | - public function getDescription() |
|
126 | - { |
|
127 | - return $this->description; |
|
128 | - } |
|
24 | + public const REQUIRED = 1; |
|
25 | + public const OPTIONAL = 2; |
|
26 | + public const IS_ARRAY = 4; |
|
27 | + |
|
28 | + private $name; |
|
29 | + private $mode; |
|
30 | + private $default; |
|
31 | + private $description; |
|
32 | + |
|
33 | + /** |
|
34 | + * @param string $name The argument name |
|
35 | + * @param int|null $mode The argument mode: self::REQUIRED or self::OPTIONAL |
|
36 | + * @param string $description A description text |
|
37 | + * @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only) |
|
38 | + * |
|
39 | + * @throws InvalidArgumentException When argument mode is not valid |
|
40 | + */ |
|
41 | + public function __construct(string $name, int $mode = null, string $description = '', $default = null) |
|
42 | + { |
|
43 | + if (null === $mode) { |
|
44 | + $mode = self::OPTIONAL; |
|
45 | + } elseif ($mode > 7 || $mode < 1) { |
|
46 | + throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode)); |
|
47 | + } |
|
48 | + |
|
49 | + $this->name = $name; |
|
50 | + $this->mode = $mode; |
|
51 | + $this->description = $description; |
|
52 | + |
|
53 | + $this->setDefault($default); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Returns the argument name. |
|
58 | + * |
|
59 | + * @return string The argument name |
|
60 | + */ |
|
61 | + public function getName() |
|
62 | + { |
|
63 | + return $this->name; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Returns true if the argument is required. |
|
68 | + * |
|
69 | + * @return bool true if parameter mode is self::REQUIRED, false otherwise |
|
70 | + */ |
|
71 | + public function isRequired() |
|
72 | + { |
|
73 | + return self::REQUIRED === (self::REQUIRED & $this->mode); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns true if the argument can take multiple values. |
|
78 | + * |
|
79 | + * @return bool true if mode is self::IS_ARRAY, false otherwise |
|
80 | + */ |
|
81 | + public function isArray() |
|
82 | + { |
|
83 | + return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Sets the default value. |
|
88 | + * |
|
89 | + * @param string|bool|int|float|array|null $default |
|
90 | + * |
|
91 | + * @throws LogicException When incorrect default value is given |
|
92 | + */ |
|
93 | + public function setDefault($default = null) |
|
94 | + { |
|
95 | + if (self::REQUIRED === $this->mode && null !== $default) { |
|
96 | + throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.'); |
|
97 | + } |
|
98 | + |
|
99 | + if ($this->isArray()) { |
|
100 | + if (null === $default) { |
|
101 | + $default = []; |
|
102 | + } elseif (!\is_array($default)) { |
|
103 | + throw new LogicException('A default value for an array argument must be an array.'); |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + $this->default = $default; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Returns the default value. |
|
112 | + * |
|
113 | + * @return string|bool|int|float|array|null |
|
114 | + */ |
|
115 | + public function getDefault() |
|
116 | + { |
|
117 | + return $this->default; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns the description text. |
|
122 | + * |
|
123 | + * @return string The description text |
|
124 | + */ |
|
125 | + public function getDescription() |
|
126 | + { |
|
127 | + return $this->description; |
|
128 | + } |
|
129 | 129 | } |
@@ -38,19 +38,19 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @throws InvalidArgumentException When argument mode is not valid |
40 | 40 | */ |
41 | - public function __construct(string $name, int $mode = null, string $description = '', $default = null) |
|
41 | + public function __construct( string $name, int $mode = null, string $description = '', $default = null ) |
|
42 | 42 | { |
43 | - if (null === $mode) { |
|
43 | + if ( null === $mode ) { |
|
44 | 44 | $mode = self::OPTIONAL; |
45 | - } elseif ($mode > 7 || $mode < 1) { |
|
46 | - throw new InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode)); |
|
45 | + } elseif ( $mode > 7 || $mode < 1 ) { |
|
46 | + throw new InvalidArgumentException( sprintf( 'Argument mode "%s" is not valid.', $mode ) ); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | $this->name = $name; |
50 | 50 | $this->mode = $mode; |
51 | 51 | $this->description = $description; |
52 | 52 | |
53 | - $this->setDefault($default); |
|
53 | + $this->setDefault( $default ); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public function isRequired() |
72 | 72 | { |
73 | - return self::REQUIRED === (self::REQUIRED & $this->mode); |
|
73 | + return self::REQUIRED === ( self::REQUIRED & $this->mode ); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | */ |
81 | 81 | public function isArray() |
82 | 82 | { |
83 | - return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
|
83 | + return self::IS_ARRAY === ( self::IS_ARRAY & $this->mode ); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
@@ -90,17 +90,17 @@ discard block |
||
90 | 90 | * |
91 | 91 | * @throws LogicException When incorrect default value is given |
92 | 92 | */ |
93 | - public function setDefault($default = null) |
|
93 | + public function setDefault( $default = null ) |
|
94 | 94 | { |
95 | - if (self::REQUIRED === $this->mode && null !== $default) { |
|
96 | - throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.'); |
|
95 | + if ( self::REQUIRED === $this->mode && null !== $default ) { |
|
96 | + throw new LogicException( 'Cannot set a default value except for InputArgument::OPTIONAL mode.' ); |
|
97 | 97 | } |
98 | 98 | |
99 | - if ($this->isArray()) { |
|
100 | - if (null === $default) { |
|
101 | - $default = []; |
|
102 | - } elseif (!\is_array($default)) { |
|
103 | - throw new LogicException('A default value for an array argument must be an array.'); |
|
99 | + if ( $this->isArray() ) { |
|
100 | + if ( null === $default ) { |
|
101 | + $default = [ ]; |
|
102 | + } elseif ( ! \is_array( $default ) ) { |
|
103 | + throw new LogicException( 'A default value for an array argument must be an array.' ); |
|
104 | 104 | } |
105 | 105 | } |
106 | 106 |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @author Fabien Potencier <[email protected]> |
21 | 21 | */ |
22 | -class InputArgument |
|
23 | -{ |
|
22 | +class InputArgument { |
|
24 | 23 | public const REQUIRED = 1; |
25 | 24 | public const OPTIONAL = 2; |
26 | 25 | public const IS_ARRAY = 4; |
@@ -38,8 +37,7 @@ discard block |
||
38 | 37 | * |
39 | 38 | * @throws InvalidArgumentException When argument mode is not valid |
40 | 39 | */ |
41 | - public function __construct(string $name, int $mode = null, string $description = '', $default = null) |
|
42 | - { |
|
40 | + public function __construct(string $name, int $mode = null, string $description = '', $default = null) { |
|
43 | 41 | if (null === $mode) { |
44 | 42 | $mode = self::OPTIONAL; |
45 | 43 | } elseif ($mode > 7 || $mode < 1) { |
@@ -58,8 +56,7 @@ discard block |
||
58 | 56 | * |
59 | 57 | * @return string The argument name |
60 | 58 | */ |
61 | - public function getName() |
|
62 | - { |
|
59 | + public function getName() { |
|
63 | 60 | return $this->name; |
64 | 61 | } |
65 | 62 | |
@@ -68,8 +65,7 @@ discard block |
||
68 | 65 | * |
69 | 66 | * @return bool true if parameter mode is self::REQUIRED, false otherwise |
70 | 67 | */ |
71 | - public function isRequired() |
|
72 | - { |
|
68 | + public function isRequired() { |
|
73 | 69 | return self::REQUIRED === (self::REQUIRED & $this->mode); |
74 | 70 | } |
75 | 71 | |
@@ -78,8 +74,7 @@ discard block |
||
78 | 74 | * |
79 | 75 | * @return bool true if mode is self::IS_ARRAY, false otherwise |
80 | 76 | */ |
81 | - public function isArray() |
|
82 | - { |
|
77 | + public function isArray() { |
|
83 | 78 | return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); |
84 | 79 | } |
85 | 80 | |
@@ -90,8 +85,7 @@ discard block |
||
90 | 85 | * |
91 | 86 | * @throws LogicException When incorrect default value is given |
92 | 87 | */ |
93 | - public function setDefault($default = null) |
|
94 | - { |
|
88 | + public function setDefault($default = null) { |
|
95 | 89 | if (self::REQUIRED === $this->mode && null !== $default) { |
96 | 90 | throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.'); |
97 | 91 | } |
@@ -112,8 +106,7 @@ discard block |
||
112 | 106 | * |
113 | 107 | * @return string|bool|int|float|array|null |
114 | 108 | */ |
115 | - public function getDefault() |
|
116 | - { |
|
109 | + public function getDefault() { |
|
117 | 110 | return $this->default; |
118 | 111 | } |
119 | 112 | |
@@ -122,8 +115,7 @@ discard block |
||
122 | 115 | * |
123 | 116 | * @return string The description text |
124 | 117 | */ |
125 | - public function getDescription() |
|
126 | - { |
|
118 | + public function getDescription() { |
|
127 | 119 | return $this->description; |
128 | 120 | } |
129 | 121 | } |
@@ -40,332 +40,332 @@ |
||
40 | 40 | */ |
41 | 41 | class ArgvInput extends Input |
42 | 42 | { |
43 | - private $tokens; |
|
44 | - private $parsed; |
|
45 | - |
|
46 | - public function __construct(array $argv = null, InputDefinition $definition = null) |
|
47 | - { |
|
48 | - $argv = $argv ?? $_SERVER['argv'] ?? []; |
|
49 | - |
|
50 | - // strip the application name |
|
51 | - array_shift($argv); |
|
52 | - |
|
53 | - $this->tokens = $argv; |
|
54 | - |
|
55 | - parent::__construct($definition); |
|
56 | - } |
|
57 | - |
|
58 | - protected function setTokens(array $tokens) |
|
59 | - { |
|
60 | - $this->tokens = $tokens; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - protected function parse() |
|
67 | - { |
|
68 | - $parseOptions = true; |
|
69 | - $this->parsed = $this->tokens; |
|
70 | - while (null !== $token = array_shift($this->parsed)) { |
|
71 | - if ($parseOptions && '' == $token) { |
|
72 | - $this->parseArgument($token); |
|
73 | - } elseif ($parseOptions && '--' == $token) { |
|
74 | - $parseOptions = false; |
|
75 | - } elseif ($parseOptions && str_starts_with($token, '--')) { |
|
76 | - $this->parseLongOption($token); |
|
77 | - } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
|
78 | - $this->parseShortOption($token); |
|
79 | - } else { |
|
80 | - $this->parseArgument($token); |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Parses a short option. |
|
87 | - */ |
|
88 | - private function parseShortOption(string $token) |
|
89 | - { |
|
90 | - $name = substr($token, 1); |
|
91 | - |
|
92 | - if (\strlen($name) > 1) { |
|
93 | - if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
|
94 | - // an option with a value (with no space) |
|
95 | - $this->addShortOption($name[0], substr($name, 1)); |
|
96 | - } else { |
|
97 | - $this->parseShortOptionSet($name); |
|
98 | - } |
|
99 | - } else { |
|
100 | - $this->addShortOption($name, null); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Parses a short option set. |
|
106 | - * |
|
107 | - * @throws RuntimeException When option given doesn't exist |
|
108 | - */ |
|
109 | - private function parseShortOptionSet(string $name) |
|
110 | - { |
|
111 | - $len = \strlen($name); |
|
112 | - for ($i = 0; $i < $len; ++$i) { |
|
113 | - if (!$this->definition->hasShortcut($name[$i])) { |
|
114 | - $encoding = mb_detect_encoding($name, null, true); |
|
115 | - throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); |
|
116 | - } |
|
117 | - |
|
118 | - $option = $this->definition->getOptionForShortcut($name[$i]); |
|
119 | - if ($option->acceptValue()) { |
|
120 | - $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
|
121 | - |
|
122 | - break; |
|
123 | - } else { |
|
124 | - $this->addLongOption($option->getName(), null); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Parses a long option. |
|
131 | - */ |
|
132 | - private function parseLongOption(string $token) |
|
133 | - { |
|
134 | - $name = substr($token, 2); |
|
135 | - |
|
136 | - if (false !== $pos = strpos($name, '=')) { |
|
137 | - if (0 === \strlen($value = substr($name, $pos + 1))) { |
|
138 | - array_unshift($this->parsed, $value); |
|
139 | - } |
|
140 | - $this->addLongOption(substr($name, 0, $pos), $value); |
|
141 | - } else { |
|
142 | - $this->addLongOption($name, null); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Parses an argument. |
|
148 | - * |
|
149 | - * @throws RuntimeException When too many arguments are given |
|
150 | - */ |
|
151 | - private function parseArgument(string $token) |
|
152 | - { |
|
153 | - $c = \count($this->arguments); |
|
154 | - |
|
155 | - // if input is expecting another argument, add it |
|
156 | - if ($this->definition->hasArgument($c)) { |
|
157 | - $arg = $this->definition->getArgument($c); |
|
158 | - $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; |
|
159 | - |
|
160 | - // if last argument isArray(), append token to last argument |
|
161 | - } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
|
162 | - $arg = $this->definition->getArgument($c - 1); |
|
163 | - $this->arguments[$arg->getName()][] = $token; |
|
164 | - |
|
165 | - // unexpected argument |
|
166 | - } else { |
|
167 | - $all = $this->definition->getArguments(); |
|
168 | - $symfonyCommandName = null; |
|
169 | - if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) { |
|
170 | - $symfonyCommandName = $this->arguments['command'] ?? null; |
|
171 | - unset($all[$key]); |
|
172 | - } |
|
173 | - |
|
174 | - if (\count($all)) { |
|
175 | - if ($symfonyCommandName) { |
|
176 | - $message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all))); |
|
177 | - } else { |
|
178 | - $message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))); |
|
179 | - } |
|
180 | - } elseif ($symfonyCommandName) { |
|
181 | - $message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token); |
|
182 | - } else { |
|
183 | - $message = sprintf('No arguments expected, got "%s".', $token); |
|
184 | - } |
|
185 | - |
|
186 | - throw new RuntimeException($message); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Adds a short option value. |
|
192 | - * |
|
193 | - * @throws RuntimeException When option given doesn't exist |
|
194 | - */ |
|
195 | - private function addShortOption(string $shortcut, $value) |
|
196 | - { |
|
197 | - if (!$this->definition->hasShortcut($shortcut)) { |
|
198 | - throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
199 | - } |
|
200 | - |
|
201 | - $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Adds a long option value. |
|
206 | - * |
|
207 | - * @throws RuntimeException When option given doesn't exist |
|
208 | - */ |
|
209 | - private function addLongOption(string $name, $value) |
|
210 | - { |
|
211 | - if (!$this->definition->hasOption($name)) { |
|
212 | - if (!$this->definition->hasNegation($name)) { |
|
213 | - throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
|
214 | - } |
|
215 | - |
|
216 | - $optionName = $this->definition->negationToName($name); |
|
217 | - if (null !== $value) { |
|
218 | - throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
219 | - } |
|
220 | - $this->options[$optionName] = false; |
|
221 | - |
|
222 | - return; |
|
223 | - } |
|
224 | - |
|
225 | - $option = $this->definition->getOption($name); |
|
226 | - |
|
227 | - if (null !== $value && !$option->acceptValue()) { |
|
228 | - throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
229 | - } |
|
230 | - |
|
231 | - if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { |
|
232 | - // if option accepts an optional or mandatory argument |
|
233 | - // let's see if there is one provided |
|
234 | - $next = array_shift($this->parsed); |
|
235 | - if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) { |
|
236 | - $value = $next; |
|
237 | - } else { |
|
238 | - array_unshift($this->parsed, $next); |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - if (null === $value) { |
|
243 | - if ($option->isValueRequired()) { |
|
244 | - throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
|
245 | - } |
|
246 | - |
|
247 | - if (!$option->isArray() && !$option->isValueOptional()) { |
|
248 | - $value = true; |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - if ($option->isArray()) { |
|
253 | - $this->options[$name][] = $value; |
|
254 | - } else { |
|
255 | - $this->options[$name] = $value; |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * {@inheritdoc} |
|
261 | - */ |
|
262 | - public function getFirstArgument() |
|
263 | - { |
|
264 | - $isOption = false; |
|
265 | - foreach ($this->tokens as $i => $token) { |
|
266 | - if ($token && '-' === $token[0]) { |
|
267 | - if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { |
|
268 | - continue; |
|
269 | - } |
|
270 | - |
|
271 | - // If it's a long option, consider that everything after "--" is the option name. |
|
272 | - // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) |
|
273 | - $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); |
|
274 | - if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { |
|
275 | - // noop |
|
276 | - } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { |
|
277 | - $isOption = true; |
|
278 | - } |
|
279 | - |
|
280 | - continue; |
|
281 | - } |
|
282 | - |
|
283 | - if ($isOption) { |
|
284 | - $isOption = false; |
|
285 | - continue; |
|
286 | - } |
|
287 | - |
|
288 | - return $token; |
|
289 | - } |
|
290 | - |
|
291 | - return null; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * {@inheritdoc} |
|
296 | - */ |
|
297 | - public function hasParameterOption($values, bool $onlyParams = false) |
|
298 | - { |
|
299 | - $values = (array) $values; |
|
300 | - |
|
301 | - foreach ($this->tokens as $token) { |
|
302 | - if ($onlyParams && '--' === $token) { |
|
303 | - return false; |
|
304 | - } |
|
305 | - foreach ($values as $value) { |
|
306 | - // Options with values: |
|
307 | - // For long options, test for '--option=' at beginning |
|
308 | - // For short options, test for '-o' at beginning |
|
309 | - $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
310 | - if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { |
|
311 | - return true; |
|
312 | - } |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - return false; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * {@inheritdoc} |
|
321 | - */ |
|
322 | - public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
323 | - { |
|
324 | - $values = (array) $values; |
|
325 | - $tokens = $this->tokens; |
|
326 | - |
|
327 | - while (0 < \count($tokens)) { |
|
328 | - $token = array_shift($tokens); |
|
329 | - if ($onlyParams && '--' === $token) { |
|
330 | - return $default; |
|
331 | - } |
|
332 | - |
|
333 | - foreach ($values as $value) { |
|
334 | - if ($token === $value) { |
|
335 | - return array_shift($tokens); |
|
336 | - } |
|
337 | - // Options with values: |
|
338 | - // For long options, test for '--option=' at beginning |
|
339 | - // For short options, test for '-o' at beginning |
|
340 | - $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
341 | - if ('' !== $leading && str_starts_with($token, $leading)) { |
|
342 | - return substr($token, \strlen($leading)); |
|
343 | - } |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - return $default; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Returns a stringified representation of the args passed to the command. |
|
352 | - * |
|
353 | - * @return string |
|
354 | - */ |
|
355 | - public function __toString() |
|
356 | - { |
|
357 | - $tokens = array_map(function ($token) { |
|
358 | - if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
|
359 | - return $match[1].$this->escapeToken($match[2]); |
|
360 | - } |
|
361 | - |
|
362 | - if ($token && '-' !== $token[0]) { |
|
363 | - return $this->escapeToken($token); |
|
364 | - } |
|
365 | - |
|
366 | - return $token; |
|
367 | - }, $this->tokens); |
|
368 | - |
|
369 | - return implode(' ', $tokens); |
|
370 | - } |
|
43 | + private $tokens; |
|
44 | + private $parsed; |
|
45 | + |
|
46 | + public function __construct(array $argv = null, InputDefinition $definition = null) |
|
47 | + { |
|
48 | + $argv = $argv ?? $_SERVER['argv'] ?? []; |
|
49 | + |
|
50 | + // strip the application name |
|
51 | + array_shift($argv); |
|
52 | + |
|
53 | + $this->tokens = $argv; |
|
54 | + |
|
55 | + parent::__construct($definition); |
|
56 | + } |
|
57 | + |
|
58 | + protected function setTokens(array $tokens) |
|
59 | + { |
|
60 | + $this->tokens = $tokens; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + protected function parse() |
|
67 | + { |
|
68 | + $parseOptions = true; |
|
69 | + $this->parsed = $this->tokens; |
|
70 | + while (null !== $token = array_shift($this->parsed)) { |
|
71 | + if ($parseOptions && '' == $token) { |
|
72 | + $this->parseArgument($token); |
|
73 | + } elseif ($parseOptions && '--' == $token) { |
|
74 | + $parseOptions = false; |
|
75 | + } elseif ($parseOptions && str_starts_with($token, '--')) { |
|
76 | + $this->parseLongOption($token); |
|
77 | + } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
|
78 | + $this->parseShortOption($token); |
|
79 | + } else { |
|
80 | + $this->parseArgument($token); |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Parses a short option. |
|
87 | + */ |
|
88 | + private function parseShortOption(string $token) |
|
89 | + { |
|
90 | + $name = substr($token, 1); |
|
91 | + |
|
92 | + if (\strlen($name) > 1) { |
|
93 | + if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
|
94 | + // an option with a value (with no space) |
|
95 | + $this->addShortOption($name[0], substr($name, 1)); |
|
96 | + } else { |
|
97 | + $this->parseShortOptionSet($name); |
|
98 | + } |
|
99 | + } else { |
|
100 | + $this->addShortOption($name, null); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Parses a short option set. |
|
106 | + * |
|
107 | + * @throws RuntimeException When option given doesn't exist |
|
108 | + */ |
|
109 | + private function parseShortOptionSet(string $name) |
|
110 | + { |
|
111 | + $len = \strlen($name); |
|
112 | + for ($i = 0; $i < $len; ++$i) { |
|
113 | + if (!$this->definition->hasShortcut($name[$i])) { |
|
114 | + $encoding = mb_detect_encoding($name, null, true); |
|
115 | + throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); |
|
116 | + } |
|
117 | + |
|
118 | + $option = $this->definition->getOptionForShortcut($name[$i]); |
|
119 | + if ($option->acceptValue()) { |
|
120 | + $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
|
121 | + |
|
122 | + break; |
|
123 | + } else { |
|
124 | + $this->addLongOption($option->getName(), null); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Parses a long option. |
|
131 | + */ |
|
132 | + private function parseLongOption(string $token) |
|
133 | + { |
|
134 | + $name = substr($token, 2); |
|
135 | + |
|
136 | + if (false !== $pos = strpos($name, '=')) { |
|
137 | + if (0 === \strlen($value = substr($name, $pos + 1))) { |
|
138 | + array_unshift($this->parsed, $value); |
|
139 | + } |
|
140 | + $this->addLongOption(substr($name, 0, $pos), $value); |
|
141 | + } else { |
|
142 | + $this->addLongOption($name, null); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Parses an argument. |
|
148 | + * |
|
149 | + * @throws RuntimeException When too many arguments are given |
|
150 | + */ |
|
151 | + private function parseArgument(string $token) |
|
152 | + { |
|
153 | + $c = \count($this->arguments); |
|
154 | + |
|
155 | + // if input is expecting another argument, add it |
|
156 | + if ($this->definition->hasArgument($c)) { |
|
157 | + $arg = $this->definition->getArgument($c); |
|
158 | + $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; |
|
159 | + |
|
160 | + // if last argument isArray(), append token to last argument |
|
161 | + } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
|
162 | + $arg = $this->definition->getArgument($c - 1); |
|
163 | + $this->arguments[$arg->getName()][] = $token; |
|
164 | + |
|
165 | + // unexpected argument |
|
166 | + } else { |
|
167 | + $all = $this->definition->getArguments(); |
|
168 | + $symfonyCommandName = null; |
|
169 | + if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) { |
|
170 | + $symfonyCommandName = $this->arguments['command'] ?? null; |
|
171 | + unset($all[$key]); |
|
172 | + } |
|
173 | + |
|
174 | + if (\count($all)) { |
|
175 | + if ($symfonyCommandName) { |
|
176 | + $message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all))); |
|
177 | + } else { |
|
178 | + $message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))); |
|
179 | + } |
|
180 | + } elseif ($symfonyCommandName) { |
|
181 | + $message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token); |
|
182 | + } else { |
|
183 | + $message = sprintf('No arguments expected, got "%s".', $token); |
|
184 | + } |
|
185 | + |
|
186 | + throw new RuntimeException($message); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Adds a short option value. |
|
192 | + * |
|
193 | + * @throws RuntimeException When option given doesn't exist |
|
194 | + */ |
|
195 | + private function addShortOption(string $shortcut, $value) |
|
196 | + { |
|
197 | + if (!$this->definition->hasShortcut($shortcut)) { |
|
198 | + throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
199 | + } |
|
200 | + |
|
201 | + $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Adds a long option value. |
|
206 | + * |
|
207 | + * @throws RuntimeException When option given doesn't exist |
|
208 | + */ |
|
209 | + private function addLongOption(string $name, $value) |
|
210 | + { |
|
211 | + if (!$this->definition->hasOption($name)) { |
|
212 | + if (!$this->definition->hasNegation($name)) { |
|
213 | + throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
|
214 | + } |
|
215 | + |
|
216 | + $optionName = $this->definition->negationToName($name); |
|
217 | + if (null !== $value) { |
|
218 | + throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
219 | + } |
|
220 | + $this->options[$optionName] = false; |
|
221 | + |
|
222 | + return; |
|
223 | + } |
|
224 | + |
|
225 | + $option = $this->definition->getOption($name); |
|
226 | + |
|
227 | + if (null !== $value && !$option->acceptValue()) { |
|
228 | + throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
229 | + } |
|
230 | + |
|
231 | + if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { |
|
232 | + // if option accepts an optional or mandatory argument |
|
233 | + // let's see if there is one provided |
|
234 | + $next = array_shift($this->parsed); |
|
235 | + if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) { |
|
236 | + $value = $next; |
|
237 | + } else { |
|
238 | + array_unshift($this->parsed, $next); |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + if (null === $value) { |
|
243 | + if ($option->isValueRequired()) { |
|
244 | + throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
|
245 | + } |
|
246 | + |
|
247 | + if (!$option->isArray() && !$option->isValueOptional()) { |
|
248 | + $value = true; |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + if ($option->isArray()) { |
|
253 | + $this->options[$name][] = $value; |
|
254 | + } else { |
|
255 | + $this->options[$name] = $value; |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * {@inheritdoc} |
|
261 | + */ |
|
262 | + public function getFirstArgument() |
|
263 | + { |
|
264 | + $isOption = false; |
|
265 | + foreach ($this->tokens as $i => $token) { |
|
266 | + if ($token && '-' === $token[0]) { |
|
267 | + if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { |
|
268 | + continue; |
|
269 | + } |
|
270 | + |
|
271 | + // If it's a long option, consider that everything after "--" is the option name. |
|
272 | + // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) |
|
273 | + $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); |
|
274 | + if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { |
|
275 | + // noop |
|
276 | + } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { |
|
277 | + $isOption = true; |
|
278 | + } |
|
279 | + |
|
280 | + continue; |
|
281 | + } |
|
282 | + |
|
283 | + if ($isOption) { |
|
284 | + $isOption = false; |
|
285 | + continue; |
|
286 | + } |
|
287 | + |
|
288 | + return $token; |
|
289 | + } |
|
290 | + |
|
291 | + return null; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * {@inheritdoc} |
|
296 | + */ |
|
297 | + public function hasParameterOption($values, bool $onlyParams = false) |
|
298 | + { |
|
299 | + $values = (array) $values; |
|
300 | + |
|
301 | + foreach ($this->tokens as $token) { |
|
302 | + if ($onlyParams && '--' === $token) { |
|
303 | + return false; |
|
304 | + } |
|
305 | + foreach ($values as $value) { |
|
306 | + // Options with values: |
|
307 | + // For long options, test for '--option=' at beginning |
|
308 | + // For short options, test for '-o' at beginning |
|
309 | + $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
310 | + if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { |
|
311 | + return true; |
|
312 | + } |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + return false; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * {@inheritdoc} |
|
321 | + */ |
|
322 | + public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
323 | + { |
|
324 | + $values = (array) $values; |
|
325 | + $tokens = $this->tokens; |
|
326 | + |
|
327 | + while (0 < \count($tokens)) { |
|
328 | + $token = array_shift($tokens); |
|
329 | + if ($onlyParams && '--' === $token) { |
|
330 | + return $default; |
|
331 | + } |
|
332 | + |
|
333 | + foreach ($values as $value) { |
|
334 | + if ($token === $value) { |
|
335 | + return array_shift($tokens); |
|
336 | + } |
|
337 | + // Options with values: |
|
338 | + // For long options, test for '--option=' at beginning |
|
339 | + // For short options, test for '-o' at beginning |
|
340 | + $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
341 | + if ('' !== $leading && str_starts_with($token, $leading)) { |
|
342 | + return substr($token, \strlen($leading)); |
|
343 | + } |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + return $default; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Returns a stringified representation of the args passed to the command. |
|
352 | + * |
|
353 | + * @return string |
|
354 | + */ |
|
355 | + public function __toString() |
|
356 | + { |
|
357 | + $tokens = array_map(function ($token) { |
|
358 | + if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
|
359 | + return $match[1].$this->escapeToken($match[2]); |
|
360 | + } |
|
361 | + |
|
362 | + if ($token && '-' !== $token[0]) { |
|
363 | + return $this->escapeToken($token); |
|
364 | + } |
|
365 | + |
|
366 | + return $token; |
|
367 | + }, $this->tokens); |
|
368 | + |
|
369 | + return implode(' ', $tokens); |
|
370 | + } |
|
371 | 371 | } |
@@ -43,19 +43,19 @@ discard block |
||
43 | 43 | private $tokens; |
44 | 44 | private $parsed; |
45 | 45 | |
46 | - public function __construct(array $argv = null, InputDefinition $definition = null) |
|
46 | + public function __construct( array $argv = null, InputDefinition $definition = null ) |
|
47 | 47 | { |
48 | - $argv = $argv ?? $_SERVER['argv'] ?? []; |
|
48 | + $argv = $argv ?? $_SERVER[ 'argv' ] ?? [ ]; |
|
49 | 49 | |
50 | 50 | // strip the application name |
51 | - array_shift($argv); |
|
51 | + array_shift( $argv ); |
|
52 | 52 | |
53 | 53 | $this->tokens = $argv; |
54 | 54 | |
55 | - parent::__construct($definition); |
|
55 | + parent::__construct( $definition ); |
|
56 | 56 | } |
57 | 57 | |
58 | - protected function setTokens(array $tokens) |
|
58 | + protected function setTokens( array $tokens ) |
|
59 | 59 | { |
60 | 60 | $this->tokens = $tokens; |
61 | 61 | } |
@@ -67,17 +67,17 @@ discard block |
||
67 | 67 | { |
68 | 68 | $parseOptions = true; |
69 | 69 | $this->parsed = $this->tokens; |
70 | - while (null !== $token = array_shift($this->parsed)) { |
|
71 | - if ($parseOptions && '' == $token) { |
|
72 | - $this->parseArgument($token); |
|
73 | - } elseif ($parseOptions && '--' == $token) { |
|
70 | + while ( null !== $token = array_shift( $this->parsed ) ) { |
|
71 | + if ( $parseOptions && '' == $token ) { |
|
72 | + $this->parseArgument( $token ); |
|
73 | + } elseif ( $parseOptions && '--' == $token ) { |
|
74 | 74 | $parseOptions = false; |
75 | - } elseif ($parseOptions && str_starts_with($token, '--')) { |
|
76 | - $this->parseLongOption($token); |
|
77 | - } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
|
78 | - $this->parseShortOption($token); |
|
75 | + } elseif ( $parseOptions && str_starts_with( $token, '--' ) ) { |
|
76 | + $this->parseLongOption( $token ); |
|
77 | + } elseif ( $parseOptions && '-' === $token[ 0 ] && '-' !== $token ) { |
|
78 | + $this->parseShortOption( $token ); |
|
79 | 79 | } else { |
80 | - $this->parseArgument($token); |
|
80 | + $this->parseArgument( $token ); |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 | } |
@@ -85,19 +85,19 @@ discard block |
||
85 | 85 | /** |
86 | 86 | * Parses a short option. |
87 | 87 | */ |
88 | - private function parseShortOption(string $token) |
|
88 | + private function parseShortOption( string $token ) |
|
89 | 89 | { |
90 | - $name = substr($token, 1); |
|
90 | + $name = substr( $token, 1 ); |
|
91 | 91 | |
92 | - if (\strlen($name) > 1) { |
|
93 | - if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
|
92 | + if ( \strlen( $name ) > 1 ) { |
|
93 | + if ( $this->definition->hasShortcut( $name[ 0 ] ) && $this->definition->getOptionForShortcut( $name[ 0 ] )->acceptValue() ) { |
|
94 | 94 | // an option with a value (with no space) |
95 | - $this->addShortOption($name[0], substr($name, 1)); |
|
95 | + $this->addShortOption( $name[ 0 ], substr( $name, 1 ) ); |
|
96 | 96 | } else { |
97 | - $this->parseShortOptionSet($name); |
|
97 | + $this->parseShortOptionSet( $name ); |
|
98 | 98 | } |
99 | 99 | } else { |
100 | - $this->addShortOption($name, null); |
|
100 | + $this->addShortOption( $name, null ); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 | |
@@ -106,22 +106,22 @@ discard block |
||
106 | 106 | * |
107 | 107 | * @throws RuntimeException When option given doesn't exist |
108 | 108 | */ |
109 | - private function parseShortOptionSet(string $name) |
|
109 | + private function parseShortOptionSet( string $name ) |
|
110 | 110 | { |
111 | - $len = \strlen($name); |
|
112 | - for ($i = 0; $i < $len; ++$i) { |
|
113 | - if (!$this->definition->hasShortcut($name[$i])) { |
|
114 | - $encoding = mb_detect_encoding($name, null, true); |
|
115 | - throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); |
|
111 | + $len = \strlen( $name ); |
|
112 | + for ( $i = 0; $i < $len; ++$i ) { |
|
113 | + if ( ! $this->definition->hasShortcut( $name[ $i ] ) ) { |
|
114 | + $encoding = mb_detect_encoding( $name, null, true ); |
|
115 | + throw new RuntimeException( sprintf( 'The "-%s" option does not exist.', false === $encoding ? $name[ $i ] : mb_substr( $name, $i, 1, $encoding ) ) ); |
|
116 | 116 | } |
117 | 117 | |
118 | - $option = $this->definition->getOptionForShortcut($name[$i]); |
|
119 | - if ($option->acceptValue()) { |
|
120 | - $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
|
118 | + $option = $this->definition->getOptionForShortcut( $name[ $i ] ); |
|
119 | + if ( $option->acceptValue() ) { |
|
120 | + $this->addLongOption( $option->getName(), $i === $len - 1 ? null : substr( $name, $i + 1 ) ); |
|
121 | 121 | |
122 | 122 | break; |
123 | 123 | } else { |
124 | - $this->addLongOption($option->getName(), null); |
|
124 | + $this->addLongOption( $option->getName(), null ); |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | } |
@@ -129,17 +129,17 @@ discard block |
||
129 | 129 | /** |
130 | 130 | * Parses a long option. |
131 | 131 | */ |
132 | - private function parseLongOption(string $token) |
|
132 | + private function parseLongOption( string $token ) |
|
133 | 133 | { |
134 | - $name = substr($token, 2); |
|
134 | + $name = substr( $token, 2 ); |
|
135 | 135 | |
136 | - if (false !== $pos = strpos($name, '=')) { |
|
137 | - if (0 === \strlen($value = substr($name, $pos + 1))) { |
|
138 | - array_unshift($this->parsed, $value); |
|
136 | + if ( false !== $pos = strpos( $name, '=' ) ) { |
|
137 | + if ( 0 === \strlen( $value = substr( $name, $pos + 1 ) ) ) { |
|
138 | + array_unshift( $this->parsed, $value ); |
|
139 | 139 | } |
140 | - $this->addLongOption(substr($name, 0, $pos), $value); |
|
140 | + $this->addLongOption( substr( $name, 0, $pos ), $value ); |
|
141 | 141 | } else { |
142 | - $this->addLongOption($name, null); |
|
142 | + $this->addLongOption( $name, null ); |
|
143 | 143 | } |
144 | 144 | } |
145 | 145 | |
@@ -148,42 +148,42 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @throws RuntimeException When too many arguments are given |
150 | 150 | */ |
151 | - private function parseArgument(string $token) |
|
151 | + private function parseArgument( string $token ) |
|
152 | 152 | { |
153 | - $c = \count($this->arguments); |
|
153 | + $c = \count( $this->arguments ); |
|
154 | 154 | |
155 | 155 | // if input is expecting another argument, add it |
156 | - if ($this->definition->hasArgument($c)) { |
|
157 | - $arg = $this->definition->getArgument($c); |
|
158 | - $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; |
|
156 | + if ( $this->definition->hasArgument( $c ) ) { |
|
157 | + $arg = $this->definition->getArgument( $c ); |
|
158 | + $this->arguments[ $arg->getName() ] = $arg->isArray() ? [ $token ] : $token; |
|
159 | 159 | |
160 | 160 | // if last argument isArray(), append token to last argument |
161 | - } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
|
162 | - $arg = $this->definition->getArgument($c - 1); |
|
163 | - $this->arguments[$arg->getName()][] = $token; |
|
161 | + } elseif ( $this->definition->hasArgument( $c - 1 ) && $this->definition->getArgument( $c - 1 )->isArray() ) { |
|
162 | + $arg = $this->definition->getArgument( $c - 1 ); |
|
163 | + $this->arguments[ $arg->getName() ][ ] = $token; |
|
164 | 164 | |
165 | 165 | // unexpected argument |
166 | 166 | } else { |
167 | 167 | $all = $this->definition->getArguments(); |
168 | 168 | $symfonyCommandName = null; |
169 | - if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) { |
|
170 | - $symfonyCommandName = $this->arguments['command'] ?? null; |
|
171 | - unset($all[$key]); |
|
169 | + if ( ( $inputArgument = $all[ $key = array_key_first( $all ) ] ?? null ) && 'command' === $inputArgument->getName() ) { |
|
170 | + $symfonyCommandName = $this->arguments[ 'command' ] ?? null; |
|
171 | + unset( $all[ $key ] ); |
|
172 | 172 | } |
173 | 173 | |
174 | - if (\count($all)) { |
|
175 | - if ($symfonyCommandName) { |
|
176 | - $message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all))); |
|
174 | + if ( \count( $all ) ) { |
|
175 | + if ( $symfonyCommandName ) { |
|
176 | + $message = sprintf( 'Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode( '" "', array_keys( $all ) ) ); |
|
177 | 177 | } else { |
178 | - $message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))); |
|
178 | + $message = sprintf( 'Too many arguments, expected arguments "%s".', implode( '" "', array_keys( $all ) ) ); |
|
179 | 179 | } |
180 | - } elseif ($symfonyCommandName) { |
|
181 | - $message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token); |
|
180 | + } elseif ( $symfonyCommandName ) { |
|
181 | + $message = sprintf( 'No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token ); |
|
182 | 182 | } else { |
183 | - $message = sprintf('No arguments expected, got "%s".', $token); |
|
183 | + $message = sprintf( 'No arguments expected, got "%s".', $token ); |
|
184 | 184 | } |
185 | 185 | |
186 | - throw new RuntimeException($message); |
|
186 | + throw new RuntimeException( $message ); |
|
187 | 187 | } |
188 | 188 | } |
189 | 189 | |
@@ -192,13 +192,13 @@ discard block |
||
192 | 192 | * |
193 | 193 | * @throws RuntimeException When option given doesn't exist |
194 | 194 | */ |
195 | - private function addShortOption(string $shortcut, $value) |
|
195 | + private function addShortOption( string $shortcut, $value ) |
|
196 | 196 | { |
197 | - if (!$this->definition->hasShortcut($shortcut)) { |
|
198 | - throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
197 | + if ( ! $this->definition->hasShortcut( $shortcut ) ) { |
|
198 | + throw new RuntimeException( sprintf( 'The "-%s" option does not exist.', $shortcut ) ); |
|
199 | 199 | } |
200 | 200 | |
201 | - $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
201 | + $this->addLongOption( $this->definition->getOptionForShortcut( $shortcut )->getName(), $value ); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -206,53 +206,53 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @throws RuntimeException When option given doesn't exist |
208 | 208 | */ |
209 | - private function addLongOption(string $name, $value) |
|
209 | + private function addLongOption( string $name, $value ) |
|
210 | 210 | { |
211 | - if (!$this->definition->hasOption($name)) { |
|
212 | - if (!$this->definition->hasNegation($name)) { |
|
213 | - throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
|
211 | + if ( ! $this->definition->hasOption( $name ) ) { |
|
212 | + if ( ! $this->definition->hasNegation( $name ) ) { |
|
213 | + throw new RuntimeException( sprintf( 'The "--%s" option does not exist.', $name ) ); |
|
214 | 214 | } |
215 | 215 | |
216 | - $optionName = $this->definition->negationToName($name); |
|
217 | - if (null !== $value) { |
|
218 | - throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
216 | + $optionName = $this->definition->negationToName( $name ); |
|
217 | + if ( null !== $value ) { |
|
218 | + throw new RuntimeException( sprintf( 'The "--%s" option does not accept a value.', $name ) ); |
|
219 | 219 | } |
220 | - $this->options[$optionName] = false; |
|
220 | + $this->options[ $optionName ] = false; |
|
221 | 221 | |
222 | 222 | return; |
223 | 223 | } |
224 | 224 | |
225 | - $option = $this->definition->getOption($name); |
|
225 | + $option = $this->definition->getOption( $name ); |
|
226 | 226 | |
227 | - if (null !== $value && !$option->acceptValue()) { |
|
228 | - throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
227 | + if ( null !== $value && ! $option->acceptValue() ) { |
|
228 | + throw new RuntimeException( sprintf( 'The "--%s" option does not accept a value.', $name ) ); |
|
229 | 229 | } |
230 | 230 | |
231 | - if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { |
|
231 | + if ( \in_array( $value, [ '', null ], true ) && $option->acceptValue() && \count( $this->parsed ) ) { |
|
232 | 232 | // if option accepts an optional or mandatory argument |
233 | 233 | // let's see if there is one provided |
234 | - $next = array_shift($this->parsed); |
|
235 | - if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) { |
|
234 | + $next = array_shift( $this->parsed ); |
|
235 | + if ( ( isset( $next[ 0 ] ) && '-' !== $next[ 0 ] ) || \in_array( $next, [ '', null ], true ) ) { |
|
236 | 236 | $value = $next; |
237 | 237 | } else { |
238 | - array_unshift($this->parsed, $next); |
|
238 | + array_unshift( $this->parsed, $next ); |
|
239 | 239 | } |
240 | 240 | } |
241 | 241 | |
242 | - if (null === $value) { |
|
243 | - if ($option->isValueRequired()) { |
|
244 | - throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
|
242 | + if ( null === $value ) { |
|
243 | + if ( $option->isValueRequired() ) { |
|
244 | + throw new RuntimeException( sprintf( 'The "--%s" option requires a value.', $name ) ); |
|
245 | 245 | } |
246 | 246 | |
247 | - if (!$option->isArray() && !$option->isValueOptional()) { |
|
247 | + if ( ! $option->isArray() && ! $option->isValueOptional() ) { |
|
248 | 248 | $value = true; |
249 | 249 | } |
250 | 250 | } |
251 | 251 | |
252 | - if ($option->isArray()) { |
|
253 | - $this->options[$name][] = $value; |
|
252 | + if ( $option->isArray() ) { |
|
253 | + $this->options[ $name ][ ] = $value; |
|
254 | 254 | } else { |
255 | - $this->options[$name] = $value; |
|
255 | + $this->options[ $name ] = $value; |
|
256 | 256 | } |
257 | 257 | } |
258 | 258 | |
@@ -262,25 +262,25 @@ discard block |
||
262 | 262 | public function getFirstArgument() |
263 | 263 | { |
264 | 264 | $isOption = false; |
265 | - foreach ($this->tokens as $i => $token) { |
|
266 | - if ($token && '-' === $token[0]) { |
|
267 | - if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { |
|
265 | + foreach ( $this->tokens as $i => $token ) { |
|
266 | + if ( $token && '-' === $token[ 0 ] ) { |
|
267 | + if ( str_contains( $token, '=' ) || ! isset( $this->tokens[ $i + 1 ] ) ) { |
|
268 | 268 | continue; |
269 | 269 | } |
270 | 270 | |
271 | 271 | // If it's a long option, consider that everything after "--" is the option name. |
272 | 272 | // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) |
273 | - $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); |
|
274 | - if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { |
|
273 | + $name = '-' === $token[ 1 ] ? substr( $token, 2 ) : substr( $token, -1 ); |
|
274 | + if ( ! isset( $this->options[ $name ] ) && ! $this->definition->hasShortcut( $name ) ) { |
|
275 | 275 | // noop |
276 | - } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { |
|
276 | + } elseif ( ( isset( $this->options[ $name ] ) || isset( $this->options[ $name = $this->definition->shortcutToName( $name ) ] ) ) && $this->tokens[ $i + 1 ] === $this->options[ $name ] ) { |
|
277 | 277 | $isOption = true; |
278 | 278 | } |
279 | 279 | |
280 | 280 | continue; |
281 | 281 | } |
282 | 282 | |
283 | - if ($isOption) { |
|
283 | + if ( $isOption ) { |
|
284 | 284 | $isOption = false; |
285 | 285 | continue; |
286 | 286 | } |
@@ -294,20 +294,20 @@ discard block |
||
294 | 294 | /** |
295 | 295 | * {@inheritdoc} |
296 | 296 | */ |
297 | - public function hasParameterOption($values, bool $onlyParams = false) |
|
297 | + public function hasParameterOption( $values, bool $onlyParams = false ) |
|
298 | 298 | { |
299 | - $values = (array) $values; |
|
299 | + $values = (array)$values; |
|
300 | 300 | |
301 | - foreach ($this->tokens as $token) { |
|
302 | - if ($onlyParams && '--' === $token) { |
|
301 | + foreach ( $this->tokens as $token ) { |
|
302 | + if ( $onlyParams && '--' === $token ) { |
|
303 | 303 | return false; |
304 | 304 | } |
305 | - foreach ($values as $value) { |
|
305 | + foreach ( $values as $value ) { |
|
306 | 306 | // Options with values: |
307 | 307 | // For long options, test for '--option=' at beginning |
308 | 308 | // For short options, test for '-o' at beginning |
309 | - $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
310 | - if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { |
|
309 | + $leading = str_starts_with( $value, '--' ) ? $value . '=' : $value; |
|
310 | + if ( $token === $value || '' !== $leading && str_starts_with( $token, $leading ) ) { |
|
311 | 311 | return true; |
312 | 312 | } |
313 | 313 | } |
@@ -319,27 +319,27 @@ discard block |
||
319 | 319 | /** |
320 | 320 | * {@inheritdoc} |
321 | 321 | */ |
322 | - public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
322 | + public function getParameterOption( $values, $default = false, bool $onlyParams = false ) |
|
323 | 323 | { |
324 | - $values = (array) $values; |
|
324 | + $values = (array)$values; |
|
325 | 325 | $tokens = $this->tokens; |
326 | 326 | |
327 | - while (0 < \count($tokens)) { |
|
328 | - $token = array_shift($tokens); |
|
329 | - if ($onlyParams && '--' === $token) { |
|
327 | + while ( 0 < \count( $tokens ) ) { |
|
328 | + $token = array_shift( $tokens ); |
|
329 | + if ( $onlyParams && '--' === $token ) { |
|
330 | 330 | return $default; |
331 | 331 | } |
332 | 332 | |
333 | - foreach ($values as $value) { |
|
334 | - if ($token === $value) { |
|
335 | - return array_shift($tokens); |
|
333 | + foreach ( $values as $value ) { |
|
334 | + if ( $token === $value ) { |
|
335 | + return array_shift( $tokens ); |
|
336 | 336 | } |
337 | 337 | // Options with values: |
338 | 338 | // For long options, test for '--option=' at beginning |
339 | 339 | // For short options, test for '-o' at beginning |
340 | - $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
341 | - if ('' !== $leading && str_starts_with($token, $leading)) { |
|
342 | - return substr($token, \strlen($leading)); |
|
340 | + $leading = str_starts_with( $value, '--' ) ? $value . '=' : $value; |
|
341 | + if ( '' !== $leading && str_starts_with( $token, $leading ) ) { |
|
342 | + return substr( $token, \strlen( $leading ) ); |
|
343 | 343 | } |
344 | 344 | } |
345 | 345 | } |
@@ -354,18 +354,18 @@ discard block |
||
354 | 354 | */ |
355 | 355 | public function __toString() |
356 | 356 | { |
357 | - $tokens = array_map(function ($token) { |
|
358 | - if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
|
359 | - return $match[1].$this->escapeToken($match[2]); |
|
357 | + $tokens = array_map( function( $token ) { |
|
358 | + if ( preg_match( '{^(-[^=]+=)(.+)}', $token, $match ) ) { |
|
359 | + return $match[ 1 ] . $this->escapeToken( $match[ 2 ] ); |
|
360 | 360 | } |
361 | 361 | |
362 | - if ($token && '-' !== $token[0]) { |
|
363 | - return $this->escapeToken($token); |
|
362 | + if ( $token && '-' !== $token[ 0 ] ) { |
|
363 | + return $this->escapeToken( $token ); |
|
364 | 364 | } |
365 | 365 | |
366 | 366 | return $token; |
367 | - }, $this->tokens); |
|
367 | + }, $this->tokens ); |
|
368 | 368 | |
369 | - return implode(' ', $tokens); |
|
369 | + return implode( ' ', $tokens ); |
|
370 | 370 | } |
371 | 371 | } |
@@ -38,13 +38,11 @@ discard block |
||
38 | 38 | * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html |
39 | 39 | * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02 |
40 | 40 | */ |
41 | -class ArgvInput extends Input |
|
42 | -{ |
|
41 | +class ArgvInput extends Input { |
|
43 | 42 | private $tokens; |
44 | 43 | private $parsed; |
45 | 44 | |
46 | - public function __construct(array $argv = null, InputDefinition $definition = null) |
|
47 | - { |
|
45 | + public function __construct(array $argv = null, InputDefinition $definition = null) { |
|
48 | 46 | $argv = $argv ?? $_SERVER['argv'] ?? []; |
49 | 47 | |
50 | 48 | // strip the application name |
@@ -55,16 +53,14 @@ discard block |
||
55 | 53 | parent::__construct($definition); |
56 | 54 | } |
57 | 55 | |
58 | - protected function setTokens(array $tokens) |
|
59 | - { |
|
56 | + protected function setTokens(array $tokens) { |
|
60 | 57 | $this->tokens = $tokens; |
61 | 58 | } |
62 | 59 | |
63 | 60 | /** |
64 | 61 | * {@inheritdoc} |
65 | 62 | */ |
66 | - protected function parse() |
|
67 | - { |
|
63 | + protected function parse() { |
|
68 | 64 | $parseOptions = true; |
69 | 65 | $this->parsed = $this->tokens; |
70 | 66 | while (null !== $token = array_shift($this->parsed)) { |
@@ -85,8 +81,7 @@ discard block |
||
85 | 81 | /** |
86 | 82 | * Parses a short option. |
87 | 83 | */ |
88 | - private function parseShortOption(string $token) |
|
89 | - { |
|
84 | + private function parseShortOption(string $token) { |
|
90 | 85 | $name = substr($token, 1); |
91 | 86 | |
92 | 87 | if (\strlen($name) > 1) { |
@@ -106,8 +101,7 @@ discard block |
||
106 | 101 | * |
107 | 102 | * @throws RuntimeException When option given doesn't exist |
108 | 103 | */ |
109 | - private function parseShortOptionSet(string $name) |
|
110 | - { |
|
104 | + private function parseShortOptionSet(string $name) { |
|
111 | 105 | $len = \strlen($name); |
112 | 106 | for ($i = 0; $i < $len; ++$i) { |
113 | 107 | if (!$this->definition->hasShortcut($name[$i])) { |
@@ -129,8 +123,7 @@ discard block |
||
129 | 123 | /** |
130 | 124 | * Parses a long option. |
131 | 125 | */ |
132 | - private function parseLongOption(string $token) |
|
133 | - { |
|
126 | + private function parseLongOption(string $token) { |
|
134 | 127 | $name = substr($token, 2); |
135 | 128 | |
136 | 129 | if (false !== $pos = strpos($name, '=')) { |
@@ -148,8 +141,7 @@ discard block |
||
148 | 141 | * |
149 | 142 | * @throws RuntimeException When too many arguments are given |
150 | 143 | */ |
151 | - private function parseArgument(string $token) |
|
152 | - { |
|
144 | + private function parseArgument(string $token) { |
|
153 | 145 | $c = \count($this->arguments); |
154 | 146 | |
155 | 147 | // if input is expecting another argument, add it |
@@ -192,8 +184,7 @@ discard block |
||
192 | 184 | * |
193 | 185 | * @throws RuntimeException When option given doesn't exist |
194 | 186 | */ |
195 | - private function addShortOption(string $shortcut, $value) |
|
196 | - { |
|
187 | + private function addShortOption(string $shortcut, $value) { |
|
197 | 188 | if (!$this->definition->hasShortcut($shortcut)) { |
198 | 189 | throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
199 | 190 | } |
@@ -206,8 +197,7 @@ discard block |
||
206 | 197 | * |
207 | 198 | * @throws RuntimeException When option given doesn't exist |
208 | 199 | */ |
209 | - private function addLongOption(string $name, $value) |
|
210 | - { |
|
200 | + private function addLongOption(string $name, $value) { |
|
211 | 201 | if (!$this->definition->hasOption($name)) { |
212 | 202 | if (!$this->definition->hasNegation($name)) { |
213 | 203 | throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
@@ -259,8 +249,7 @@ discard block |
||
259 | 249 | /** |
260 | 250 | * {@inheritdoc} |
261 | 251 | */ |
262 | - public function getFirstArgument() |
|
263 | - { |
|
252 | + public function getFirstArgument() { |
|
264 | 253 | $isOption = false; |
265 | 254 | foreach ($this->tokens as $i => $token) { |
266 | 255 | if ($token && '-' === $token[0]) { |
@@ -294,8 +283,7 @@ discard block |
||
294 | 283 | /** |
295 | 284 | * {@inheritdoc} |
296 | 285 | */ |
297 | - public function hasParameterOption($values, bool $onlyParams = false) |
|
298 | - { |
|
286 | + public function hasParameterOption($values, bool $onlyParams = false) { |
|
299 | 287 | $values = (array) $values; |
300 | 288 | |
301 | 289 | foreach ($this->tokens as $token) { |
@@ -319,8 +307,7 @@ discard block |
||
319 | 307 | /** |
320 | 308 | * {@inheritdoc} |
321 | 309 | */ |
322 | - public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
323 | - { |
|
310 | + public function getParameterOption($values, $default = false, bool $onlyParams = false) { |
|
324 | 311 | $values = (array) $values; |
325 | 312 | $tokens = $this->tokens; |
326 | 313 | |
@@ -352,8 +339,7 @@ discard block |
||
352 | 339 | * |
353 | 340 | * @return string |
354 | 341 | */ |
355 | - public function __toString() |
|
356 | - { |
|
342 | + public function __toString() { |
|
357 | 343 | $tokens = array_map(function ($token) { |
358 | 344 | if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
359 | 345 | return $match[1].$this->escapeToken($match[2]); |
@@ -19,19 +19,19 @@ |
||
19 | 19 | */ |
20 | 20 | interface StreamableInputInterface extends InputInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * Sets the input stream to read from when interacting with the user. |
|
24 | - * |
|
25 | - * This is mainly useful for testing purpose. |
|
26 | - * |
|
27 | - * @param resource $stream The input stream |
|
28 | - */ |
|
29 | - public function setStream($stream); |
|
22 | + /** |
|
23 | + * Sets the input stream to read from when interacting with the user. |
|
24 | + * |
|
25 | + * This is mainly useful for testing purpose. |
|
26 | + * |
|
27 | + * @param resource $stream The input stream |
|
28 | + */ |
|
29 | + public function setStream($stream); |
|
30 | 30 | |
31 | - /** |
|
32 | - * Returns the input stream. |
|
33 | - * |
|
34 | - * @return resource|null |
|
35 | - */ |
|
36 | - public function getStream(); |
|
31 | + /** |
|
32 | + * Returns the input stream. |
|
33 | + * |
|
34 | + * @return resource|null |
|
35 | + */ |
|
36 | + public function getStream(); |
|
37 | 37 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | * |
27 | 27 | * @param resource $stream The input stream |
28 | 28 | */ |
29 | - public function setStream($stream); |
|
29 | + public function setStream( $stream ); |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Returns the input stream. |
@@ -17,8 +17,7 @@ |
||
17 | 17 | * |
18 | 18 | * @author Robin Chalas <[email protected]> |
19 | 19 | */ |
20 | -interface StreamableInputInterface extends InputInterface |
|
21 | -{ |
|
20 | +interface StreamableInputInterface extends InputInterface { |
|
22 | 21 | /** |
23 | 22 | * Sets the input stream to read from when interacting with the user. |
24 | 23 | * |
@@ -21,131 +21,131 @@ |
||
21 | 21 | */ |
22 | 22 | interface InputInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * Returns the first argument from the raw parameters (not parsed). |
|
26 | - * |
|
27 | - * @return string|null The value of the first argument or null otherwise |
|
28 | - */ |
|
29 | - public function getFirstArgument(); |
|
30 | - |
|
31 | - /** |
|
32 | - * Returns true if the raw parameters (not parsed) contain a value. |
|
33 | - * |
|
34 | - * This method is to be used to introspect the input parameters |
|
35 | - * before they have been validated. It must be used carefully. |
|
36 | - * Does not necessarily return the correct result for short options |
|
37 | - * when multiple flags are combined in the same option. |
|
38 | - * |
|
39 | - * @param string|array $values The values to look for in the raw parameters (can be an array) |
|
40 | - * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
41 | - * |
|
42 | - * @return bool true if the value is contained in the raw parameters |
|
43 | - */ |
|
44 | - public function hasParameterOption($values, bool $onlyParams = false); |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns the value of a raw option (not parsed). |
|
48 | - * |
|
49 | - * This method is to be used to introspect the input parameters |
|
50 | - * before they have been validated. It must be used carefully. |
|
51 | - * Does not necessarily return the correct result for short options |
|
52 | - * when multiple flags are combined in the same option. |
|
53 | - * |
|
54 | - * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
|
55 | - * @param string|bool|int|float|array|null $default The default value to return if no result is found |
|
56 | - * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
57 | - * |
|
58 | - * @return mixed The option value |
|
59 | - */ |
|
60 | - public function getParameterOption($values, $default = false, bool $onlyParams = false); |
|
61 | - |
|
62 | - /** |
|
63 | - * Binds the current Input instance with the given arguments and options. |
|
64 | - * |
|
65 | - * @throws RuntimeException |
|
66 | - */ |
|
67 | - public function bind(InputDefinition $definition); |
|
68 | - |
|
69 | - /** |
|
70 | - * Validates the input. |
|
71 | - * |
|
72 | - * @throws RuntimeException When not enough arguments are given |
|
73 | - */ |
|
74 | - public function validate(); |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns all the given arguments merged with the default values. |
|
78 | - * |
|
79 | - * @return array<string|bool|int|float|array|null> |
|
80 | - */ |
|
81 | - public function getArguments(); |
|
82 | - |
|
83 | - /** |
|
84 | - * Returns the argument value for a given argument name. |
|
85 | - * |
|
86 | - * @return mixed |
|
87 | - * |
|
88 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
89 | - */ |
|
90 | - public function getArgument(string $name); |
|
91 | - |
|
92 | - /** |
|
93 | - * Sets an argument value by name. |
|
94 | - * |
|
95 | - * @param mixed $value The argument value |
|
96 | - * |
|
97 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
98 | - */ |
|
99 | - public function setArgument(string $name, $value); |
|
100 | - |
|
101 | - /** |
|
102 | - * Returns true if an InputArgument object exists by name or position. |
|
103 | - * |
|
104 | - * @return bool true if the InputArgument object exists, false otherwise |
|
105 | - */ |
|
106 | - public function hasArgument(string $name); |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns all the given options merged with the default values. |
|
110 | - * |
|
111 | - * @return array<string|bool|int|float|array|null> |
|
112 | - */ |
|
113 | - public function getOptions(); |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns the option value for a given option name. |
|
117 | - * |
|
118 | - * @return mixed |
|
119 | - * |
|
120 | - * @throws InvalidArgumentException When option given doesn't exist |
|
121 | - */ |
|
122 | - public function getOption(string $name); |
|
123 | - |
|
124 | - /** |
|
125 | - * Sets an option value by name. |
|
126 | - * |
|
127 | - * @param mixed $value The option value |
|
128 | - * |
|
129 | - * @throws InvalidArgumentException When option given doesn't exist |
|
130 | - */ |
|
131 | - public function setOption(string $name, $value); |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns true if an InputOption object exists by name. |
|
135 | - * |
|
136 | - * @return bool true if the InputOption object exists, false otherwise |
|
137 | - */ |
|
138 | - public function hasOption(string $name); |
|
139 | - |
|
140 | - /** |
|
141 | - * Is this input means interactive? |
|
142 | - * |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - public function isInteractive(); |
|
146 | - |
|
147 | - /** |
|
148 | - * Sets the input interactivity. |
|
149 | - */ |
|
150 | - public function setInteractive(bool $interactive); |
|
24 | + /** |
|
25 | + * Returns the first argument from the raw parameters (not parsed). |
|
26 | + * |
|
27 | + * @return string|null The value of the first argument or null otherwise |
|
28 | + */ |
|
29 | + public function getFirstArgument(); |
|
30 | + |
|
31 | + /** |
|
32 | + * Returns true if the raw parameters (not parsed) contain a value. |
|
33 | + * |
|
34 | + * This method is to be used to introspect the input parameters |
|
35 | + * before they have been validated. It must be used carefully. |
|
36 | + * Does not necessarily return the correct result for short options |
|
37 | + * when multiple flags are combined in the same option. |
|
38 | + * |
|
39 | + * @param string|array $values The values to look for in the raw parameters (can be an array) |
|
40 | + * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
41 | + * |
|
42 | + * @return bool true if the value is contained in the raw parameters |
|
43 | + */ |
|
44 | + public function hasParameterOption($values, bool $onlyParams = false); |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns the value of a raw option (not parsed). |
|
48 | + * |
|
49 | + * This method is to be used to introspect the input parameters |
|
50 | + * before they have been validated. It must be used carefully. |
|
51 | + * Does not necessarily return the correct result for short options |
|
52 | + * when multiple flags are combined in the same option. |
|
53 | + * |
|
54 | + * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
|
55 | + * @param string|bool|int|float|array|null $default The default value to return if no result is found |
|
56 | + * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
57 | + * |
|
58 | + * @return mixed The option value |
|
59 | + */ |
|
60 | + public function getParameterOption($values, $default = false, bool $onlyParams = false); |
|
61 | + |
|
62 | + /** |
|
63 | + * Binds the current Input instance with the given arguments and options. |
|
64 | + * |
|
65 | + * @throws RuntimeException |
|
66 | + */ |
|
67 | + public function bind(InputDefinition $definition); |
|
68 | + |
|
69 | + /** |
|
70 | + * Validates the input. |
|
71 | + * |
|
72 | + * @throws RuntimeException When not enough arguments are given |
|
73 | + */ |
|
74 | + public function validate(); |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns all the given arguments merged with the default values. |
|
78 | + * |
|
79 | + * @return array<string|bool|int|float|array|null> |
|
80 | + */ |
|
81 | + public function getArguments(); |
|
82 | + |
|
83 | + /** |
|
84 | + * Returns the argument value for a given argument name. |
|
85 | + * |
|
86 | + * @return mixed |
|
87 | + * |
|
88 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
89 | + */ |
|
90 | + public function getArgument(string $name); |
|
91 | + |
|
92 | + /** |
|
93 | + * Sets an argument value by name. |
|
94 | + * |
|
95 | + * @param mixed $value The argument value |
|
96 | + * |
|
97 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
98 | + */ |
|
99 | + public function setArgument(string $name, $value); |
|
100 | + |
|
101 | + /** |
|
102 | + * Returns true if an InputArgument object exists by name or position. |
|
103 | + * |
|
104 | + * @return bool true if the InputArgument object exists, false otherwise |
|
105 | + */ |
|
106 | + public function hasArgument(string $name); |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns all the given options merged with the default values. |
|
110 | + * |
|
111 | + * @return array<string|bool|int|float|array|null> |
|
112 | + */ |
|
113 | + public function getOptions(); |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns the option value for a given option name. |
|
117 | + * |
|
118 | + * @return mixed |
|
119 | + * |
|
120 | + * @throws InvalidArgumentException When option given doesn't exist |
|
121 | + */ |
|
122 | + public function getOption(string $name); |
|
123 | + |
|
124 | + /** |
|
125 | + * Sets an option value by name. |
|
126 | + * |
|
127 | + * @param mixed $value The option value |
|
128 | + * |
|
129 | + * @throws InvalidArgumentException When option given doesn't exist |
|
130 | + */ |
|
131 | + public function setOption(string $name, $value); |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns true if an InputOption object exists by name. |
|
135 | + * |
|
136 | + * @return bool true if the InputOption object exists, false otherwise |
|
137 | + */ |
|
138 | + public function hasOption(string $name); |
|
139 | + |
|
140 | + /** |
|
141 | + * Is this input means interactive? |
|
142 | + * |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + public function isInteractive(); |
|
146 | + |
|
147 | + /** |
|
148 | + * Sets the input interactivity. |
|
149 | + */ |
|
150 | + public function setInteractive(bool $interactive); |
|
151 | 151 | } |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return bool true if the value is contained in the raw parameters |
43 | 43 | */ |
44 | - public function hasParameterOption($values, bool $onlyParams = false); |
|
44 | + public function hasParameterOption( $values, bool $onlyParams = false ); |
|
45 | 45 | |
46 | 46 | /** |
47 | 47 | * Returns the value of a raw option (not parsed). |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | * |
58 | 58 | * @return mixed The option value |
59 | 59 | */ |
60 | - public function getParameterOption($values, $default = false, bool $onlyParams = false); |
|
60 | + public function getParameterOption( $values, $default = false, bool $onlyParams = false ); |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * Binds the current Input instance with the given arguments and options. |
64 | 64 | * |
65 | 65 | * @throws RuntimeException |
66 | 66 | */ |
67 | - public function bind(InputDefinition $definition); |
|
67 | + public function bind( InputDefinition $definition ); |
|
68 | 68 | |
69 | 69 | /** |
70 | 70 | * Validates the input. |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * |
88 | 88 | * @throws InvalidArgumentException When argument given doesn't exist |
89 | 89 | */ |
90 | - public function getArgument(string $name); |
|
90 | + public function getArgument( string $name ); |
|
91 | 91 | |
92 | 92 | /** |
93 | 93 | * Sets an argument value by name. |
@@ -96,14 +96,14 @@ discard block |
||
96 | 96 | * |
97 | 97 | * @throws InvalidArgumentException When argument given doesn't exist |
98 | 98 | */ |
99 | - public function setArgument(string $name, $value); |
|
99 | + public function setArgument( string $name, $value ); |
|
100 | 100 | |
101 | 101 | /** |
102 | 102 | * Returns true if an InputArgument object exists by name or position. |
103 | 103 | * |
104 | 104 | * @return bool true if the InputArgument object exists, false otherwise |
105 | 105 | */ |
106 | - public function hasArgument(string $name); |
|
106 | + public function hasArgument( string $name ); |
|
107 | 107 | |
108 | 108 | /** |
109 | 109 | * Returns all the given options merged with the default values. |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * |
120 | 120 | * @throws InvalidArgumentException When option given doesn't exist |
121 | 121 | */ |
122 | - public function getOption(string $name); |
|
122 | + public function getOption( string $name ); |
|
123 | 123 | |
124 | 124 | /** |
125 | 125 | * Sets an option value by name. |
@@ -128,14 +128,14 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @throws InvalidArgumentException When option given doesn't exist |
130 | 130 | */ |
131 | - public function setOption(string $name, $value); |
|
131 | + public function setOption( string $name, $value ); |
|
132 | 132 | |
133 | 133 | /** |
134 | 134 | * Returns true if an InputOption object exists by name. |
135 | 135 | * |
136 | 136 | * @return bool true if the InputOption object exists, false otherwise |
137 | 137 | */ |
138 | - public function hasOption(string $name); |
|
138 | + public function hasOption( string $name ); |
|
139 | 139 | |
140 | 140 | /** |
141 | 141 | * Is this input means interactive? |
@@ -147,5 +147,5 @@ discard block |
||
147 | 147 | /** |
148 | 148 | * Sets the input interactivity. |
149 | 149 | */ |
150 | - public function setInteractive(bool $interactive); |
|
150 | + public function setInteractive( bool $interactive ); |
|
151 | 151 | } |
@@ -19,8 +19,7 @@ |
||
19 | 19 | * |
20 | 20 | * @author Fabien Potencier <[email protected]> |
21 | 21 | */ |
22 | -interface InputInterface |
|
23 | -{ |
|
22 | +interface InputInterface { |
|
24 | 23 | /** |
25 | 24 | * Returns the first argument from the raw parameters (not parsed). |
26 | 25 | * |