ConfigCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Composer\Config;
  17. use Composer\Factory;
  18. use Composer\Json\JsonFile;
  19. class ConfigCommand extends Command
  20. {
  21. /**
  22. * @var Composer\Json\JsonFile
  23. */
  24. protected $configFile;
  25. /**
  26. * {@inheritDoc}
  27. */
  28. protected function configure()
  29. {
  30. $this
  31. ->setName('config')
  32. ->setDescription('Set config options')
  33. ->setDefinition(array(
  34. new InputOption('global', 'g', InputOption::VALUE_NONE, 'Set this as a global config settings.'),
  35. new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'),
  36. new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'),
  37. new InputArgument('setting-key', null, 'Setting key'),
  38. new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'),
  39. ))
  40. // @todo Document
  41. ->setHelp(<<<EOT
  42. EOT
  43. )
  44. ;
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. protected function initialize(InputInterface $input, OutputInterface $output)
  50. {
  51. // Get the local composer.json or the global config.json
  52. $this->configFile = $input->getOption('global')
  53. ? (Factory::createConfig()->get('home') . '/config.json')
  54. : 'composer.json';
  55. $this->configFile = new JsonFile($this->configFile);
  56. if (!$this->configFile->exists()) {
  57. touch($this->configFile->getPath());
  58. // If you read an empty file, Composer throws an error
  59. // Toss some of the defaults in there
  60. $defaults = Config::$defaultConfig;
  61. $defaults['repositories'] = Config::$defaultRepositories;
  62. $this->configFile->write($defaults);
  63. }
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. protected function execute(InputInterface $input, OutputInterface $output)
  69. {
  70. // Open file in editor
  71. if ($input->getOption('editor')) {
  72. // @todo Find a way to use another editor
  73. $editor = system("bash -cl 'echo \$EDITOR'");
  74. system($editor . ' ' . $this->configFile->getPath() . ' > `tty`');
  75. return 0;
  76. }
  77. // List the configuration of the file settings
  78. if ($input->getOption('list')) {
  79. $this->displayFileContents($this->configFile->read(), $output);
  80. return 0;
  81. }
  82. // If the user enters in a config variable, parse it and save to file
  83. if ($input->getArgument('setting-key')) {
  84. if (null === $input->getArgument('setting-value')) {
  85. throw new \RuntimeException('You must include a setting value.');
  86. }
  87. $setting = $this->parseSetting($input->getArgument('setting-key'), $input->getArgument('setting-value'));
  88. $configSettings = $this->configFile->read();
  89. $settings = array_merge($configSettings, $setting);
  90. // Make confirmation
  91. if ($input->isInteractive()) {
  92. $dialog = $this->getHelperSet()->get('dialog');
  93. $output->writeln(JsonFile::encode($settings));
  94. if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm?', 'yes', '?'), true)) {
  95. $output->writeln('<error>Command Aborted by User</error>');
  96. return 1;
  97. }
  98. }
  99. $this->configFile->write($settings);
  100. }
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. protected function interact(InputInterface $input, OutputInterface $output)
  106. {
  107. }
  108. /**
  109. * Display the contents of the file in a pretty formatted way
  110. *
  111. * @param array $contents
  112. * @param OutputInterface $output
  113. * @param integer $depth
  114. * @param string|null $k
  115. */
  116. protected function displayFileContents(array $contents, OutputInterface $output, &$depth = 0, $k = null)
  117. {
  118. // @todo Look into a way to refactor this code, as it is right now, I
  119. // don't like it, also the name of the function could be better
  120. foreach ($contents as $key => $value) {
  121. if (is_array($value)) {
  122. $depth++;
  123. $k .= $key . '.';
  124. $this->displayFileContents($value, $output, $depth, $k);
  125. if (substr_count($k,'.') > 1) {
  126. $k = str_split($k,strrpos($k,'.',-2));
  127. $k = $k[0] . '.';
  128. } else { $k = null; }
  129. $depth--;
  130. continue;
  131. }
  132. $output->writeln('[<comment>' . $k . $key . '</comment>] <info>' . $value . '</info>');
  133. }
  134. }
  135. /**
  136. * This function will take a setting key (a.b.c) and return an
  137. * array that matches this
  138. *
  139. * @param string $key
  140. * @param string $value
  141. * @return array
  142. */
  143. protected function parseSetting($key, $value)
  144. {
  145. $parts = array_reverse(explode('.', $key));
  146. $tmp = array();
  147. for($i=0;$i<count($parts);$i++) {
  148. $tmp[$parts[$i]] = (0 === $i) ? $value : $tmp;
  149. if (0 < $i) {
  150. unset($tmp[$parts[$i - 1]]);
  151. }
  152. }
  153. return $tmp;
  154. }
  155. }