Base.php 2.3 KB

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