Base.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Command\Config\System;
  7. use OC\SystemConfig;
  8. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  9. abstract class Base extends \OC\Core\Command\Base {
  10. public function __construct(
  11. protected SystemConfig $systemConfig,
  12. ) {
  13. parent::__construct();
  14. }
  15. /**
  16. * @param string $argumentName
  17. * @param CompletionContext $context
  18. * @return string[]
  19. */
  20. public function completeArgumentValues($argumentName, CompletionContext $context) {
  21. if ($argumentName === 'name') {
  22. $words = $this->getPreviousNames($context, $context->getWordIndex());
  23. if (empty($words)) {
  24. $completions = $this->systemConfig->getKeys();
  25. } else {
  26. $key = array_shift($words);
  27. $value = $this->systemConfig->getValue($key);
  28. $completions = array_keys($value);
  29. while (!empty($words) && is_array($value)) {
  30. $key = array_shift($words);
  31. if (!isset($value[$key]) || !is_array($value[$key])) {
  32. break;
  33. }
  34. $value = $value[$key];
  35. $completions = array_keys($value);
  36. }
  37. }
  38. return $completions;
  39. }
  40. return parent::completeArgumentValues($argumentName, $context);
  41. }
  42. /**
  43. * @param CompletionContext $context
  44. * @param int $currentIndex
  45. * @return string[]
  46. */
  47. protected function getPreviousNames(CompletionContext $context, $currentIndex) {
  48. $word = $context->getWordAtIndex($currentIndex - 1);
  49. if ($word === $this->getName() || $currentIndex <= 0) {
  50. return [];
  51. }
  52. $words = $this->getPreviousNames($context, $currentIndex - 1);
  53. $words[] = $word;
  54. return $words;
  55. }
  56. }