@@ -19,8 +19,8 @@ |
||
19 | 19 | */ |
20 | 20 | interface InputAwareInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * Sets the Console Input. |
|
24 | - */ |
|
25 | - public function setInput(InputInterface $input); |
|
22 | + /** |
|
23 | + * Sets the Console Input. |
|
24 | + */ |
|
25 | + public function setInput(InputInterface $input); |
|
26 | 26 | } |
@@ -27,187 +27,187 @@ |
||
27 | 27 | */ |
28 | 28 | abstract class Input implements InputInterface, StreamableInputInterface |
29 | 29 | { |
30 | - protected $definition; |
|
31 | - protected $stream; |
|
32 | - protected $options = []; |
|
33 | - protected $arguments = []; |
|
34 | - protected $interactive = true; |
|
35 | - |
|
36 | - public function __construct(InputDefinition $definition = null) |
|
37 | - { |
|
38 | - if (null === $definition) { |
|
39 | - $this->definition = new InputDefinition(); |
|
40 | - } else { |
|
41 | - $this->bind($definition); |
|
42 | - $this->validate(); |
|
43 | - } |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - public function bind(InputDefinition $definition) |
|
50 | - { |
|
51 | - $this->arguments = []; |
|
52 | - $this->options = []; |
|
53 | - $this->definition = $definition; |
|
54 | - |
|
55 | - $this->parse(); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Processes command line arguments. |
|
60 | - */ |
|
61 | - abstract protected function parse(); |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - public function validate() |
|
67 | - { |
|
68 | - $definition = $this->definition; |
|
69 | - $givenArguments = $this->arguments; |
|
70 | - |
|
71 | - $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) { |
|
72 | - return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); |
|
73 | - }); |
|
74 | - |
|
75 | - if (\count($missingArguments) > 0) { |
|
76 | - throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments))); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * {@inheritdoc} |
|
82 | - */ |
|
83 | - public function isInteractive() |
|
84 | - { |
|
85 | - return $this->interactive; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * {@inheritdoc} |
|
90 | - */ |
|
91 | - public function setInteractive(bool $interactive) |
|
92 | - { |
|
93 | - $this->interactive = $interactive; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - public function getArguments() |
|
100 | - { |
|
101 | - return array_merge($this->definition->getArgumentDefaults(), $this->arguments); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * {@inheritdoc} |
|
106 | - */ |
|
107 | - public function getArgument(string $name) |
|
108 | - { |
|
109 | - if (!$this->definition->hasArgument($name)) { |
|
110 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
111 | - } |
|
112 | - |
|
113 | - return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * {@inheritdoc} |
|
118 | - */ |
|
119 | - public function setArgument(string $name, $value) |
|
120 | - { |
|
121 | - if (!$this->definition->hasArgument($name)) { |
|
122 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
123 | - } |
|
124 | - |
|
125 | - $this->arguments[$name] = $value; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * {@inheritdoc} |
|
130 | - */ |
|
131 | - public function hasArgument(string $name) |
|
132 | - { |
|
133 | - return $this->definition->hasArgument($name); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * {@inheritdoc} |
|
138 | - */ |
|
139 | - public function getOptions() |
|
140 | - { |
|
141 | - return array_merge($this->definition->getOptionDefaults(), $this->options); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * {@inheritdoc} |
|
146 | - */ |
|
147 | - public function getOption(string $name) |
|
148 | - { |
|
149 | - if ($this->definition->hasNegation($name)) { |
|
150 | - if (null === $value = $this->getOption($this->definition->negationToName($name))) { |
|
151 | - return $value; |
|
152 | - } |
|
153 | - |
|
154 | - return !$value; |
|
155 | - } |
|
156 | - |
|
157 | - if (!$this->definition->hasOption($name)) { |
|
158 | - throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
159 | - } |
|
160 | - |
|
161 | - return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * {@inheritdoc} |
|
166 | - */ |
|
167 | - public function setOption(string $name, $value) |
|
168 | - { |
|
169 | - if ($this->definition->hasNegation($name)) { |
|
170 | - $this->options[$this->definition->negationToName($name)] = !$value; |
|
171 | - |
|
172 | - return; |
|
173 | - } elseif (!$this->definition->hasOption($name)) { |
|
174 | - throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
175 | - } |
|
176 | - |
|
177 | - $this->options[$name] = $value; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * {@inheritdoc} |
|
182 | - */ |
|
183 | - public function hasOption(string $name) |
|
184 | - { |
|
185 | - return $this->definition->hasOption($name) || $this->definition->hasNegation($name); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Escapes a token through escapeshellarg if it contains unsafe chars. |
|
190 | - * |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - public function escapeToken(string $token) |
|
194 | - { |
|
195 | - return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * {@inheritdoc} |
|
200 | - */ |
|
201 | - public function setStream($stream) |
|
202 | - { |
|
203 | - $this->stream = $stream; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * {@inheritdoc} |
|
208 | - */ |
|
209 | - public function getStream() |
|
210 | - { |
|
211 | - return $this->stream; |
|
212 | - } |
|
30 | + protected $definition; |
|
31 | + protected $stream; |
|
32 | + protected $options = []; |
|
33 | + protected $arguments = []; |
|
34 | + protected $interactive = true; |
|
35 | + |
|
36 | + public function __construct(InputDefinition $definition = null) |
|
37 | + { |
|
38 | + if (null === $definition) { |
|
39 | + $this->definition = new InputDefinition(); |
|
40 | + } else { |
|
41 | + $this->bind($definition); |
|
42 | + $this->validate(); |
|
43 | + } |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + public function bind(InputDefinition $definition) |
|
50 | + { |
|
51 | + $this->arguments = []; |
|
52 | + $this->options = []; |
|
53 | + $this->definition = $definition; |
|
54 | + |
|
55 | + $this->parse(); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Processes command line arguments. |
|
60 | + */ |
|
61 | + abstract protected function parse(); |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + public function validate() |
|
67 | + { |
|
68 | + $definition = $this->definition; |
|
69 | + $givenArguments = $this->arguments; |
|
70 | + |
|
71 | + $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) { |
|
72 | + return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); |
|
73 | + }); |
|
74 | + |
|
75 | + if (\count($missingArguments) > 0) { |
|
76 | + throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments))); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * {@inheritdoc} |
|
82 | + */ |
|
83 | + public function isInteractive() |
|
84 | + { |
|
85 | + return $this->interactive; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * {@inheritdoc} |
|
90 | + */ |
|
91 | + public function setInteractive(bool $interactive) |
|
92 | + { |
|
93 | + $this->interactive = $interactive; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + public function getArguments() |
|
100 | + { |
|
101 | + return array_merge($this->definition->getArgumentDefaults(), $this->arguments); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * {@inheritdoc} |
|
106 | + */ |
|
107 | + public function getArgument(string $name) |
|
108 | + { |
|
109 | + if (!$this->definition->hasArgument($name)) { |
|
110 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
111 | + } |
|
112 | + |
|
113 | + return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * {@inheritdoc} |
|
118 | + */ |
|
119 | + public function setArgument(string $name, $value) |
|
120 | + { |
|
121 | + if (!$this->definition->hasArgument($name)) { |
|
122 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
123 | + } |
|
124 | + |
|
125 | + $this->arguments[$name] = $value; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * {@inheritdoc} |
|
130 | + */ |
|
131 | + public function hasArgument(string $name) |
|
132 | + { |
|
133 | + return $this->definition->hasArgument($name); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * {@inheritdoc} |
|
138 | + */ |
|
139 | + public function getOptions() |
|
140 | + { |
|
141 | + return array_merge($this->definition->getOptionDefaults(), $this->options); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * {@inheritdoc} |
|
146 | + */ |
|
147 | + public function getOption(string $name) |
|
148 | + { |
|
149 | + if ($this->definition->hasNegation($name)) { |
|
150 | + if (null === $value = $this->getOption($this->definition->negationToName($name))) { |
|
151 | + return $value; |
|
152 | + } |
|
153 | + |
|
154 | + return !$value; |
|
155 | + } |
|
156 | + |
|
157 | + if (!$this->definition->hasOption($name)) { |
|
158 | + throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
159 | + } |
|
160 | + |
|
161 | + return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * {@inheritdoc} |
|
166 | + */ |
|
167 | + public function setOption(string $name, $value) |
|
168 | + { |
|
169 | + if ($this->definition->hasNegation($name)) { |
|
170 | + $this->options[$this->definition->negationToName($name)] = !$value; |
|
171 | + |
|
172 | + return; |
|
173 | + } elseif (!$this->definition->hasOption($name)) { |
|
174 | + throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
175 | + } |
|
176 | + |
|
177 | + $this->options[$name] = $value; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * {@inheritdoc} |
|
182 | + */ |
|
183 | + public function hasOption(string $name) |
|
184 | + { |
|
185 | + return $this->definition->hasOption($name) || $this->definition->hasNegation($name); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Escapes a token through escapeshellarg if it contains unsafe chars. |
|
190 | + * |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + public function escapeToken(string $token) |
|
194 | + { |
|
195 | + return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * {@inheritdoc} |
|
200 | + */ |
|
201 | + public function setStream($stream) |
|
202 | + { |
|
203 | + $this->stream = $stream; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * {@inheritdoc} |
|
208 | + */ |
|
209 | + public function getStream() |
|
210 | + { |
|
211 | + return $this->stream; |
|
212 | + } |
|
213 | 213 | } |
@@ -25,186 +25,186 @@ |
||
25 | 25 | */ |
26 | 26 | class ArrayInput extends Input |
27 | 27 | { |
28 | - private $parameters; |
|
29 | - |
|
30 | - public function __construct(array $parameters, InputDefinition $definition = null) |
|
31 | - { |
|
32 | - $this->parameters = $parameters; |
|
33 | - |
|
34 | - parent::__construct($definition); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function getFirstArgument() |
|
41 | - { |
|
42 | - foreach ($this->parameters as $param => $value) { |
|
43 | - if ($param && \is_string($param) && '-' === $param[0]) { |
|
44 | - continue; |
|
45 | - } |
|
46 | - |
|
47 | - return $value; |
|
48 | - } |
|
49 | - |
|
50 | - return null; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - public function hasParameterOption($values, bool $onlyParams = false) |
|
57 | - { |
|
58 | - $values = (array) $values; |
|
59 | - |
|
60 | - foreach ($this->parameters as $k => $v) { |
|
61 | - if (!\is_int($k)) { |
|
62 | - $v = $k; |
|
63 | - } |
|
64 | - |
|
65 | - if ($onlyParams && '--' === $v) { |
|
66 | - return false; |
|
67 | - } |
|
68 | - |
|
69 | - if (\in_array($v, $values)) { |
|
70 | - return true; |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - return false; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * {@inheritdoc} |
|
79 | - */ |
|
80 | - public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
81 | - { |
|
82 | - $values = (array) $values; |
|
83 | - |
|
84 | - foreach ($this->parameters as $k => $v) { |
|
85 | - if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) { |
|
86 | - return $default; |
|
87 | - } |
|
88 | - |
|
89 | - if (\is_int($k)) { |
|
90 | - if (\in_array($v, $values)) { |
|
91 | - return true; |
|
92 | - } |
|
93 | - } elseif (\in_array($k, $values)) { |
|
94 | - return $v; |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - return $default; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Returns a stringified representation of the args passed to the command. |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function __toString() |
|
107 | - { |
|
108 | - $params = []; |
|
109 | - foreach ($this->parameters as $param => $val) { |
|
110 | - if ($param && \is_string($param) && '-' === $param[0]) { |
|
111 | - $glue = ('-' === $param[1]) ? '=' : ' '; |
|
112 | - if (\is_array($val)) { |
|
113 | - foreach ($val as $v) { |
|
114 | - $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); |
|
115 | - } |
|
116 | - } else { |
|
117 | - $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); |
|
118 | - } |
|
119 | - } else { |
|
120 | - $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - return implode(' ', $params); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * {@inheritdoc} |
|
129 | - */ |
|
130 | - protected function parse() |
|
131 | - { |
|
132 | - foreach ($this->parameters as $key => $value) { |
|
133 | - if ('--' === $key) { |
|
134 | - return; |
|
135 | - } |
|
136 | - if (str_starts_with($key, '--')) { |
|
137 | - $this->addLongOption(substr($key, 2), $value); |
|
138 | - } elseif (str_starts_with($key, '-')) { |
|
139 | - $this->addShortOption(substr($key, 1), $value); |
|
140 | - } else { |
|
141 | - $this->addArgument($key, $value); |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Adds a short option value. |
|
148 | - * |
|
149 | - * @throws InvalidOptionException When option given doesn't exist |
|
150 | - */ |
|
151 | - private function addShortOption(string $shortcut, $value) |
|
152 | - { |
|
153 | - if (!$this->definition->hasShortcut($shortcut)) { |
|
154 | - throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
155 | - } |
|
156 | - |
|
157 | - $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Adds a long option value. |
|
162 | - * |
|
163 | - * @throws InvalidOptionException When option given doesn't exist |
|
164 | - * @throws InvalidOptionException When a required value is missing |
|
165 | - */ |
|
166 | - private function addLongOption(string $name, $value) |
|
167 | - { |
|
168 | - if (!$this->definition->hasOption($name)) { |
|
169 | - if (!$this->definition->hasNegation($name)) { |
|
170 | - throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); |
|
171 | - } |
|
172 | - |
|
173 | - $optionName = $this->definition->negationToName($name); |
|
174 | - $this->options[$optionName] = false; |
|
175 | - |
|
176 | - return; |
|
177 | - } |
|
178 | - |
|
179 | - $option = $this->definition->getOption($name); |
|
180 | - |
|
181 | - if (null === $value) { |
|
182 | - if ($option->isValueRequired()) { |
|
183 | - throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); |
|
184 | - } |
|
185 | - |
|
186 | - if (!$option->isValueOptional()) { |
|
187 | - $value = true; |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - $this->options[$name] = $value; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Adds an argument value. |
|
196 | - * |
|
197 | - * @param string|int $name The argument name |
|
198 | - * @param mixed $value The value for the argument |
|
199 | - * |
|
200 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
201 | - */ |
|
202 | - private function addArgument($name, $value) |
|
203 | - { |
|
204 | - if (!$this->definition->hasArgument($name)) { |
|
205 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
206 | - } |
|
207 | - |
|
208 | - $this->arguments[$name] = $value; |
|
209 | - } |
|
28 | + private $parameters; |
|
29 | + |
|
30 | + public function __construct(array $parameters, InputDefinition $definition = null) |
|
31 | + { |
|
32 | + $this->parameters = $parameters; |
|
33 | + |
|
34 | + parent::__construct($definition); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function getFirstArgument() |
|
41 | + { |
|
42 | + foreach ($this->parameters as $param => $value) { |
|
43 | + if ($param && \is_string($param) && '-' === $param[0]) { |
|
44 | + continue; |
|
45 | + } |
|
46 | + |
|
47 | + return $value; |
|
48 | + } |
|
49 | + |
|
50 | + return null; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + public function hasParameterOption($values, bool $onlyParams = false) |
|
57 | + { |
|
58 | + $values = (array) $values; |
|
59 | + |
|
60 | + foreach ($this->parameters as $k => $v) { |
|
61 | + if (!\is_int($k)) { |
|
62 | + $v = $k; |
|
63 | + } |
|
64 | + |
|
65 | + if ($onlyParams && '--' === $v) { |
|
66 | + return false; |
|
67 | + } |
|
68 | + |
|
69 | + if (\in_array($v, $values)) { |
|
70 | + return true; |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + return false; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * {@inheritdoc} |
|
79 | + */ |
|
80 | + public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
81 | + { |
|
82 | + $values = (array) $values; |
|
83 | + |
|
84 | + foreach ($this->parameters as $k => $v) { |
|
85 | + if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) { |
|
86 | + return $default; |
|
87 | + } |
|
88 | + |
|
89 | + if (\is_int($k)) { |
|
90 | + if (\in_array($v, $values)) { |
|
91 | + return true; |
|
92 | + } |
|
93 | + } elseif (\in_array($k, $values)) { |
|
94 | + return $v; |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + return $default; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Returns a stringified representation of the args passed to the command. |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function __toString() |
|
107 | + { |
|
108 | + $params = []; |
|
109 | + foreach ($this->parameters as $param => $val) { |
|
110 | + if ($param && \is_string($param) && '-' === $param[0]) { |
|
111 | + $glue = ('-' === $param[1]) ? '=' : ' '; |
|
112 | + if (\is_array($val)) { |
|
113 | + foreach ($val as $v) { |
|
114 | + $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); |
|
115 | + } |
|
116 | + } else { |
|
117 | + $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); |
|
118 | + } |
|
119 | + } else { |
|
120 | + $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + return implode(' ', $params); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * {@inheritdoc} |
|
129 | + */ |
|
130 | + protected function parse() |
|
131 | + { |
|
132 | + foreach ($this->parameters as $key => $value) { |
|
133 | + if ('--' === $key) { |
|
134 | + return; |
|
135 | + } |
|
136 | + if (str_starts_with($key, '--')) { |
|
137 | + $this->addLongOption(substr($key, 2), $value); |
|
138 | + } elseif (str_starts_with($key, '-')) { |
|
139 | + $this->addShortOption(substr($key, 1), $value); |
|
140 | + } else { |
|
141 | + $this->addArgument($key, $value); |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Adds a short option value. |
|
148 | + * |
|
149 | + * @throws InvalidOptionException When option given doesn't exist |
|
150 | + */ |
|
151 | + private function addShortOption(string $shortcut, $value) |
|
152 | + { |
|
153 | + if (!$this->definition->hasShortcut($shortcut)) { |
|
154 | + throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
155 | + } |
|
156 | + |
|
157 | + $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Adds a long option value. |
|
162 | + * |
|
163 | + * @throws InvalidOptionException When option given doesn't exist |
|
164 | + * @throws InvalidOptionException When a required value is missing |
|
165 | + */ |
|
166 | + private function addLongOption(string $name, $value) |
|
167 | + { |
|
168 | + if (!$this->definition->hasOption($name)) { |
|
169 | + if (!$this->definition->hasNegation($name)) { |
|
170 | + throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); |
|
171 | + } |
|
172 | + |
|
173 | + $optionName = $this->definition->negationToName($name); |
|
174 | + $this->options[$optionName] = false; |
|
175 | + |
|
176 | + return; |
|
177 | + } |
|
178 | + |
|
179 | + $option = $this->definition->getOption($name); |
|
180 | + |
|
181 | + if (null === $value) { |
|
182 | + if ($option->isValueRequired()) { |
|
183 | + throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); |
|
184 | + } |
|
185 | + |
|
186 | + if (!$option->isValueOptional()) { |
|
187 | + $value = true; |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + $this->options[$name] = $value; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Adds an argument value. |
|
196 | + * |
|
197 | + * @param string|int $name The argument name |
|
198 | + * @param mixed $value The value for the argument |
|
199 | + * |
|
200 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
201 | + */ |
|
202 | + private function addArgument($name, $value) |
|
203 | + { |
|
204 | + if (!$this->definition->hasArgument($name)) { |
|
205 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
206 | + } |
|
207 | + |
|
208 | + $this->arguments[$name] = $value; |
|
209 | + } |
|
210 | 210 | } |
@@ -13,53 +13,53 @@ |
||
13 | 13 | |
14 | 14 | final class SignalRegistry |
15 | 15 | { |
16 | - private $signalHandlers = []; |
|
16 | + private $signalHandlers = []; |
|
17 | 17 | |
18 | - public function __construct() |
|
19 | - { |
|
20 | - if (\function_exists('pcntl_async_signals')) { |
|
21 | - pcntl_async_signals(true); |
|
22 | - } |
|
23 | - } |
|
18 | + public function __construct() |
|
19 | + { |
|
20 | + if (\function_exists('pcntl_async_signals')) { |
|
21 | + pcntl_async_signals(true); |
|
22 | + } |
|
23 | + } |
|
24 | 24 | |
25 | - public function register(int $signal, callable $signalHandler): void |
|
26 | - { |
|
27 | - if (!isset($this->signalHandlers[$signal])) { |
|
28 | - $previousCallback = pcntl_signal_get_handler($signal); |
|
25 | + public function register(int $signal, callable $signalHandler): void |
|
26 | + { |
|
27 | + if (!isset($this->signalHandlers[$signal])) { |
|
28 | + $previousCallback = pcntl_signal_get_handler($signal); |
|
29 | 29 | |
30 | - if (\is_callable($previousCallback)) { |
|
31 | - $this->signalHandlers[$signal][] = $previousCallback; |
|
32 | - } |
|
33 | - } |
|
30 | + if (\is_callable($previousCallback)) { |
|
31 | + $this->signalHandlers[$signal][] = $previousCallback; |
|
32 | + } |
|
33 | + } |
|
34 | 34 | |
35 | - $this->signalHandlers[$signal][] = $signalHandler; |
|
35 | + $this->signalHandlers[$signal][] = $signalHandler; |
|
36 | 36 | |
37 | - pcntl_signal($signal, [$this, 'handle']); |
|
38 | - } |
|
37 | + pcntl_signal($signal, [$this, 'handle']); |
|
38 | + } |
|
39 | 39 | |
40 | - public static function isSupported(): bool |
|
41 | - { |
|
42 | - if (!\function_exists('pcntl_signal')) { |
|
43 | - return false; |
|
44 | - } |
|
40 | + public static function isSupported(): bool |
|
41 | + { |
|
42 | + if (!\function_exists('pcntl_signal')) { |
|
43 | + return false; |
|
44 | + } |
|
45 | 45 | |
46 | - if (\in_array('pcntl_signal', explode(',', ini_get('disable_functions')))) { |
|
47 | - return false; |
|
48 | - } |
|
46 | + if (\in_array('pcntl_signal', explode(',', ini_get('disable_functions')))) { |
|
47 | + return false; |
|
48 | + } |
|
49 | 49 | |
50 | - return true; |
|
51 | - } |
|
50 | + return true; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @internal |
|
55 | - */ |
|
56 | - public function handle(int $signal): void |
|
57 | - { |
|
58 | - $count = \count($this->signalHandlers[$signal]); |
|
53 | + /** |
|
54 | + * @internal |
|
55 | + */ |
|
56 | + public function handle(int $signal): void |
|
57 | + { |
|
58 | + $count = \count($this->signalHandlers[$signal]); |
|
59 | 59 | |
60 | - foreach ($this->signalHandlers[$signal] as $i => $signalHandler) { |
|
61 | - $hasNext = $i !== $count - 1; |
|
62 | - $signalHandler($signal, $hasNext); |
|
63 | - } |
|
64 | - } |
|
60 | + foreach ($this->signalHandlers[$signal] as $i => $signalHandler) { |
|
61 | + $hasNext = $i !== $count - 1; |
|
62 | + $signalHandler($signal, $hasNext); |
|
63 | + } |
|
64 | + } |
|
65 | 65 | } |
@@ -20,43 +20,43 @@ |
||
20 | 20 | */ |
21 | 21 | class FactoryCommandLoader implements CommandLoaderInterface |
22 | 22 | { |
23 | - private $factories; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param callable[] $factories Indexed by command names |
|
27 | - */ |
|
28 | - public function __construct(array $factories) |
|
29 | - { |
|
30 | - $this->factories = $factories; |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * {@inheritdoc} |
|
35 | - */ |
|
36 | - public function has(string $name) |
|
37 | - { |
|
38 | - return isset($this->factories[$name]); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - */ |
|
44 | - public function get(string $name) |
|
45 | - { |
|
46 | - if (!isset($this->factories[$name])) { |
|
47 | - throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); |
|
48 | - } |
|
49 | - |
|
50 | - $factory = $this->factories[$name]; |
|
51 | - |
|
52 | - return $factory(); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - public function getNames() |
|
59 | - { |
|
60 | - return array_keys($this->factories); |
|
61 | - } |
|
23 | + private $factories; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param callable[] $factories Indexed by command names |
|
27 | + */ |
|
28 | + public function __construct(array $factories) |
|
29 | + { |
|
30 | + $this->factories = $factories; |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * {@inheritdoc} |
|
35 | + */ |
|
36 | + public function has(string $name) |
|
37 | + { |
|
38 | + return isset($this->factories[$name]); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + */ |
|
44 | + public function get(string $name) |
|
45 | + { |
|
46 | + if (!isset($this->factories[$name])) { |
|
47 | + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); |
|
48 | + } |
|
49 | + |
|
50 | + $factory = $this->factories[$name]; |
|
51 | + |
|
52 | + return $factory(); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + public function getNames() |
|
59 | + { |
|
60 | + return array_keys($this->factories); |
|
61 | + } |
|
62 | 62 | } |
@@ -21,43 +21,43 @@ |
||
21 | 21 | */ |
22 | 22 | class ContainerCommandLoader implements CommandLoaderInterface |
23 | 23 | { |
24 | - private $container; |
|
25 | - private $commandMap; |
|
26 | - |
|
27 | - /** |
|
28 | - * @param array $commandMap An array with command names as keys and service ids as values |
|
29 | - */ |
|
30 | - public function __construct(ContainerInterface $container, array $commandMap) |
|
31 | - { |
|
32 | - $this->container = $container; |
|
33 | - $this->commandMap = $commandMap; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * {@inheritdoc} |
|
38 | - */ |
|
39 | - public function get(string $name) |
|
40 | - { |
|
41 | - if (!$this->has($name)) { |
|
42 | - throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); |
|
43 | - } |
|
44 | - |
|
45 | - return $this->container->get($this->commandMap[$name]); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - public function has(string $name) |
|
52 | - { |
|
53 | - return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritdoc} |
|
58 | - */ |
|
59 | - public function getNames() |
|
60 | - { |
|
61 | - return array_keys($this->commandMap); |
|
62 | - } |
|
24 | + private $container; |
|
25 | + private $commandMap; |
|
26 | + |
|
27 | + /** |
|
28 | + * @param array $commandMap An array with command names as keys and service ids as values |
|
29 | + */ |
|
30 | + public function __construct(ContainerInterface $container, array $commandMap) |
|
31 | + { |
|
32 | + $this->container = $container; |
|
33 | + $this->commandMap = $commandMap; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * {@inheritdoc} |
|
38 | + */ |
|
39 | + public function get(string $name) |
|
40 | + { |
|
41 | + if (!$this->has($name)) { |
|
42 | + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); |
|
43 | + } |
|
44 | + |
|
45 | + return $this->container->get($this->commandMap[$name]); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + public function has(string $name) |
|
52 | + { |
|
53 | + return isset($this->commandMap[$name]) && $this->container->has($this->commandMap[$name]); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritdoc} |
|
58 | + */ |
|
59 | + public function getNames() |
|
60 | + { |
|
61 | + return array_keys($this->commandMap); |
|
62 | + } |
|
63 | 63 | } |
@@ -18,163 +18,163 @@ |
||
18 | 18 | */ |
19 | 19 | final class Color |
20 | 20 | { |
21 | - private const COLORS = [ |
|
22 | - 'black' => 0, |
|
23 | - 'red' => 1, |
|
24 | - 'green' => 2, |
|
25 | - 'yellow' => 3, |
|
26 | - 'blue' => 4, |
|
27 | - 'magenta' => 5, |
|
28 | - 'cyan' => 6, |
|
29 | - 'white' => 7, |
|
30 | - 'default' => 9, |
|
31 | - ]; |
|
32 | - |
|
33 | - private const BRIGHT_COLORS = [ |
|
34 | - 'gray' => 0, |
|
35 | - 'bright-red' => 1, |
|
36 | - 'bright-green' => 2, |
|
37 | - 'bright-yellow' => 3, |
|
38 | - 'bright-blue' => 4, |
|
39 | - 'bright-magenta' => 5, |
|
40 | - 'bright-cyan' => 6, |
|
41 | - 'bright-white' => 7, |
|
42 | - ]; |
|
43 | - |
|
44 | - private const AVAILABLE_OPTIONS = [ |
|
45 | - 'bold' => ['set' => 1, 'unset' => 22], |
|
46 | - 'underscore' => ['set' => 4, 'unset' => 24], |
|
47 | - 'blink' => ['set' => 5, 'unset' => 25], |
|
48 | - 'reverse' => ['set' => 7, 'unset' => 27], |
|
49 | - 'conceal' => ['set' => 8, 'unset' => 28], |
|
50 | - ]; |
|
51 | - |
|
52 | - private $foreground; |
|
53 | - private $background; |
|
54 | - private $options = []; |
|
55 | - |
|
56 | - public function __construct(string $foreground = '', string $background = '', array $options = []) |
|
57 | - { |
|
58 | - $this->foreground = $this->parseColor($foreground); |
|
59 | - $this->background = $this->parseColor($background, true); |
|
60 | - |
|
61 | - foreach ($options as $option) { |
|
62 | - if (!isset(self::AVAILABLE_OPTIONS[$option])) { |
|
63 | - throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(self::AVAILABLE_OPTIONS)))); |
|
64 | - } |
|
65 | - |
|
66 | - $this->options[$option] = self::AVAILABLE_OPTIONS[$option]; |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - public function apply(string $text): string |
|
71 | - { |
|
72 | - return $this->set().$text.$this->unset(); |
|
73 | - } |
|
74 | - |
|
75 | - public function set(): string |
|
76 | - { |
|
77 | - $setCodes = []; |
|
78 | - if ('' !== $this->foreground) { |
|
79 | - $setCodes[] = $this->foreground; |
|
80 | - } |
|
81 | - if ('' !== $this->background) { |
|
82 | - $setCodes[] = $this->background; |
|
83 | - } |
|
84 | - foreach ($this->options as $option) { |
|
85 | - $setCodes[] = $option['set']; |
|
86 | - } |
|
87 | - if (0 === \count($setCodes)) { |
|
88 | - return ''; |
|
89 | - } |
|
90 | - |
|
91 | - return sprintf("\033[%sm", implode(';', $setCodes)); |
|
92 | - } |
|
93 | - |
|
94 | - public function unset(): string |
|
95 | - { |
|
96 | - $unsetCodes = []; |
|
97 | - if ('' !== $this->foreground) { |
|
98 | - $unsetCodes[] = 39; |
|
99 | - } |
|
100 | - if ('' !== $this->background) { |
|
101 | - $unsetCodes[] = 49; |
|
102 | - } |
|
103 | - foreach ($this->options as $option) { |
|
104 | - $unsetCodes[] = $option['unset']; |
|
105 | - } |
|
106 | - if (0 === \count($unsetCodes)) { |
|
107 | - return ''; |
|
108 | - } |
|
109 | - |
|
110 | - return sprintf("\033[%sm", implode(';', $unsetCodes)); |
|
111 | - } |
|
112 | - |
|
113 | - private function parseColor(string $color, bool $background = false): string |
|
114 | - { |
|
115 | - if ('' === $color) { |
|
116 | - return ''; |
|
117 | - } |
|
118 | - |
|
119 | - if ('#' === $color[0]) { |
|
120 | - $color = substr($color, 1); |
|
121 | - |
|
122 | - if (3 === \strlen($color)) { |
|
123 | - $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2]; |
|
124 | - } |
|
125 | - |
|
126 | - if (6 !== \strlen($color)) { |
|
127 | - throw new InvalidArgumentException(sprintf('Invalid "%s" color.', $color)); |
|
128 | - } |
|
129 | - |
|
130 | - return ($background ? '4' : '3').$this->convertHexColorToAnsi(hexdec($color)); |
|
131 | - } |
|
132 | - |
|
133 | - if (isset(self::COLORS[$color])) { |
|
134 | - return ($background ? '4' : '3').self::COLORS[$color]; |
|
135 | - } |
|
136 | - |
|
137 | - if (isset(self::BRIGHT_COLORS[$color])) { |
|
138 | - return ($background ? '10' : '9').self::BRIGHT_COLORS[$color]; |
|
139 | - } |
|
140 | - |
|
141 | - throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, implode(', ', array_merge(array_keys(self::COLORS), array_keys(self::BRIGHT_COLORS))))); |
|
142 | - } |
|
143 | - |
|
144 | - private function convertHexColorToAnsi(int $color): string |
|
145 | - { |
|
146 | - $r = ($color >> 16) & 255; |
|
147 | - $g = ($color >> 8) & 255; |
|
148 | - $b = $color & 255; |
|
149 | - |
|
150 | - // see https://github.com/termstandard/colors/ for more information about true color support |
|
151 | - if ('truecolor' !== getenv('COLORTERM')) { |
|
152 | - return (string) $this->degradeHexColorToAnsi($r, $g, $b); |
|
153 | - } |
|
154 | - |
|
155 | - return sprintf('8;2;%d;%d;%d', $r, $g, $b); |
|
156 | - } |
|
157 | - |
|
158 | - private function degradeHexColorToAnsi(int $r, int $g, int $b): int |
|
159 | - { |
|
160 | - if (0 === round($this->getSaturation($r, $g, $b) / 50)) { |
|
161 | - return 0; |
|
162 | - } |
|
163 | - |
|
164 | - return (round($b / 255) << 2) | (round($g / 255) << 1) | round($r / 255); |
|
165 | - } |
|
166 | - |
|
167 | - private function getSaturation(int $r, int $g, int $b): int |
|
168 | - { |
|
169 | - $r = $r / 255; |
|
170 | - $g = $g / 255; |
|
171 | - $b = $b / 255; |
|
172 | - $v = max($r, $g, $b); |
|
173 | - |
|
174 | - if (0 === $diff = $v - min($r, $g, $b)) { |
|
175 | - return 0; |
|
176 | - } |
|
177 | - |
|
178 | - return (int) $diff * 100 / $v; |
|
179 | - } |
|
21 | + private const COLORS = [ |
|
22 | + 'black' => 0, |
|
23 | + 'red' => 1, |
|
24 | + 'green' => 2, |
|
25 | + 'yellow' => 3, |
|
26 | + 'blue' => 4, |
|
27 | + 'magenta' => 5, |
|
28 | + 'cyan' => 6, |
|
29 | + 'white' => 7, |
|
30 | + 'default' => 9, |
|
31 | + ]; |
|
32 | + |
|
33 | + private const BRIGHT_COLORS = [ |
|
34 | + 'gray' => 0, |
|
35 | + 'bright-red' => 1, |
|
36 | + 'bright-green' => 2, |
|
37 | + 'bright-yellow' => 3, |
|
38 | + 'bright-blue' => 4, |
|
39 | + 'bright-magenta' => 5, |
|
40 | + 'bright-cyan' => 6, |
|
41 | + 'bright-white' => 7, |
|
42 | + ]; |
|
43 | + |
|
44 | + private const AVAILABLE_OPTIONS = [ |
|
45 | + 'bold' => ['set' => 1, 'unset' => 22], |
|
46 | + 'underscore' => ['set' => 4, 'unset' => 24], |
|
47 | + 'blink' => ['set' => 5, 'unset' => 25], |
|
48 | + 'reverse' => ['set' => 7, 'unset' => 27], |
|
49 | + 'conceal' => ['set' => 8, 'unset' => 28], |
|
50 | + ]; |
|
51 | + |
|
52 | + private $foreground; |
|
53 | + private $background; |
|
54 | + private $options = []; |
|
55 | + |
|
56 | + public function __construct(string $foreground = '', string $background = '', array $options = []) |
|
57 | + { |
|
58 | + $this->foreground = $this->parseColor($foreground); |
|
59 | + $this->background = $this->parseColor($background, true); |
|
60 | + |
|
61 | + foreach ($options as $option) { |
|
62 | + if (!isset(self::AVAILABLE_OPTIONS[$option])) { |
|
63 | + throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(self::AVAILABLE_OPTIONS)))); |
|
64 | + } |
|
65 | + |
|
66 | + $this->options[$option] = self::AVAILABLE_OPTIONS[$option]; |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + public function apply(string $text): string |
|
71 | + { |
|
72 | + return $this->set().$text.$this->unset(); |
|
73 | + } |
|
74 | + |
|
75 | + public function set(): string |
|
76 | + { |
|
77 | + $setCodes = []; |
|
78 | + if ('' !== $this->foreground) { |
|
79 | + $setCodes[] = $this->foreground; |
|
80 | + } |
|
81 | + if ('' !== $this->background) { |
|
82 | + $setCodes[] = $this->background; |
|
83 | + } |
|
84 | + foreach ($this->options as $option) { |
|
85 | + $setCodes[] = $option['set']; |
|
86 | + } |
|
87 | + if (0 === \count($setCodes)) { |
|
88 | + return ''; |
|
89 | + } |
|
90 | + |
|
91 | + return sprintf("\033[%sm", implode(';', $setCodes)); |
|
92 | + } |
|
93 | + |
|
94 | + public function unset(): string |
|
95 | + { |
|
96 | + $unsetCodes = []; |
|
97 | + if ('' !== $this->foreground) { |
|
98 | + $unsetCodes[] = 39; |
|
99 | + } |
|
100 | + if ('' !== $this->background) { |
|
101 | + $unsetCodes[] = 49; |
|
102 | + } |
|
103 | + foreach ($this->options as $option) { |
|
104 | + $unsetCodes[] = $option['unset']; |
|
105 | + } |
|
106 | + if (0 === \count($unsetCodes)) { |
|
107 | + return ''; |
|
108 | + } |
|
109 | + |
|
110 | + return sprintf("\033[%sm", implode(';', $unsetCodes)); |
|
111 | + } |
|
112 | + |
|
113 | + private function parseColor(string $color, bool $background = false): string |
|
114 | + { |
|
115 | + if ('' === $color) { |
|
116 | + return ''; |
|
117 | + } |
|
118 | + |
|
119 | + if ('#' === $color[0]) { |
|
120 | + $color = substr($color, 1); |
|
121 | + |
|
122 | + if (3 === \strlen($color)) { |
|
123 | + $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2]; |
|
124 | + } |
|
125 | + |
|
126 | + if (6 !== \strlen($color)) { |
|
127 | + throw new InvalidArgumentException(sprintf('Invalid "%s" color.', $color)); |
|
128 | + } |
|
129 | + |
|
130 | + return ($background ? '4' : '3').$this->convertHexColorToAnsi(hexdec($color)); |
|
131 | + } |
|
132 | + |
|
133 | + if (isset(self::COLORS[$color])) { |
|
134 | + return ($background ? '4' : '3').self::COLORS[$color]; |
|
135 | + } |
|
136 | + |
|
137 | + if (isset(self::BRIGHT_COLORS[$color])) { |
|
138 | + return ($background ? '10' : '9').self::BRIGHT_COLORS[$color]; |
|
139 | + } |
|
140 | + |
|
141 | + throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, implode(', ', array_merge(array_keys(self::COLORS), array_keys(self::BRIGHT_COLORS))))); |
|
142 | + } |
|
143 | + |
|
144 | + private function convertHexColorToAnsi(int $color): string |
|
145 | + { |
|
146 | + $r = ($color >> 16) & 255; |
|
147 | + $g = ($color >> 8) & 255; |
|
148 | + $b = $color & 255; |
|
149 | + |
|
150 | + // see https://github.com/termstandard/colors/ for more information about true color support |
|
151 | + if ('truecolor' !== getenv('COLORTERM')) { |
|
152 | + return (string) $this->degradeHexColorToAnsi($r, $g, $b); |
|
153 | + } |
|
154 | + |
|
155 | + return sprintf('8;2;%d;%d;%d', $r, $g, $b); |
|
156 | + } |
|
157 | + |
|
158 | + private function degradeHexColorToAnsi(int $r, int $g, int $b): int |
|
159 | + { |
|
160 | + if (0 === round($this->getSaturation($r, $g, $b) / 50)) { |
|
161 | + return 0; |
|
162 | + } |
|
163 | + |
|
164 | + return (round($b / 255) << 2) | (round($g / 255) << 1) | round($r / 255); |
|
165 | + } |
|
166 | + |
|
167 | + private function getSaturation(int $r, int $g, int $b): int |
|
168 | + { |
|
169 | + $r = $r / 255; |
|
170 | + $g = $g / 255; |
|
171 | + $b = $b / 255; |
|
172 | + $v = max($r, $g, $b); |
|
173 | + |
|
174 | + if (0 === $diff = $v - min($r, $g, $b)) { |
|
175 | + return 0; |
|
176 | + } |
|
177 | + |
|
178 | + return (int) $diff * 100 / $v; |
|
179 | + } |
|
180 | 180 | } |
@@ -18,34 +18,34 @@ |
||
18 | 18 | */ |
19 | 19 | final class ConsoleCommandEvent extends ConsoleEvent |
20 | 20 | { |
21 | - /** |
|
22 | - * The return code for skipped commands, this will also be passed into the terminate event. |
|
23 | - */ |
|
24 | - public const RETURN_CODE_DISABLED = 113; |
|
21 | + /** |
|
22 | + * The return code for skipped commands, this will also be passed into the terminate event. |
|
23 | + */ |
|
24 | + public const RETURN_CODE_DISABLED = 113; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Indicates if the command should be run or skipped. |
|
28 | - */ |
|
29 | - private $commandShouldRun = true; |
|
26 | + /** |
|
27 | + * Indicates if the command should be run or skipped. |
|
28 | + */ |
|
29 | + private $commandShouldRun = true; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Disables the command, so it won't be run. |
|
33 | - */ |
|
34 | - public function disableCommand(): bool |
|
35 | - { |
|
36 | - return $this->commandShouldRun = false; |
|
37 | - } |
|
31 | + /** |
|
32 | + * Disables the command, so it won't be run. |
|
33 | + */ |
|
34 | + public function disableCommand(): bool |
|
35 | + { |
|
36 | + return $this->commandShouldRun = false; |
|
37 | + } |
|
38 | 38 | |
39 | - public function enableCommand(): bool |
|
40 | - { |
|
41 | - return $this->commandShouldRun = true; |
|
42 | - } |
|
39 | + public function enableCommand(): bool |
|
40 | + { |
|
41 | + return $this->commandShouldRun = true; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Returns true if the command is runnable, false otherwise. |
|
46 | - */ |
|
47 | - public function commandShouldRun(): bool |
|
48 | - { |
|
49 | - return $this->commandShouldRun; |
|
50 | - } |
|
44 | + /** |
|
45 | + * Returns true if the command is runnable, false otherwise. |
|
46 | + */ |
|
47 | + public function commandShouldRun(): bool |
|
48 | + { |
|
49 | + return $this->commandShouldRun; |
|
50 | + } |
|
51 | 51 | } |
@@ -22,37 +22,37 @@ |
||
22 | 22 | */ |
23 | 23 | final class ConsoleErrorEvent extends ConsoleEvent |
24 | 24 | { |
25 | - private $error; |
|
26 | - private $exitCode; |
|
27 | - |
|
28 | - public function __construct(InputInterface $input, OutputInterface $output, \Throwable $error, Command $command = null) |
|
29 | - { |
|
30 | - parent::__construct($command, $input, $output); |
|
31 | - |
|
32 | - $this->error = $error; |
|
33 | - } |
|
34 | - |
|
35 | - public function getError(): \Throwable |
|
36 | - { |
|
37 | - return $this->error; |
|
38 | - } |
|
39 | - |
|
40 | - public function setError(\Throwable $error): void |
|
41 | - { |
|
42 | - $this->error = $error; |
|
43 | - } |
|
44 | - |
|
45 | - public function setExitCode(int $exitCode): void |
|
46 | - { |
|
47 | - $this->exitCode = $exitCode; |
|
48 | - |
|
49 | - $r = new \ReflectionProperty($this->error, 'code'); |
|
50 | - $r->setAccessible(true); |
|
51 | - $r->setValue($this->error, $this->exitCode); |
|
52 | - } |
|
53 | - |
|
54 | - public function getExitCode(): int |
|
55 | - { |
|
56 | - return $this->exitCode ?? (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1); |
|
57 | - } |
|
25 | + private $error; |
|
26 | + private $exitCode; |
|
27 | + |
|
28 | + public function __construct(InputInterface $input, OutputInterface $output, \Throwable $error, Command $command = null) |
|
29 | + { |
|
30 | + parent::__construct($command, $input, $output); |
|
31 | + |
|
32 | + $this->error = $error; |
|
33 | + } |
|
34 | + |
|
35 | + public function getError(): \Throwable |
|
36 | + { |
|
37 | + return $this->error; |
|
38 | + } |
|
39 | + |
|
40 | + public function setError(\Throwable $error): void |
|
41 | + { |
|
42 | + $this->error = $error; |
|
43 | + } |
|
44 | + |
|
45 | + public function setExitCode(int $exitCode): void |
|
46 | + { |
|
47 | + $this->exitCode = $exitCode; |
|
48 | + |
|
49 | + $r = new \ReflectionProperty($this->error, 'code'); |
|
50 | + $r->setAccessible(true); |
|
51 | + $r->setValue($this->error, $this->exitCode); |
|
52 | + } |
|
53 | + |
|
54 | + public function getExitCode(): int |
|
55 | + { |
|
56 | + return $this->exitCode ?? (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1); |
|
57 | + } |
|
58 | 58 | } |