<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use JsonSchema\Validator;
use Composer\Config;
use Composer\Factory;
use Composer\Json\JsonFile;
use Composer\Json\JsonValidationException;

/**
 * @author <NAME> <<EMAIL>>
 */
class ConfigCommand extends Command
{
    /**
     * @var Composer\Json\JsonFile
     */
    protected $configFile;

    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        $this
            ->setName('config')
            ->setDescription('Set config options')
            ->setDefinition(array(
                new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'),
                new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'),
                new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'),
                new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json', 'composer.json'),
                new InputArgument('setting-key', null, 'Setting key'),
                new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'),
            ))
            ->setHelp(<<<EOT
This command allows you to edit some basic composer settings in either the
local composer.json file or the global config.json file.

To edit the global config.json file:

    <comment>php composer.phar --global</comment>

To add a repository:

    <comment>php composer.phar repositories.foo vcs http://bar.com</comment>

You can add a repository to the global config.json file by passing in the
<info>--global</info> option.

To edit the file in an external editor:

    <comment>php composer.phar --edit</comment>

To choose your editor you can set the "EDITOR" env variable.

To get a list of configuration values in the file:

    <comment>php composer.phar --list</comment>

You can always pass more than one option. As an example, if you want to edit the
global config.json file.

    <comment>php composer.phar --edit --global</comment>
EOT
            )
        ;
    }

    /**
     * {@inheritDoc}
     */
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('global') && 'composer.json' !== $input->getOption('file')) {
            throw new \RuntimeException('--file and --global can not be combined');
        }

        // Get the local composer.json, global config.json, or if the user
        // passed in a file to use
        $this->configFile = $input->getOption('global')
            ? (Factory::createConfig()->get('home') . '/config.json')
            : $input->getOption('file');

        $this->configFile = new JsonFile($this->configFile);
        if (!$this->configFile->exists()) {
            touch($this->configFile->getPath());
            // If you read an empty file, Composer throws an error
            // Toss some of the defaults in there
            $defaults = Config::$defaultConfig;
            $defaults['repositories'] = Config::$defaultRepositories;
            $this->configFile->write($defaults);
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Open file in editor
        if ($input->getOption('editor')) {
            // @todo Find a way to use another editor
            $editor = system("bash -cl 'echo \$EDITOR'");
            system($editor . ' ' . $this->configFile->getPath() . ' > `tty`');
            return 0;
        }

        // List the configuration of the file settings
        if ($input->getOption('list')) {
            $this->displayFileContents($this->configFile->read(), $output);

            return 0;
        }

        // If the user enters in a config variable, parse it and save to file
        if ($input->getArgument('setting-key')) {
            if (null === $input->getArgument('setting-value')) {
                throw new \RuntimeException('You must include a setting value.');
            }

            /**
             * The user needs the ability to add a repository with one command.
             * For example "config -g repository.foo 'vcs http://example.com'
             */
            $configSettings = $this->configFile->read(); // what is current in the config
            $settings       = array(); // This will what will be merged into the above
            $values         = $input->getArgument('setting-value'); // what the user is trying to add/change

            // Checking for each known config value is going to make this method very large
            // what is a better way to do this?

            // repositories.foo
            if (preg_match('/^repositories\.(.+)/', $input->getArgument('setting-key'), $matches)) {
                if (2 !== count($values)) {<commit_msg>Use default editors if EDITOR is not set, improve windows compatibility<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use JsonSchema\Validator;
use Composer\Config;
use Composer\Factory;
use Composer\Json\JsonFile;
use Composer\Json\JsonValidationException;

/**
 * @author <NAME> <<EMAIL>>
 */
class ConfigCommand extends Command
{
    /**
     * @var Composer\Json\JsonFile
     */
    protected $configFile;

    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        $this
            ->setName('config')
            ->setDescription('Set config options')
            ->setDefinition(array(
                new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'),
                new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'),
                new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'),
                new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json', 'composer.json'),
                new InputArgument('setting-key', null, 'Setting key'),
                new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'),
            ))
            ->setHelp(<<<EOT
This command allows you to edit some basic composer settings in either the
local composer.json file or the global config.json file.

To edit the global config.json file:

    <comment>php composer.phar --global</comment>

To add a repository:

    <comment>php composer.phar repositories.foo vcs http://bar.com</comment>

You can add a repository to the global config.json file by passing in the
<info>--global</info> option.

To edit the file in an external editor:

    <comment>php composer.phar --edit</comment>

To choose your editor you can set the "EDITOR" env variable.

To get a list of configuration values in the file:

    <comment>php composer.phar --list</comment>

You can always pass more than one option. As an example, if you want to edit the
global config.json file.

    <comment>php composer.phar --edit --global</comment>
EOT
            )
        ;
    }

    /**
     * {@inheritDoc}
     */
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        if ($input->getOption('global') && 'composer.json' !== $input->getOption('file')) {
            throw new \RuntimeException('--file and --global can not be combined');
        }

        // Get the local composer.json, global config.json, or if the user
        // passed in a file to use
        $this->configFile = $input->getOption('global')
            ? (Factory::createConfig()->get('home') . '/config.json')
            : $input->getOption('file');

        $this->configFile = new JsonFile($this->configFile);
        if (!$this->configFile->exists()) {
            touch($this->configFile->getPath());
            // If you read an empty file, Composer throws an error
            // Toss some of the defaults in there
            $defaults = Config::$defaultConfig;
            $defaults['repositories'] = Config::$defaultRepositories;
            $this->configFile->write($defaults);
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Open file in editor
        if ($input->getOption('editor')) {
            $editor = getenv('EDITOR');
            if (!$editor) {
                $editor = defined('PHP_WINDOWS_VERSION_BUILD') ? 'notepad' : 'vi';
            }

            system($editor . ' ' . $this->configFile->getPath() . (defined('PHP_WINDOWS_VERSION_BUILD') ? '':  ' > `tty`'));

            return 0;
        }

        // List the configuration of the file settings
        if ($input->getOption('list')) {
            $this->displayFileContents($this->configFile->read(), $output);

            return 0;
        }

        // If the user enters in a config variable, parse it and save to file
        if ($input->getArgument('setting-key')) {
            if (null === $input->getArgument('setting-value')) {
                throw new \RuntimeException('You must include a setting value.');
            }

            /**
             * The user needs the ability to add a repository with one command.
             * For example "config -g repository.foo 'vcs http://example.com'
             */
            $configSettings = $this->configFile->read(); // what is current in the config
            $settings       = array(); // This will what will be merged into the above
            $values         = $input->getArgument('setting-value'); // what the user is trying to add/change

            // Checking for each known config value is going to make this method very large
            // what is a better way to do this?

            // repositories.foo
            if (preg_match('/^repositories\.(.+)/', $input->getArgument('setting-key'), $matches)) {
                if (2 !== count($values)) {
{'fraction_non_alphanumeric': 0.12633534602879704, 'fraction_numerical': 0.000557361820715281, 'mean_word_length': 2.3919344675488343, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4996926', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2796, 'n_words': 914}