ConfigCommand.php 21 KB

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