public function testKeysScalar() { return $this->assertEquals([], $this->configManager->keys('b')); }
protected function execute(InputInterface $input, OutputInterface $output) { $metaConfigFile = $input->getOption('config-file'); if ($metaConfigFile === null) { foreach (['./.lbconf', './.lbconf.php'] as $possibleFile) { if (file_exists($possibleFile)) { $metaConfigFile = $possibleFile; break; } } if ($metaConfigFile === null) { throw new \InvalidArgumentException("Cannot file meta-config file. Looked for './.lbconf', './.lbconf.php'."); } } $configManager = new ConfigManager(); $configManager->loadConfig($metaConfigFile); $key = $input->getArgument('key'); $value = $input->getArgument('value'); $actions = []; if ($input->getOption('get')) { $actions[] = self::ACTION_GET; } if ($input->getOption('set')) { $actions[] = self::ACTION_SET; } if ($input->getOption('del')) { $actions[] = self::ACTION_DELETE; } if ($input->getOption('keys')) { $actions[] = self::ACTION_KEYS; } if ($input->getOption('complete')) { $actions[] = self::ACTION_COMPLETE; } if (count($actions) > 1) { throw new \InvalidArgumentException('Cannot specify more than one action at a time.'); } if (count($actions) === 0) { if ($value !== null) { $action = self::ACTION_SET; } else { $action = self::ACTION_GET; } } else { $action = $actions[0]; } switch ($action) { case self::ACTION_GET: $value = $configManager->get($key); $output->writeln(json_encode($value, JSON_PRETTY_PRINT)); break; case self::ACTION_SET: $value = $input->getArgument('value'); $type = $input->getOption('type'); $configManager->set($key, $value, $type); $configManager->storeConfig(); break; case self::ACTION_DELETE: $configManager->delete($key); $configManager->storeConfig(); break; case self::ACTION_KEYS: $keys = $configManager->keys($key); sort($keys); // For keys, it's probably nicer to sort them $output->writeln(json_encode($keys, JSON_PRETTY_PRINT)); break; case self::ACTION_COMPLETE: // Find the keys for everything up to the last period $pieces = explode('.', $key); $suffix = array_pop($pieces); $prefix = implode('.', $pieces); $keys = $configManager->keys($prefix ?: null); sort($keys); // For keys, it's probably nicer to sort them $keys = array_map(function ($key) use($prefix) { return $prefix ? "{$prefix}.{$key}" : $key; }, $keys); echo implode(" ", $keys); die; default: throw new \InvalidArgumentException("Invalid action: {$action}"); } }