ConfigCommand.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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'),
  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') && null !== $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') ?: trim(getenv('COMPOSER')) ?: 'composer.json');
  110. // create global composer.json if this was invoked using `composer global config`
  111. if ($configFile === 'composer.json' && !file_exists($configFile) && realpath(getcwd()) === realpath($this->config->get('home'))) {
  112. file_put_contents($configFile, "{\n}\n");
  113. }
  114. $this->configFile = new JsonFile($configFile);
  115. $this->configSource = new JsonConfigSource($this->configFile);
  116. $authConfigFile = $input->getOption('global')
  117. ? ($this->config->get('home') . '/auth.json')
  118. : dirname(realpath($configFile)) . '/auth.json';
  119. $this->authConfigFile = new JsonFile($authConfigFile);
  120. $this->authConfigSource = new JsonConfigSource($this->authConfigFile, true);
  121. // initialize the global file if it's not there
  122. if ($input->getOption('global') && !$this->configFile->exists()) {
  123. touch($this->configFile->getPath());
  124. $this->configFile->write(array('config' => new \ArrayObject));
  125. @chmod($this->configFile->getPath(), 0600);
  126. }
  127. if ($input->getOption('global') && !$this->authConfigFile->exists()) {
  128. touch($this->authConfigFile->getPath());
  129. $this->authConfigFile->write(array('http-basic' => new \ArrayObject, 'github-oauth' => new \ArrayObject));
  130. @chmod($this->authConfigFile->getPath(), 0600);
  131. }
  132. if (!$this->configFile->exists()) {
  133. throw new \RuntimeException(sprintf('File "%s" cannot be found in the current directory', $configFile));
  134. }
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. protected function execute(InputInterface $input, OutputInterface $output)
  140. {
  141. // Open file in editor
  142. if ($input->getOption('editor')) {
  143. $editor = escapeshellcmd(getenv('EDITOR'));
  144. if (!$editor) {
  145. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  146. $editor = 'notepad';
  147. } else {
  148. foreach (array('vim', 'vi', 'nano', 'pico', 'ed') as $candidate) {
  149. if (exec('which '.$candidate)) {
  150. $editor = $candidate;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. $file = $input->getOption('auth') ? $this->authConfigFile->getPath() : $this->configFile->getPath();
  157. system($editor . ' ' . $file . (defined('PHP_WINDOWS_VERSION_BUILD') ? '' : ' > `tty`'));
  158. return 0;
  159. }
  160. if (!$input->getOption('global')) {
  161. $this->config->merge($this->configFile->read());
  162. $this->config->merge(array('config' => $this->authConfigFile->exists() ? $this->authConfigFile->read() : array()));
  163. }
  164. // List the configuration of the file settings
  165. if ($input->getOption('list')) {
  166. $this->listConfiguration($this->config->all(), $this->config->raw(), $output);
  167. return 0;
  168. }
  169. $settingKey = $input->getArgument('setting-key');
  170. if (!$settingKey) {
  171. return 0;
  172. }
  173. // If the user enters in a config variable, parse it and save to file
  174. if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) {
  175. throw new \RuntimeException('You can not combine a setting value with --unset');
  176. }
  177. // show the value if no value is provided
  178. if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) {
  179. $data = $this->config->all();
  180. if (preg_match('/^repos?(?:itories)?(?:\.(.+))?/', $settingKey, $matches)) {
  181. if (empty($matches[1])) {
  182. $value = isset($data['repositories']) ? $data['repositories'] : array();
  183. } else {
  184. if (!isset($data['repositories'][$matches[1]])) {
  185. throw new \InvalidArgumentException('There is no '.$matches[1].' repository defined');
  186. }
  187. $value = $data['repositories'][$matches[1]];
  188. }
  189. } elseif (strpos($settingKey, '.')) {
  190. $bits = explode('.', $settingKey);
  191. $data = $data['config'];
  192. $match = false;
  193. foreach ($bits as $bit) {
  194. $key = isset($key) ? $key.'.'.$bit : $bit;
  195. $match = false;
  196. if (isset($data[$key])) {
  197. $match = true;
  198. $data = $data[$key];
  199. unset($key);
  200. }
  201. }
  202. if (!$match) {
  203. throw new \RuntimeException($settingKey.' is not defined.');
  204. }
  205. $value = $data;
  206. } elseif (isset($data['config'][$settingKey])) {
  207. $value = $this->config->get($settingKey, $input->getOption('absolute') ? 0 : Config::RELATIVE_PATHS);
  208. } else {
  209. throw new \RuntimeException($settingKey.' is not defined');
  210. }
  211. if (is_array($value)) {
  212. $value = json_encode($value);
  213. }
  214. $this->getIO()->write($value);
  215. return 0;
  216. }
  217. $values = $input->getArgument('setting-value'); // what the user is trying to add/change
  218. $booleanValidator = function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); };
  219. $booleanNormalizer = function ($val) { return $val !== 'false' && (bool) $val; };
  220. // handle config values
  221. $uniqueConfigValues = array(
  222. 'process-timeout' => array('is_numeric', 'intval'),
  223. 'use-include-path' => array($booleanValidator, $booleanNormalizer),
  224. 'preferred-install' => array(
  225. function ($val) { return in_array($val, array('auto', 'source', 'dist'), true); },
  226. function ($val) { return $val; }
  227. ),
  228. 'store-auths' => array(
  229. function ($val) { return in_array($val, array('true', 'false', 'prompt'), true); },
  230. function ($val) {
  231. if ('prompt' === $val) {
  232. return 'prompt';
  233. }
  234. return $val !== 'false' && (bool) $val;
  235. }
  236. ),
  237. 'notify-on-install' => array($booleanValidator, $booleanNormalizer),
  238. 'vendor-dir' => array('is_string', function ($val) { return $val; }),
  239. 'bin-dir' => array('is_string', function ($val) { return $val; }),
  240. 'cache-dir' => array('is_string', function ($val) { return $val; }),
  241. 'cache-files-dir' => array('is_string', function ($val) { return $val; }),
  242. 'cache-repo-dir' => array('is_string', function ($val) { return $val; }),
  243. 'cache-vcs-dir' => array('is_string', function ($val) { return $val; }),
  244. 'cache-ttl' => array('is_numeric', 'intval'),
  245. 'cache-files-ttl' => array('is_numeric', 'intval'),
  246. 'cache-files-maxsize' => array(
  247. function ($val) { return preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $val) > 0; },
  248. function ($val) { return $val; }
  249. ),
  250. 'discard-changes' => array(
  251. function ($val) { return in_array($val, array('stash', 'true', 'false', '1', '0'), true); },
  252. function ($val) {
  253. if ('stash' === $val) {
  254. return 'stash';
  255. }
  256. return $val !== 'false' && (bool) $val;
  257. }
  258. ),
  259. 'autoloader-suffix' => array('is_string', function ($val) { return $val === 'null' ? null : $val; }),
  260. 'optimize-autoloader' => array($booleanValidator, $booleanNormalizer),
  261. 'classmap-authoritative' => array($booleanValidator, $booleanNormalizer),
  262. 'prepend-autoloader' => array($booleanValidator, $booleanNormalizer),
  263. 'github-expose-hostname' => array($booleanValidator, $booleanNormalizer),
  264. );
  265. $multiConfigValues = array(
  266. 'github-protocols' => array(
  267. function ($vals) {
  268. if (!is_array($vals)) {
  269. return 'array expected';
  270. }
  271. foreach ($vals as $val) {
  272. if (!in_array($val, array('git', 'https', 'ssh'))) {
  273. return 'valid protocols include: git, https, ssh';
  274. }
  275. }
  276. return true;
  277. },
  278. function ($vals) {
  279. return $vals;
  280. }
  281. ),
  282. 'github-domains' => array(
  283. function ($vals) {
  284. if (!is_array($vals)) {
  285. return 'array expected';
  286. }
  287. return true;
  288. },
  289. function ($vals) {
  290. return $vals;
  291. }
  292. ),
  293. );
  294. foreach ($uniqueConfigValues as $name => $callbacks) {
  295. if ($settingKey === $name) {
  296. if ($input->getOption('unset')) {
  297. return $this->configSource->removeConfigSetting($settingKey);
  298. }
  299. list($validator, $normalizer) = $callbacks;
  300. if (1 !== count($values)) {
  301. throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300');
  302. }
  303. if (true !== $validation = $validator($values[0])) {
  304. throw new \RuntimeException(sprintf(
  305. '"%s" is an invalid value'.($validation ? ' ('.$validation.')' : ''),
  306. $values[0]
  307. ));
  308. }
  309. return $this->configSource->addConfigSetting($settingKey, $normalizer($values[0]));
  310. }
  311. }
  312. foreach ($multiConfigValues as $name => $callbacks) {
  313. if ($settingKey === $name) {
  314. if ($input->getOption('unset')) {
  315. return $this->configSource->removeConfigSetting($settingKey);
  316. }
  317. list($validator, $normalizer) = $callbacks;
  318. if (true !== $validation = $validator($values)) {
  319. throw new \RuntimeException(sprintf(
  320. '%s is an invalid value'.($validation ? ' ('.$validation.')' : ''),
  321. json_encode($values)
  322. ));
  323. }
  324. return $this->configSource->addConfigSetting($settingKey, $normalizer($values));
  325. }
  326. }
  327. // handle repositories
  328. if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) {
  329. if ($input->getOption('unset')) {
  330. return $this->configSource->removeRepository($matches[1]);
  331. }
  332. if (2 === count($values)) {
  333. return $this->configSource->addRepository($matches[1], array(
  334. 'type' => $values[0],
  335. 'url' => $values[1],
  336. ));
  337. }
  338. if (1 === count($values)) {
  339. $bool = strtolower($values[0]);
  340. if (true === $booleanValidator($bool) && false === $booleanNormalizer($bool)) {
  341. return $this->configSource->addRepository($matches[1], false);
  342. }
  343. }
  344. throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com');
  345. }
  346. // handle platform
  347. if (preg_match('/^platform\.(.+)/', $settingKey, $matches)) {
  348. if ($input->getOption('unset')) {
  349. return $this->configSource->removeConfigSetting($settingKey);
  350. }
  351. return $this->configSource->addConfigSetting($settingKey, $values[0]);
  352. }
  353. // handle github-oauth
  354. if (preg_match('/^(github-oauth|http-basic)\.(.+)/', $settingKey, $matches)) {
  355. if ($input->getOption('unset')) {
  356. $this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  357. $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  358. return;
  359. }
  360. if ($matches[1] === 'github-oauth') {
  361. if (1 !== count($values)) {
  362. throw new \RuntimeException('Too many arguments, expected only one token');
  363. }
  364. $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  365. $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], $values[0]);
  366. } elseif ($matches[1] === 'http-basic') {
  367. if (2 !== count($values)) {
  368. throw new \RuntimeException('Expected two arguments (username, password), got '.count($values));
  369. }
  370. $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]);
  371. $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('username' => $values[0], 'password' => $values[1]));
  372. }
  373. return;
  374. }
  375. throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command');
  376. }
  377. /**
  378. * Display the contents of the file in a pretty formatted way
  379. *
  380. * @param array $contents
  381. * @param array $rawContents
  382. * @param OutputInterface $output
  383. * @param string|null $k
  384. */
  385. protected function listConfiguration(array $contents, array $rawContents, OutputInterface $output, $k = null)
  386. {
  387. $origK = $k;
  388. $io = $this->getIO();
  389. foreach ($contents as $key => $value) {
  390. if ($k === null && !in_array($key, array('config', 'repositories'))) {
  391. continue;
  392. }
  393. $rawVal = isset($rawContents[$key]) ? $rawContents[$key] : null;
  394. if (is_array($value) && (!is_numeric(key($value)) || ($key === 'repositories' && null === $k))) {
  395. $k .= preg_replace('{^config\.}', '', $key . '.');
  396. $this->listConfiguration($value, $rawVal, $output, $k);
  397. if (substr_count($k, '.') > 1) {
  398. $k = str_split($k, strrpos($k, '.', -2));
  399. $k = $k[0] . '.';
  400. } else {
  401. $k = $origK;
  402. }
  403. continue;
  404. }
  405. if (is_array($value)) {
  406. $value = array_map(function ($val) {
  407. return is_array($val) ? json_encode($val) : $val;
  408. }, $value);
  409. $value = '['.implode(', ', $value).']';
  410. }
  411. if (is_bool($value)) {
  412. $value = var_export($value, true);
  413. }
  414. if (is_string($rawVal) && $rawVal != $value) {
  415. $io->write('[<comment>' . $k . $key . '</comment>] <info>' . $rawVal . ' (' . $value . ')</info>');
  416. } else {
  417. $io->write('[<comment>' . $k . $key . '</comment>] <info>' . $value . '</info>');
  418. }
  419. }
  420. }
  421. }