Beispiel #1
0
 /**
  * Remove packages from the root install.
  *
  * @param  $packages array Indexed array of package names to remove
  *
  * @throws \Bolt\Exception\PackageManagerException
  *
  * @return int 0 on success or a positive error code on failure
  */
 public function execute(array $packages)
 {
     if (empty($packages)) {
         throw new PackageManagerException('No package specified for removal');
     }
     $io = $this->app['extend.manager']->getIO();
     $options = $this->app['extend.manager']->getOptions();
     $jsonFile = new JsonFile($options['composerjson']);
     $composerDefinition = $jsonFile->read();
     $composerBackup = file_get_contents($jsonFile->getPath());
     $json = new JsonConfigSource($jsonFile);
     $type = $options['dev'] ? 'require-dev' : 'require';
     // Remove packages from JSON
     foreach ($packages as $package) {
         if (isset($composerDefinition[$type][$package])) {
             $json->removeLink($type, $package);
         }
     }
     // Reload Composer config
     $composer = $this->app['extend.manager']->getFactory()->resetComposer();
     $install = Installer::create($io, $composer);
     try {
         $install->setVerbose($options['verbose'])->setDevMode(!$options['updatenodev'])->setUpdate(true)->setUpdateWhitelist($packages)->setWhitelistDependencies($options['updatewithdependencies'])->setIgnorePlatformRequirements($options['ignoreplatformreqs']);
         $status = $install->run();
         if ($status !== 0) {
             // Write out old JSON file
             file_put_contents($jsonFile->getPath(), $composerBackup);
         }
     } catch (\Exception $e) {
         $msg = __CLASS__ . '::' . __FUNCTION__ . ' recieved an error from Composer: ' . $e->getMessage() . ' in ' . $e->getFile() . '::' . $e->getLine();
         $this->app['logger.system']->critical($msg, array('event' => 'exception', 'exception' => $e));
         throw new PackageManagerException($e->getMessage(), $e->getCode(), $e);
     }
     return $status;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $packages = $input->getArgument('packages');
     $file = Factory::getComposerFile();
     $jsonFile = new JsonFile($file);
     $composer = $jsonFile->read();
     $composerBackup = file_get_contents($jsonFile->getPath());
     $json = new JsonConfigSource($jsonFile);
     $type = $input->getOption('dev') ? 'require-dev' : 'require';
     $altType = !$input->getOption('dev') ? 'require-dev' : 'require';
     $io = $this->getIO();
     if ($input->getOption('update-with-dependencies')) {
         $io->writeError('<warning>You are using the deprecated option "update-with-dependencies". This is now default behaviour. The --no-update-with-dependencies option can be used to remove a package without its dependencies.</warning>');
     }
     foreach ($packages as $package) {
         if (isset($composer[$type][$package])) {
             $json->removeLink($type, $package);
         } elseif (isset($composer[$altType][$package])) {
             $io->writeError('<warning>' . $package . ' could not be found in ' . $type . ' but it is present in ' . $altType . '</warning>');
             if ($io->isInteractive()) {
                 if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [<comment>yes</comment>]? ', true)) {
                     $json->removeLink($altType, $package);
                 }
             }
         } else {
             $io->writeError('<warning>' . $package . ' is not required in your composer.json and has not been removed</warning>');
         }
     }
     if ($input->getOption('no-update')) {
         return 0;
     }
     // Update packages
     $this->resetComposer();
     $composer = $this->getComposer(true, $input->getOption('no-plugins'));
     $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
     $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
     $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
     $install = Installer::create($io, $composer);
     $updateDevMode = !$input->getOption('update-no-dev');
     $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader');
     $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative');
     $install->setVerbose($input->getOption('verbose'))->setDevMode($updateDevMode)->setOptimizeAutoloader($optimize)->setClassMapAuthoritative($authoritative)->setUpdate(true)->setUpdateWhitelist($packages)->setWhitelistDependencies(!$input->getOption('no-update-with-dependencies'))->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
     $exception = null;
     try {
         $status = $install->run();
     } catch (\Exception $exception) {
         $status = 1;
     }
     if ($status !== 0) {
         $io->writeError("\n" . '<error>Removal failed, reverting ' . $file . ' to its original content.</error>');
         file_put_contents($jsonFile->getPath(), $composerBackup);
     }
     if ($exception) {
         throw $exception;
     }
     return $status;
 }
Beispiel #3
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $packages = $input->getArgument('packages');
     $file = Factory::getComposerFile();
     $jsonFile = new JsonFile($file);
     $composer = $jsonFile->read();
     $composerBackup = file_get_contents($jsonFile->getPath());
     $json = new JsonConfigSource($jsonFile);
     $type = $input->getOption('dev') ? 'require-dev' : 'require';
     $altType = !$input->getOption('dev') ? 'require-dev' : 'require';
     foreach ($packages as $package) {
         if (isset($composer[$type][$package])) {
             $json->removeLink($type, $package);
         } elseif (isset($composer[$altType][$package])) {
             $this->getIO()->writeError('<warning>' . $package . ' could not be found in ' . $type . ' but it is present in ' . $altType . '</warning>');
             $dialog = $this->getHelperSet()->get('dialog');
             if ($this->getIO()->isInteractive()) {
                 if ($dialog->askConfirmation($output, $dialog->getQuestion('Do you want to remove it from ' . $altType, 'yes', '?'), true)) {
                     $json->removeLink($altType, $package);
                 }
             }
         } else {
             $this->getIO()->writeError('<warning>' . $package . ' is not required in your composer.json and has not been removed</warning>');
         }
     }
     if ($input->getOption('no-update')) {
         return 0;
     }
     // Update packages
     $composer = $this->getComposer();
     $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
     $io = $this->getIO();
     $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
     $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
     $install = Installer::create($io, $composer);
     $updateDevMode = !$input->getOption('update-no-dev');
     $install->setVerbose($input->getOption('verbose'))->setDevMode($updateDevMode)->setUpdate(true)->setUpdateWhitelist($packages)->setWhitelistDependencies($input->getOption('update-with-dependencies'))->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));
     $status = $install->run();
     if ($status !== 0) {
         $this->getIO()->writeError("\n" . '<error>Removal failed, reverting ' . $file . ' to its original content.</error>');
         file_put_contents($jsonFile->getPath(), $composerBackup);
     }
     return $status;
 }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Open file in editor
     if ($input->getOption('editor')) {
         $editor = escapeshellcmd(getenv('EDITOR'));
         if (!$editor) {
             if (Platform::isWindows()) {
                 $editor = 'notepad';
             } else {
                 foreach (array('editor', 'vim', 'vi', 'nano', 'pico', 'ed') as $candidate) {
                     if (exec('which ' . $candidate)) {
                         $editor = $candidate;
                         break;
                     }
                 }
             }
         }
         $file = $input->getOption('auth') ? $this->authConfigFile->getPath() : $this->configFile->getPath();
         system($editor . ' ' . $file . (Platform::isWindows() ? '' : ' > `tty`'));
         return 0;
     }
     if (!$input->getOption('global')) {
         $this->config->merge($this->configFile->read());
         $this->config->merge(array('config' => $this->authConfigFile->exists() ? $this->authConfigFile->read() : array()));
     }
     // List the configuration of the file settings
     if ($input->getOption('list')) {
         $this->listConfiguration($this->config->all(), $this->config->raw(), $output);
         return 0;
     }
     $settingKey = $input->getArgument('setting-key');
     if (!$settingKey) {
         return 0;
     }
     // If the user enters in a config variable, parse it and save to file
     if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) {
         throw new \RuntimeException('You can not combine a setting value with --unset');
     }
     // show the value if no value is provided
     if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) {
         $data = $this->config->all();
         if (preg_match('/^repos?(?:itories)?(?:\\.(.+))?/', $settingKey, $matches)) {
             if (empty($matches[1])) {
                 $value = isset($data['repositories']) ? $data['repositories'] : array();
             } else {
                 if (!isset($data['repositories'][$matches[1]])) {
                     throw new \InvalidArgumentException('There is no ' . $matches[1] . ' repository defined');
                 }
                 $value = $data['repositories'][$matches[1]];
             }
         } elseif (strpos($settingKey, '.')) {
             $bits = explode('.', $settingKey);
             $data = $data['config'];
             $match = false;
             foreach ($bits as $bit) {
                 $key = isset($key) ? $key . '.' . $bit : $bit;
                 $match = false;
                 if (isset($data[$key])) {
                     $match = true;
                     $data = $data[$key];
                     unset($key);
                 }
             }
             if (!$match) {
                 throw new \RuntimeException($settingKey . ' is not defined.');
             }
             $value = $data;
         } elseif (isset($data['config'][$settingKey])) {
             $value = $this->config->get($settingKey, $input->getOption('absolute') ? 0 : Config::RELATIVE_PATHS);
         } else {
             throw new \RuntimeException($settingKey . ' is not defined');
         }
         if (is_array($value)) {
             $value = json_encode($value);
         }
         $this->getIO()->write($value);
         return 0;
     }
     $values = $input->getArgument('setting-value');
     // what the user is trying to add/change
     $booleanValidator = function ($val) {
         return in_array($val, array('true', 'false', '1', '0'), true);
     };
     $booleanNormalizer = function ($val) {
         return $val !== 'false' && (bool) $val;
     };
     // handle config values
     $uniqueConfigValues = array('process-timeout' => array('is_numeric', 'intval'), 'use-include-path' => array($booleanValidator, $booleanNormalizer), 'preferred-install' => array(function ($val) {
         return in_array($val, array('auto', 'source', 'dist'), true);
     }, function ($val) {
         return $val;
     }), 'store-auths' => array(function ($val) {
         return in_array($val, array('true', 'false', 'prompt'), true);
     }, function ($val) {
         if ('prompt' === $val) {
             return 'prompt';
         }
         return $val !== 'false' && (bool) $val;
     }), 'notify-on-install' => array($booleanValidator, $booleanNormalizer), 'vendor-dir' => array('is_string', function ($val) {
         return $val;
     }), 'bin-dir' => array('is_string', function ($val) {
         return $val;
     }), 'archive-dir' => array('is_string', function ($val) {
         return $val;
     }), 'archive-format' => array('is_string', function ($val) {
         return $val;
     }), 'data-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-files-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-repo-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-vcs-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-ttl' => array('is_numeric', 'intval'), 'cache-files-ttl' => array('is_numeric', 'intval'), 'cache-files-maxsize' => array(function ($val) {
         return preg_match('/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i', $val) > 0;
     }, function ($val) {
         return $val;
     }), 'bin-compat' => array(function ($val) {
         return in_array($val, array('auto', 'full'));
     }, function ($val) {
         return $val;
     }), 'discard-changes' => array(function ($val) {
         return in_array($val, array('stash', 'true', 'false', '1', '0'), true);
     }, function ($val) {
         if ('stash' === $val) {
             return 'stash';
         }
         return $val !== 'false' && (bool) $val;
     }), 'autoloader-suffix' => array('is_string', function ($val) {
         return $val === 'null' ? null : $val;
     }), 'sort-packages' => array($booleanValidator, $booleanNormalizer), 'optimize-autoloader' => array($booleanValidator, $booleanNormalizer), 'classmap-authoritative' => array($booleanValidator, $booleanNormalizer), 'prepend-autoloader' => array($booleanValidator, $booleanNormalizer), 'disable-tls' => array($booleanValidator, $booleanNormalizer), 'secure-http' => array($booleanValidator, $booleanNormalizer), 'cafile' => array(function ($val) {
         return file_exists($val) && is_readable($val);
     }, function ($val) {
         return $val === 'null' ? null : $val;
     }), 'capath' => array(function ($val) {
         return is_dir($val) && is_readable($val);
     }, function ($val) {
         return $val === 'null' ? null : $val;
     }), 'github-expose-hostname' => array($booleanValidator, $booleanNormalizer));
     $multiConfigValues = array('github-protocols' => array(function ($vals) {
         if (!is_array($vals)) {
             return 'array expected';
         }
         foreach ($vals as $val) {
             if (!in_array($val, array('git', 'https', 'ssh'))) {
                 return 'valid protocols include: git, https, ssh';
             }
         }
         return true;
     }, function ($vals) {
         return $vals;
     }), 'github-domains' => array(function ($vals) {
         if (!is_array($vals)) {
             return 'array expected';
         }
         return true;
     }, function ($vals) {
         return $vals;
     }), 'gitlab-domains' => array(function ($vals) {
         if (!is_array($vals)) {
             return 'array expected';
         }
         return true;
     }, function ($vals) {
         return $vals;
     }));
     foreach ($uniqueConfigValues as $name => $callbacks) {
         if ($settingKey === $name) {
             if ($input->getOption('unset')) {
                 return $this->configSource->removeConfigSetting($settingKey);
             }
             list($validator, $normalizer) = $callbacks;
             if (1 !== count($values)) {
                 throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300');
             }
             if (true !== ($validation = $validator($values[0]))) {
                 throw new \RuntimeException(sprintf('"%s" is an invalid value' . ($validation ? ' (' . $validation . ')' : ''), $values[0]));
             }
             return $this->configSource->addConfigSetting($settingKey, $normalizer($values[0]));
         }
     }
     foreach ($multiConfigValues as $name => $callbacks) {
         if ($settingKey === $name) {
             if ($input->getOption('unset')) {
                 return $this->configSource->removeConfigSetting($settingKey);
             }
             list($validator, $normalizer) = $callbacks;
             if (true !== ($validation = $validator($values))) {
                 throw new \RuntimeException(sprintf('%s is an invalid value' . ($validation ? ' (' . $validation . ')' : ''), json_encode($values)));
             }
             return $this->configSource->addConfigSetting($settingKey, $normalizer($values));
         }
     }
     // handle repositories
     if (preg_match('/^repos?(?:itories)?\\.(.+)/', $settingKey, $matches)) {
         if ($input->getOption('unset')) {
             return $this->configSource->removeRepository($matches[1]);
         }
         if (2 === count($values)) {
             return $this->configSource->addRepository($matches[1], array('type' => $values[0], 'url' => $values[1]));
         }
         if (1 === count($values)) {
             $value = strtolower($values[0]);
             if (true === $booleanValidator($value)) {
                 if (false === $booleanNormalizer($value)) {
                     return $this->configSource->addRepository($matches[1], false);
                 }
             } else {
                 $value = JsonFile::parseJson($values[0]);
                 return $this->configSource->addRepository($matches[1], $value);
             }
         }
         throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs https://bar.com');
     }
     // handle platform
     if (preg_match('/^platform\\.(.+)/', $settingKey, $matches)) {
         if ($input->getOption('unset')) {
             return $this->configSource->removeConfigSetting($settingKey);
         }
         return $this->configSource->addConfigSetting($settingKey, $values[0]);
     }
     // handle github-oauth
     if (preg_match('/^(github-oauth|gitlab-oauth|http-basic)\\.(.+)/', $settingKey, $matches)) {
         if ($input->getOption('unset')) {
             $this->authConfigSource->removeConfigSetting($matches[1] . '.' . $matches[2]);
             $this->configSource->removeConfigSetting($matches[1] . '.' . $matches[2]);
             return;
         }
         if ($matches[1] === 'github-oauth' || $matches[1] === 'gitlab-oauth') {
             if (1 !== count($values)) {
                 throw new \RuntimeException('Too many arguments, expected only one token');
             }
             $this->configSource->removeConfigSetting($matches[1] . '.' . $matches[2]);
             $this->authConfigSource->addConfigSetting($matches[1] . '.' . $matches[2], $values[0]);
         } elseif ($matches[1] === 'http-basic') {
             if (2 !== count($values)) {
                 throw new \RuntimeException('Expected two arguments (username, password), got ' . count($values));
             }
             $this->configSource->removeConfigSetting($matches[1] . '.' . $matches[2]);
             $this->authConfigSource->addConfigSetting($matches[1] . '.' . $matches[2], array('username' => $values[0], 'password' => $values[1]));
         }
         return;
     }
     throw new \InvalidArgumentException('Setting ' . $settingKey . ' does not exist or is not supported by this command');
 }
 /**
  * Test removeLink()
  *
  * @param string $sourceFile     Source file
  * @param string $type           Type (require, require-dev, provide, suggest, replace, conflict)
  * @param string $name           Name
  * @param string $compareAgainst File to compare against after making changes
  *
  * @dataProvider provideRemoveLinkData
  */
 public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
 {
     $composerJson = $this->workingDir . '/composer.json';
     copy($sourceFile, $composerJson);
     $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
     $jsonConfigSource->removeLink($type, $name);
     $this->assertFileEquals($compareAgainst, $composerJson);
 }
 public function installProject(IOInterface $io, Config $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disablePlugins = false, $noScripts = false, $keepVcs = false, $noProgress = false, $noInstall = false, $ignorePlatformReqs = false, InputInterface $input)
 {
     $oldCwd = getcwd();
     // we need to manually load the configuration to pass the auth credentials to the io interface!
     $io->loadConfiguration($config);
     if ($packageName !== null) {
         $installedFromVcs = $this->installRootPackage($io, $config, $packageName, $directory, $packageVersion, $stability, $preferSource, $preferDist, $installDevPackages, $repositoryUrl, $disablePlugins, $noScripts, $keepVcs, $noProgress);
     } else {
         $installedFromVcs = false;
     }
     $composer = Factory::create($io, null, $disablePlugins);
     $composer->getDownloadManager()->setOutputProgress(!$noProgress);
     $fs = new Filesystem();
     if ($noScripts === false) {
         // dispatch event
         $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ROOT_PACKAGE_INSTALL, $installDevPackages);
     }
     $rootPackageConfig = $composer->getConfig();
     $this->updatePreferredOptions($rootPackageConfig, $input, $preferSource, $preferDist);
     // install dependencies of the created project
     if ($noInstall === false) {
         $installer = Installer::create($io, $composer);
         $installer->setPreferSource($preferSource)->setPreferDist($preferDist)->setDevMode($installDevPackages)->setRunScripts(!$noScripts)->setIgnorePlatformRequirements($ignorePlatformReqs);
         if ($disablePlugins) {
             $installer->disablePlugins();
         }
         $status = $installer->run();
         if (0 !== $status) {
             return $status;
         }
     }
     $hasVcs = $installedFromVcs;
     if (!$keepVcs && $installedFromVcs && (!$io->isInteractive() || $io->askConfirmation('<info>Do you want to remove the existing VCS (.git, .svn..) history?</info> [<comment>Y,n</comment>]? ', true))) {
         $finder = new Finder();
         $finder->depth(0)->directories()->in(getcwd())->ignoreVCS(false)->ignoreDotFiles(false);
         foreach (array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg') as $vcsName) {
             $finder->name($vcsName);
         }
         try {
             $dirs = iterator_to_array($finder);
             unset($finder);
             foreach ($dirs as $dir) {
                 if (!$fs->removeDirectory($dir)) {
                     throw new \RuntimeException('Could not remove ' . $dir);
                 }
             }
         } catch (\Exception $e) {
             $io->writeError('<error>An error occurred while removing the VCS metadata: ' . $e->getMessage() . '</error>');
         }
         $hasVcs = false;
     }
     // rewriting self.version dependencies with explicit version numbers if the package's vcs metadata is gone
     if (!$hasVcs) {
         $package = $composer->getPackage();
         $configSource = new JsonConfigSource(new JsonFile('composer.json'));
         foreach (BasePackage::$supportedLinkTypes as $type => $meta) {
             foreach ($package->{'get' . $meta['method']}() as $link) {
                 if ($link->getPrettyConstraint() === 'self.version') {
                     $configSource->addLink($type, $link->getTarget(), $package->getPrettyVersion());
                 }
             }
         }
     }
     if ($noScripts === false) {
         // dispatch event
         $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_CREATE_PROJECT_CMD, $installDevPackages);
     }
     chdir($oldCwd);
     $vendorComposerDir = $composer->getConfig()->get('vendor-dir') . '/composer';
     if (is_dir($vendorComposerDir) && $fs->isDirEmpty($vendorComposerDir)) {
         @rmdir($vendorComposerDir);
         $vendorDir = $composer->getConfig()->get('vendor-dir');
         if (is_dir($vendorDir) && $fs->isDirEmpty($vendorDir)) {
             @rmdir($vendorDir);
         }
     }
     return 0;
 }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Open file in editor
     if ($input->getOption('editor')) {
         $editor = getenv('EDITOR');
         if (!$editor) {
             if (defined('PHP_WINDOWS_VERSION_BUILD')) {
                 $editor = 'notepad';
             } else {
                 foreach (array('vim', 'vi', 'nano', 'pico', 'ed') as $candidate) {
                     if (exec('which ' . $candidate)) {
                         $editor = $candidate;
                         break;
                     }
                 }
             }
         }
         system($editor . ' ' . $this->configFile->getPath() . (defined('PHP_WINDOWS_VERSION_BUILD') ? '' : ' > `tty`'));
         return 0;
     }
     if (!$input->getOption('global')) {
         $this->config->merge($this->configFile->read());
     }
     // List the configuration of the file settings
     if ($input->getOption('list')) {
         $this->listConfiguration($this->config->all(), $this->config->raw(), $output);
         return 0;
     }
     $settingKey = $input->getArgument('setting-key');
     if (!$settingKey) {
         return 0;
     }
     // If the user enters in a config variable, parse it and save to file
     if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) {
         throw new \RuntimeException('You can not combine a setting value with --unset');
     }
     // show the value if no value is provided
     if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) {
         $data = $this->config->all();
         if (preg_match('/^repos?(?:itories)?(?:\\.(.+))?/', $settingKey, $matches)) {
             if (empty($matches[1])) {
                 $value = isset($data['repositories']) ? $data['repositories'] : array();
             } else {
                 if (!isset($data['repositories'][$matches[1]])) {
                     throw new \InvalidArgumentException('There is no ' . $matches[1] . ' repository defined');
                 }
                 $value = $data['repositories'][$matches[1]];
             }
         } elseif (strpos($settingKey, '.')) {
             $bits = explode('.', $settingKey);
             $data = $data['config'];
             foreach ($bits as $bit) {
                 if (isset($data[$bit])) {
                     $data = $data[$bit];
                 } elseif (isset($data[implode('.', $bits)])) {
                     // last bit can contain domain names and such so try to join whatever is left if it exists
                     $data = $data[implode('.', $bits)];
                     break;
                 } else {
                     throw new \RuntimeException($settingKey . ' is not defined');
                 }
                 array_shift($bits);
             }
             $value = $data;
         } elseif (isset($data['config'][$settingKey])) {
             $value = $data['config'][$settingKey];
         } else {
             throw new \RuntimeException($settingKey . ' is not defined');
         }
         if (is_array($value)) {
             $value = json_encode($value);
         }
         $output->writeln($value);
         return 0;
     }
     $values = $input->getArgument('setting-value');
     // what the user is trying to add/change
     // handle repositories
     if (preg_match('/^repos?(?:itories)?\\.(.+)/', $settingKey, $matches)) {
         if ($input->getOption('unset')) {
             return $this->configSource->removeRepository($matches[1]);
         }
         if (2 !== count($values)) {
             throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com');
         }
         return $this->configSource->addRepository($matches[1], array('type' => $values[0], 'url' => $values[1]));
     }
     // handle github-oauth
     if (preg_match('/^github-oauth\\.(.+)/', $settingKey, $matches)) {
         if ($input->getOption('unset')) {
             return $this->configSource->removeConfigSetting('github-oauth.' . $matches[1]);
         }
         if (1 !== count($values)) {
             throw new \RuntimeException('Too many arguments, expected only one token');
         }
         return $this->configSource->addConfigSetting('github-oauth.' . $matches[1], $values[0]);
     }
     $booleanValidator = function ($val) {
         return in_array($val, array('true', 'false', '1', '0'), true);
     };
     $booleanNormalizer = function ($val) {
         return $val !== 'false' && (bool) $val;
     };
     // handle config values
     $uniqueConfigValues = array('process-timeout' => array('is_numeric', 'intval'), 'use-include-path' => array($booleanValidator, $booleanNormalizer), 'preferred-install' => array(function ($val) {
         return in_array($val, array('auto', 'source', 'dist'), true);
     }, function ($val) {
         return $val;
     }), 'notify-on-install' => array($booleanValidator, $booleanNormalizer), 'vendor-dir' => array('is_string', function ($val) {
         return $val;
     }), 'bin-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-files-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-repo-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-vcs-dir' => array('is_string', function ($val) {
         return $val;
     }), 'cache-ttl' => array('is_numeric', 'intval'), 'cache-files-ttl' => array('is_numeric', 'intval'), 'cache-files-maxsize' => array(function ($val) {
         return preg_match('/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i', $val) > 0;
     }, function ($val) {
         return $val;
     }), 'discard-changes' => array(function ($val) {
         return in_array($val, array('stash', 'true', 'false', '1', '0'), true);
     }, function ($val) {
         if ('stash' === $val) {
             return 'stash';
         }
         return $val !== 'false' && (bool) $val;
     }));
     $multiConfigValues = array('github-protocols' => array(function ($vals) {
         if (!is_array($vals)) {
             return 'array expected';
         }
         foreach ($vals as $val) {
             if (!in_array($val, array('git', 'https', 'http'))) {
                 return 'valid protocols include: git, https, http';
             }
         }
         return true;
     }, function ($vals) {
         return $vals;
     }));
     foreach ($uniqueConfigValues as $name => $callbacks) {
         if ($settingKey === $name) {
             if ($input->getOption('unset')) {
                 return $this->configSource->removeConfigSetting($settingKey);
             }
             list($validator, $normalizer) = $callbacks;
             if (1 !== count($values)) {
                 throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300');
             }
             if (true !== ($validation = $validator($values[0]))) {
                 throw new \RuntimeException(sprintf('"%s" is an invalid value' . ($validation ? ' (' . $validation . ')' : ''), $values[0]));
             }
             return $this->configSource->addConfigSetting($settingKey, $normalizer($values[0]));
         }
     }
     foreach ($multiConfigValues as $name => $callbacks) {
         if ($settingKey === $name) {
             if ($input->getOption('unset')) {
                 return $this->configSource->removeConfigSetting($settingKey);
             }
             list($validator, $normalizer) = $callbacks;
             if (true !== ($validation = $validator($values))) {
                 throw new \RuntimeException(sprintf('%s is an invalid value' . ($validation ? ' (' . $validation . ')' : ''), json_encode($values)));
             }
             return $this->configSource->addConfigSetting($settingKey, $normalizer($values));
         }
     }
     throw new \InvalidArgumentException('Setting ' . $settingKey . ' does not exist or is not supported by this command');
 }