ConfigCommand.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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\Config\JsonConfigSource;
  18. use Composer\Factory;
  19. use Composer\Json\JsonFile;
  20. /**
  21. * @author Joshua Estes <Joshua.Estes@iostudio.com>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class ConfigCommand extends Command
  25. {
  26. /**
  27. * @var Config
  28. */
  29. protected $config;
  30. /**
  31. * @var JsonFile
  32. */
  33. protected $configFile;
  34. /**
  35. * @var JsonConfigSource
  36. */
  37. protected $configSource;
  38. /**
  39. * @var JsonFile
  40. */
  41. protected $authConfigFile;
  42. /**
  43. * @var JsonConfigSource
  44. */
  45. protected $authConfigSource;
  46. /**
  47. * {@inheritDoc}
  48. */
  49. protected function configure()
  50. {
  51. $this
  52. ->setName('config')
  53. ->setDescription('Set config options')
  54. ->setDefinition(array(
  55. new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'),
  56. new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'),
  57. new InputOption('auth', 'a', InputOption::VALUE_NONE, 'Affect auth config file (only used for --editor)'),
  58. new InputOption('unset', null, InputOption::VALUE_NONE, 'Unset the given setting-key'),
  59. new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'),
  60. new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json', 'composer.json'),
  61. new InputOption('absolute', null, InputOption::VALUE_NONE, 'Returns absolute paths when fetching *-dir config values instead of relative'),
  62. new InputArgument('setting-key', null, 'Setting key'),
  63. new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'),
  64. ))
  65. ->setHelp(<<<EOT
  66. This command allows you to edit some basic composer settings in either the
  67. local composer.json file or the global config.json file.
  68. To set a config setting:
  69. <comment>%command.full_name% bin-dir bin/</comment>
  70. To read a config setting:
  71. <comment>%command.full_name% bin-dir</comment>
  72. Outputs: <info>bin</info>
  73. To edit the global config.json file:
  74. <comment>%command.full_name% --global</comment>
  75. To add a repository:
  76. <comment>%command.full_name% repositories.foo vcs http://bar.com</comment>
  77. To remove a repository (repo is a short alias for repositories):
  78. <comment>%command.full_name% --unset repo.foo</comment>
  79. To disable packagist:
  80. <comment>%command.full_name% repo.packagist false</comment>
  81. You can alter repositories in the global config.json file by passing in the
  82. <info>--global</info> option.
  83. To edit the file in an external editor:
  84. <comment>%command.full_name% --editor</comment>
  85. To choose your editor you can set the "EDITOR" env variable.
  86. To get a list of configuration values in the file:
  87. <comment>%command.full_name% --list</comment>
  88. You can always pass more than one option. As an example, if you want to edit the
  89. global config.json file.
  90. <comment>%command.full_name% --editor --global</comment>
  91. EOT
  92. )
  93. ;
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. protected function initialize(InputInterface $input, OutputInterface $output)
  99. {
  100. parent::initialize($input, $output);
  101. if ($input->getOption('global') && 'composer.json' !== $input->getOption('file')) {
  102. throw new \RuntimeException('--file and --global can not be combined');
  103. }
  104. $this->config = Factory::createConfig($this->getIO());
  105. // Get the local composer.json, global config.json, or if the user
  106. // passed in a file to use
  107. $configFile = $input->getOption('global')
  108. ? ($this->config->get('home') . '/config.json')
  109. : $input->getOption('file');
  110. $this->configFile = new JsonFile($configFile);
  111. $this->configSource = new JsonConfigSource($this->configFile);
  112. $authConfigFile = $input->getOption('global')
  113. ? ($this->config->get('home') . '/auth.json')
  114. : dirname(realpath($input->getOption('file'))) . '/auth.json';
  115. $this->authConfigFile = new JsonFile($authConfigFile);
  116. $this->authConfigSource = new JsonConfigSource($this->authConfigFile, true);
  117. // initialize the global file if it's not there
  118. if ($input->getOption('global') && !$this->configFile->exists()) {
  119. touch($this->configFile->getPath());
  120. $this->configFile->write(array('config' => new \ArrayObject));
  121. @chmod($this->configFile->getPath(), 0600);
  122. }
  123. if ($input->getOption('global') && !$this->authConfigFile->exists()) {
  124. touch($this->authConfigFile->getPath());
  125. $this->authConfigFile->write(array('http-basic' => new \ArrayObject, 'github-oauth' => new \ArrayObject));
  126. @chmod($this->authConfigFile->getPath(), 0600);
  127. }
  128. if (!$this->configFile->exists()) {
  129. throw new \RuntimeException(sprintf('File "%s" cannot be found in the current directory', $configFile));
  130. }
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. protected function execute(InputInterface $input, OutputInterface $output)
  136. {
  137. // Open file in editor
  138. if ($input->getOption('editor')) {
  139. $editor = escapeshellcmd(getenv('EDITOR'));
  140. if (!$editor) {
  141. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  142. $editor = 'notepad';
  143. } else {
  144. foreach (array('vim', 'vi', 'nano', 'pico', 'ed') as $candidate) {
  145. if (exec('which '.$candidate)) {
  146. $editor = $candidate;
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. $file = $input->getOption('auth') ? $this->authConfigFile->getPath() : $this->configFile->getPath();
  153. system($editor . ' ' . $file . (defined('PHP_WINDOWS_VERSION_BUILD') ? '' : ' > `tty`'));
  154. return 0;
  155. }
  156. if (!$input->getOption('global')) {
  157. $this->config->merge($this->configFile->read());
  158. $this->config->merge(array('config' => $this->authConfigFile->exists() ? $this->authConfigFile->read() : array()));
  159. }
  160. // List the configuration of the file settings
  161. if ($input->getOption('list')) {
  162. $this->listConfiguration($this->config->all(), $this->config->raw(), $output);
  163. return 0;
  164. }
  165. $settingKey = $input->getArgument('setting-key');
  166. if (!$settingKey) {
  167. return 0;
  168. }
  169. // If the user enters in a config variable, parse it and save to file
  170. if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) {
  171. throw new \RuntimeException('You can not combine a setting value with --unset');
  172. }
  173. // show the value if no value is provided
  174. if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) {
  175. $data = $this->config->all();
  176. if (preg_match('/^repos?(?:itories)?(?:\.(.+))?/', $settingKey, $matches)) {
  177. if (empty($matches[1])) {
  178. $value = isset($data['repositories']) ? $data['repositories'] : array();
  179. } else {
  180. if (!isset($data['repositories'][$matches[1]])) {
  181. throw new \InvalidArgumentException('There is no '.$matches[1].' repository defined');
  182. }
  183. $value = $data['repositories'][$matches[1]];
  184. }
  185. } elseif (strpos($settingKey, '.')) {
  186. $bits = explode('.', $settingKey);
  187. $data = $data['config'];
  188. foreach ($bits as $bit) {
  189. if (isset($data[$bit])) {
  190. $data = $data[$bit];
  191. } elseif (isset($data[implode('.', $bits)])) {
  192. // last bit can contain domain names and such so try to join whatever is left if it exists
  193. $data = $data[implode('.', $bits)];
  194. break;
  195. } else {
  196. throw new \RuntimeException($settingKey.' is not defined');
  197. }
  198. array_shift($bits);
  199. }
  200. $value = $data;
  201. } elseif (isset($data['config'][$settingKey])) {
  202. $value = $this->config->get($settingKey, $input->getOption('absolute') ? 0 : Config::RELATIVE_PATHS);
  203. } else {
  204. throw new \RuntimeException($settingKey.' is not defined');
  205. }
  206. if (is_array($value)) {
  207. $value = json_encode($value);
  208. }
  209. $this->getIO()->write($value);
  210. return 0;
  211. }
  212. $values = $input->getArgument('setting-value'); // what the user is trying to add/change
  213. $booleanValidator = function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); };
  214. $booleanNormalizer = function ($val) { return $val !== 'false' && (bool) $val; };
  215. // handle config values
  216. $uniqueConfigValues = array(
  217. 'process-timeout' => array('is_numeric', 'intval'),
  218. 'use-include-path' => array($booleanValidator, $booleanNormalizer),
  219. 'preferred-install' => array(
  220. function ($val) { return in_array($val, array('auto', 'source', 'dist'), true); },
  221. function ($val) { return $val; }
  222. ),
  223. 'store-auths' => array(
  224. function ($val) { return in_array($val, array('true', 'false', 'prompt'), true); },
  225. function ($val) {
  226. if ('prompt' === $val) {
  227. return 'prompt';
  228. }
  229. return $val !== 'false' && (bool) $val;
  230. }
  231. ),
  232. 'notify-on-install' => array($booleanValidator, $booleanNormalizer),
  233. 'vendor-dir' => array('is_string', function ($val) { return $val; }),
  234. 'bin-dir' => array('is_string', function ($val) { return $val; }),
  235. 'cache-dir' => array('is_string', function ($val) { return $val; }),
  236. 'cache-files-dir' => array('is_string', function ($val) { return $val; }),
  237. 'cache-repo-dir' => array('is_string', function ($val) { return $val; }),
  238. 'cache-vcs-dir' => array('is_string', function ($val) { return $val; }),
  239. 'cache-ttl' => array('is_numeric', 'intval'),
  240. 'cache-files-ttl' => array('is_numeric', 'intval'),
  241. 'cache-files-maxsize' => array(
  242. function ($val) { return preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $val) > 0; },
  243. function ($val) { return $val; }
  244. ),
  245. 'discard-changes' => array(
  246. function ($val) { return in_array($val, array('stash', 'true', 'false', '1', '0'), true); },
  247. function ($val) {
  248. if ('stash' === $val) {
  249. return 'stash';
  250. }
  251. return $val !== 'false' && (bool) $val;
  252. }
  253. ),
  254. 'autoloader-suffix' => array('is_string', function ($val) { return $val === 'null' ? null : $val; }),
  255. 'optimize-autoloader' => array($booleanValidator, $booleanNormalizer),
  256. 'classmap-authoritative' => array($booleanValidator, $booleanNormalizer),
  257. 'prepend-autoloader' => array($booleanValidator, $booleanNormalizer),
  258. 'github-expose-hostname' => array($booleanValidator, $booleanNormalizer),
  259. );
  260. $multiConfigValues = array(
  261. 'github-protocols' => array(
  262. function ($vals) {
  263. if (!is_array($vals)) {
  264. return 'array expected';
  265. }
  266. foreach ($vals as $val) {
  267. if (!in_array($val, array('git', 'https', 'ssh'))) {
  268. return 'valid protocols include: git, https, ssh';
  269. }
  270. }
  271. return true;
  272. },
  273. function ($vals) {
  274. return $vals;
  275. }
  276. ),
  277. 'github-domains' => array(
  278. function ($vals) {
  279. if (!is_array($vals)) {
  280. return 'array expected';
  281. }
  282. return true;
  283. },
  284. function ($vals) {
  285. return $vals;
  286. }
  287. ),
  288. );
  289. foreach ($uniqueConfigValues as $name => $callbacks) {
  290. if ($settingKey === $name) {
  291. if ($input->getOption('unset')) {
  292. return $this->configSource->removeConfigSetting($settingKey);
  293. }
  294. list($validator, $normalizer) = $callbacks;
  295. if (1 !== count($values)) {
  296. throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300');
  297. }
  298. if (true !== $validation = $validator($values[0])) {
  299. throw new \RuntimeException(sprintf(
  300. '"%s" is an invalid value'.($validation ? ' ('.$validation.')' : ''),
  301. $values[0]
  302. ));
  303. }
  304. return $this->configSource->addConfigSetting($settingKey, $normalizer($values[0]));
  305. }
  306. }
  307. foreach ($multiConfigValues as $name => $callbacks) {
  308. if ($settingKey === $name) {
  309. if ($input->getOption('unset')) {
  310. return $this->configSource->removeConfigSetting($settingKey);
  311. }
  312. list($validator, $normalizer) = $callbacks;
  313. if (true !== $validation = $validator($values)) {
  314. throw new \RuntimeException(sprintf(
  315. '%s is an invalid value'.($validation ? ' ('.$validation.')' : ''),
  316. json_encode($values)
  317. ));
  318. }
  319. return $this->configSource->addConfigSetting($settingKey, $normalizer($values));
  320. }
  321. }
  322. // handle repositories
  323. if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
  324. if ($input->getOption('unset')) {
  325. return $this->configSource->removeRepository($matches[1]);
  326. }
  327. if (2 === count($values)) {
  328. return $this->configSource->addRepository($matches[1], array(
  329. 'type' => $values[0],
  330. 'url' => $values[1],
  331. ));
  332. }
  333. if (1 === count($values)) {
  334. $bool = strtolower($values[0]);
  335. if (true === $booleanValidator($bool) && false === $booleanNormalizer($bool)) {
  336. return $this->configSource->addRepository($matches[1], false);
  337. }
  338. }
  339. throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com');
  340. }
  341. // handle github-oauth
  342. if (preg_match('/^(github-oauth|http-basic)\.(.+)/', $settingKey, $matches)) {
  343. if ($input->getOption('unset')) {
  344. $this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  345. $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  346. return;
  347. }
  348. if ($matches[1] === 'github-oauth') {
  349. if (1 !== count($values)) {
  350. throw new \RuntimeException('Too many arguments, expected only one token');
  351. }
  352. $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  353. $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], $values[0]);
  354. } elseif ($matches[1] === 'http-basic') {
  355. if (2 !== count($values)) {
  356. throw new \RuntimeException('Expected two arguments (username, password), got '.count($values));
  357. }
  358. $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  359. $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('username' => $values[0], 'password' => $values[1]));
  360. }
  361. return;
  362. }
  363. throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command');
  364. }
  365. /**
  366. * Display the contents of the file in a pretty formatted way
  367. *
  368. * @param array $contents
  369. * @param array $rawContents
  370. * @param OutputInterface $output
  371. * @param string|null $k
  372. */
  373. protected function listConfiguration(array $contents, array $rawContents, OutputInterface $output, $k = null)
  374. {
  375. $origK = $k;
  376. foreach ($contents as $key => $value) {
  377. if ($k === null && !in_array($key, array('config', 'repositories'))) {
  378. continue;
  379. }
  380. $rawVal = isset($rawContents[$key]) ? $rawContents[$key] : null;
  381. if (is_array($value) && (!is_numeric(key($value)) || ($key === 'repositories' && null === $k))) {
  382. $k .= preg_replace('{^config\.}', '', $key . '.');
  383. $this->listConfiguration($value, $rawVal, $output, $k);
  384. if (substr_count($k, '.') > 1) {
  385. $k = str_split($k, strrpos($k, '.', -2));
  386. $k = $k[0] . '.';
  387. } else {
  388. $k = $origK;
  389. }
  390. continue;
  391. }
  392. if (is_array($value)) {
  393. $value = array_map(function ($val) {
  394. return is_array($val) ? json_encode($val) : $val;
  395. }, $value);
  396. $value = '['.implode(', ', $value).']';
  397. }
  398. if (is_bool($value)) {
  399. $value = var_export($value, true);
  400. }
  401. if (is_string($rawVal) && $rawVal != $value) {
  402. $this->getIO()->write('[<comment>' . $k . $key . '</comment>] <info>' . $rawVal . ' (' . $value . ')</info>');
  403. } else {
  404. $this->getIO()->write('[<comment>' . $k . $key . '</comment>] <info>' . $value . '</info>');
  405. }
  406. }
  407. }
  408. }