Base.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Command\Config\System;
  24. use OC\SystemConfig;
  25. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  26. abstract class Base extends \OC\Core\Command\Base {
  27. public function __construct(
  28. protected SystemConfig $systemConfig,
  29. ) {
  30. parent::__construct();
  31. }
  32. /**
  33. * @param string $argumentName
  34. * @param CompletionContext $context
  35. * @return string[]
  36. */
  37. public function completeArgumentValues($argumentName, CompletionContext $context) {
  38. if ($argumentName === 'name') {
  39. $words = $this->getPreviousNames($context, $context->getWordIndex());
  40. if (empty($words)) {
  41. $completions = $this->systemConfig->getKeys();
  42. } else {
  43. $key = array_shift($words);
  44. $value = $this->systemConfig->getValue($key);
  45. $completions = array_keys($value);
  46. while (!empty($words) && is_array($value)) {
  47. $key = array_shift($words);
  48. if (!isset($value[$key]) || !is_array($value[$key])) {
  49. break;
  50. }
  51. $value = $value[$key];
  52. $completions = array_keys($value);
  53. }
  54. }
  55. return $completions;
  56. }
  57. return parent::completeArgumentValues($argumentName, $context);
  58. }
  59. /**
  60. * @param CompletionContext $context
  61. * @param int $currentIndex
  62. * @return string[]
  63. */
  64. protected function getPreviousNames(CompletionContext $context, $currentIndex) {
  65. $word = $context->getWordAtIndex($currentIndex - 1);
  66. if ($word === $this->getName() || $currentIndex <= 0) {
  67. return [];
  68. }
  69. $words = $this->getPreviousNames($context, $currentIndex - 1);
  70. $words[] = $word;
  71. return $words;
  72. }
  73. }