-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Expose the original input arguments and options and to unparse options #57598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Changes from all commits
a944a32
378f1c6
0a834b9
d36058e
9338ba8
b01d0ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
|
||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Exception\RuntimeException; | ||
use function array_map; | ||
use function sprintf; | ||
use const true; | ||
|
||
/** | ||
* Input is the base class for all concrete Input classes. | ||
|
@@ -85,6 +88,16 @@ | |
return array_merge($this->definition->getArgumentDefaults(), $this->arguments); | ||
} | ||
|
||
/** | ||
* Returns all the given arguments NOT merged with the default values. | ||
* | ||
* @return array<string|bool|int|float|array<string|bool|int|float|null>|null> | ||
*/ | ||
public function getRawArguments(): array | ||
{ | ||
return $this->arguments; | ||
8000 | } | |
|
||
public function getArgument(string $name): mixed | ||
{ | ||
if (!$this->definition->hasArgument($name)) { | ||
|
@@ -113,6 +126,16 @@ | |
return array_merge($this->definition->getOptionDefaults(), $this->options); | ||
} | ||
|
||
/** | ||
* Returns all the given options NOT merged with the default values. | ||
* | ||
* @return array<string|bool|int|float|array<string|bool|int|float|null>|null> | ||
*/ | ||
public function getRawOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function getOption(string $name): mixed | ||
{ | ||
if ($this->definition->hasNegation($name)) { | ||
|
@@ -171,4 +194,39 @@ | |
{ | ||
return $this->stream; | ||
} | ||
|
||
/** | ||
* Returns a stringified representation of the options passed to the command. | ||
* The options must NOT be escaped as otherwise passing them to a `Process` would result in them being escaped twice. | ||
* | ||
* @param string[] $optionNames Names of the options returned. If null, all options are returned. Requested options | ||
* that either do not exist or were not passed (even if the option has a default value) | ||
* will not be part of the method output. | ||
* | ||
* @return list<string> | ||
*/ | ||
public function unparse(?array $optionNames = null): array | ||
Comment on lines
+198
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should be extracted to a dedicated class (SRP) so that it's easier to maintain: we can change it more easily. I don't know for the class name and namespace (we need to think about it). The method signature would be: function format(RawInputInterface $input, ?array $optionNames = null): array { /* ... */ } Note: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree on principle, and more generally I am not sure it is best to move this now though, because:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to how you would use this method in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In That's why I thought it would be better in Symfony to have a more re-usable piece of code (which is also simpler) even thought that means a tiny bit more work on my side. |
||
{ | ||
$rawOptions = $this->getRawOptions(); | ||
|
||
$filteredRawOptions = null === $optionNames | ||
? $rawOptions | ||
: array_intersect_key($rawOptions, array_fill_keys($optionNames, '')); | ||
|
||
$unparsedOptions = []; | ||
|
||
foreach ($filteredRawOptions as $optionName => $parsedOption) { | ||
$option = $this->definition->getOption($optionName); | ||
|
||
$unparsedOptions[] = match (true) { | ||
$option->isNegatable() => [sprintf('--%s%s', $parsedOption ? '' : 'no-', $optionName)], | ||
!$option->acceptValue() => [sprintf('--%s', $optionName,)], | ||
$option->isArray() => array_map(static fn($item,) => sprintf('--%s=%s', $optionName, $item), $parsedOption), | ||
default => [sprintf('--%s=%s', $optionName, $parsedOption)], | ||
Check failure on line 225 in src/Symfony/Component/Console/Input/Input.php
|
||
}; | ||
} | ||
|
||
return array_merge(...$unparsedOptions); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,10 @@ | |
* InputInterface is the interface implemented by all input classes. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* | ||
* @method getRawArguments(bool $strip = false): array<string|bool|int|float|null|array<string|bool|int|float|null>> Returns all the given arguments NOT merged with the default values. | ||
* @method getRawOptions(): array<string|bool|int|float|array<string|bool|int|float|null>|null> Returns all the given options NOT merged with the default values. | ||
* @method unparse(): list<string> Returns a stringified representation of the options passed to the command. | ||
*/ | ||
interface InputInterface | ||
Comment on lines
+22
to
26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid any BC break, we can add a new interface for these new capabilities: interface RawInputInterface
{
public function getRawArguments(): array:
public function getRawOptions(): array;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would that really help? In the end what you receive, because that is what is typed everywhere, is public static function getRawOptions(InputInterface $input): array
{
return $input instanceof RawInputInterface
? $input->getRawOptions()
: [];
} Which... err yes in practice if we make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to clarify: the only affected users are users implementing |
||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.