Skip to content

Commit

Permalink
Merge pull request #457 from nextcloud/backport/388/stable19
Browse files Browse the repository at this point in the history
[stable19] Bump stecman/symfony-console-completion from 0.8.0 to 0.11.0
  • Loading branch information
nickvergessen committed Aug 19, 2020
2 parents 5c2b1ac + 07d750a commit b6bf961
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 101 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"punic/punic": "^1.6",
"sabre/dav": "^4.0.0",
"scssphp/scssphp": "1.0.3",
"stecman/symfony-console-completion": "^0.8.0",
"stecman/symfony-console-completion": "^0.11.0",
"swiftmailer/swiftmailer": "^6.0",
"symfony/console": "4.4.4",
"symfony/event-dispatcher": "4.4.4",
Expand Down
40 changes: 11 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,8 @@
"serializable",
"serialize",
"tokenizer"
]
],
"abandoned": "opis/closure"
},
{
"name": "justinrainbow/json-schema",
Expand Down Expand Up @@ -2393,7 +2394,8 @@
"compression",
"javascript",
"minification"
]
],
"abandoned": true
},
{
"name": "patchwork/utf8",
Expand Down Expand Up @@ -4169,31 +4171,31 @@
},
{
"name": "stecman/symfony-console-completion",
"version": "0.8.0",
"version_normalized": "0.8.0.0",
"version": "0.11.0",
"version_normalized": "0.11.0.0",
"source": {
"type": "git",
"url": "https://github.com/stecman/symfony-console-completion.git",
"reference": "cd738867503477e91dbe84173dfabd431c883431"
"reference": "a9502dab59405e275a9f264536c4e1cb61fc3518"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/stecman/symfony-console-completion/zipball/cd738867503477e91dbe84173dfabd431c883431",
"reference": "cd738867503477e91dbe84173dfabd431c883431",
"url": "https://api.github.com/repos/stecman/symfony-console-completion/zipball/a9502dab59405e275a9f264536c4e1cb61fc3518",
"reference": "a9502dab59405e275a9f264536c4e1cb61fc3518",
"shasum": ""
},
"require": {
"php": ">=5.3.2",
"symfony/console": "~2.3 || ~3.0 || ~4.0"
"symfony/console": "~2.3 || ~3.0 || ~4.0 || ~5.0"
},
"require-dev": {
"phpunit/phpunit": "~4.8.36 || ~5.7 || ~6.4"
},
"time": "2018-02-10T04:28:01+00:00",
"time": "2019-11-24T17:03:06+00:00",
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.6.x-dev"
"dev-master": "0.10.x-dev"
}
},
"installation-source": "dist",
Expand Down
98 changes: 96 additions & 2 deletions stecman/symfony-console-completion/src/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class CompletionCommand extends SymfonyCommand
{

/**
* @var CompletionHandler
*/
Expand Down Expand Up @@ -49,6 +48,52 @@ public function getNativeDefinition()
return $this->createDefinition();
}

/**
* Ignore user-defined global options
*
* Any global options defined by user-code are meaningless to this command.
* Options outside of the core defaults are ignored to avoid name and shortcut conflicts.
*/
public function mergeApplicationDefinition($mergeArgs = true)
{
// Get current application options
$appDefinition = $this->getApplication()->getDefinition();
$originalOptions = $appDefinition->getOptions();

// Temporarily replace application options with a filtered list
$appDefinition->setOptions(
$this->filterApplicationOptions($originalOptions)
);

parent::mergeApplicationDefinition($mergeArgs);

// Restore original application options
$appDefinition->setOptions($originalOptions);
}

/**
* Reduce the passed list of options to the core defaults (if they exist)
*
* @param InputOption[] $appOptions
* @return InputOption[]
*/
protected function filterApplicationOptions(array $appOptions)
{
return array_filter($appOptions, function(InputOption $option) {
static $coreOptions = array(
'help' => true,
'quiet' => true,
'verbose' => true,
'version' => true,
'ansi' => true,
'no-ansi' => true,
'no-interaction' => true,
);

return isset($coreOptions[$option->getName()]);
});
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->handler = new CompletionHandler($this->getApplication());
Expand Down Expand Up @@ -76,7 +121,56 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->write($hook, true);
} else {
$handler->setContext(new EnvironmentCompletionContext());
$output->write($this->runCompletion(), true);

// Get completion results
$results = $this->runCompletion();

// Escape results for the current shell
$shellType = $input->getOption('shell-type') ?: $this->getShellType();

foreach ($results as &$result) {
$result = $this->escapeForShell($result, $shellType);
}

$output->write($results, true);
}

return 0;
}

/**
* Escape each completion result for the specified shell
*
* @param string $result - Completion results that should appear in the shell
* @param string $shellType - Valid shell type from HookFactory
* @return string
*/
protected function escapeForShell($result, $shellType)
{
switch ($shellType) {
// BASH requires special escaping for multi-word and special character results
// This emulates registering completion with`-o filenames`, without side-effects like dir name slashes
case 'bash':
$context = $this->handler->getContext();
$wordStart = substr($context->getRawCurrentWord(), 0, 1);

if ($wordStart == "'") {
// If the current word is single-quoted, escape any single quotes in the result
$result = str_replace("'", "\\'", $result);
} else if ($wordStart == '"') {
// If the current word is double-quoted, escape any double quotes in the result
$result = str_replace('"', '\\"', $result);
} else {
// Otherwise assume the string is unquoted and word breaks should be escaped
$result = preg_replace('/([\s\'"\\\\])/', '\\\\$1', $result);
}

// Escape output to prevent special characters being lost when passing results to compgen
return escapeshellarg($result);

// No transformation by default
default:
return $result;
}
}

Expand Down
Loading

0 comments on commit b6bf961

Please sign in to comment.