Base.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. protected SystemConfig $systemConfig;
  28. public function __construct(SystemConfig $systemConfig) {
  29. parent::__construct();
  30. $this->systemConfig = $systemConfig;
  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. }