ConfigCommand.php 21 KB

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