aboutsummaryrefslogtreecommitdiffstats
path: root/library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php
blob: cb8bb625f91b2dd4a7645454e66a6e70dc694d54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

namespace PHPGit\Command;

use PHPGit\Command;
use PHPGit\Exception\GitException;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Get and set repository or global options - `git config`
 *
 * @author Kazuyuki Hayashi <hayashi@valnur.net>
 */
class ConfigCommand extends Command
{

    /**
     * Returns all variables set in config file
     *
     *
     * ##### Options
     *
     * - **global** (_boolean_) Read or write configuration options for the current user
     * - **system** (_boolean_) Read or write configuration options for all users on the current machine
     *
     * @param array $options [optional] An array of options {@see ConfigCommand::setDefaultOptions}
     *
     * @throws GitException
     * @return array
     */
    public function __invoke(array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('config')
            ->add('--list')
            ->add('--null');

        $this->addFlags($builder, $options, array('global', 'system'));

        $config = array();
        $output = $this->git->run($builder->getProcess());
        $lines  = $this->split($output, true);

        foreach ($lines as $line) {
            list($name, $value) = explode("\n", $line, 2);

            if (isset($config[$name])) {
                $config[$name] .= "\n" . $value;
            } else {
                $config[$name] = $value;
            }
        }

        return $config;
    }

    /**
     * Set an option
     *
     * ##### Options
     *
     * - **global** (_boolean_) Read or write configuration options for the current user
     * - **system** (_boolean_) Read or write configuration options for all users on the current machine
     *
     * @param string $name    The name of the option
     * @param string $value   The value to set
     * @param array  $options [optional] An array of options {@see ConfigCommand::setDefaultOptions}
     *
     * @throws GitException
     * @return bool
     */
    public function set($name, $value, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('config');

        $this->addFlags($builder, $options, array('global', 'system'));

        $builder->add($name)->add($value);
        $process = $builder->getProcess();
        $this->git->run($process);

        return true;
    }

    /**
     * Adds a new line to the option without altering any existing values
     *
     * ##### Options
     *
     * - **global** (_boolean_) Read or write configuration options for the current user
     * - **system** (_boolean_) Read or write configuration options for all users on the current machine
     *
     * @param string $name    The name of the option
     * @param string $value   The value to add
     * @param array  $options [optional] An array of options {@see ConfigCommand::setDefaultOptions}
     *
     * @throws GitException
     * @return bool
     */
    public function add($name, $value, array $options = array())
    {
        $options = $this->resolve($options);
        $builder = $this->git->getProcessBuilder()
            ->add('config');

        $this->addFlags($builder, $options, array('global', 'system'));

        $builder->add('--add')->add($name)->add($value);
        $process = $builder->getProcess();
        $this->git->run($process);

        return true;
    }

    /**
     * {@inheritdoc}
     *
     * - **global** (_boolean_) Read or write configuration options for the current user
     * - **system** (_boolean_) Read or write configuration options for all users on the current machine
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'global' => false,
            'system' => false,
        ));
    }

}