diff options
author | Wave <wave72@users.noreply.github.com> | 2016-07-22 10:55:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-22 10:55:02 +0200 |
commit | 744ad84714fe0f7a3d90250a4ff02dc4327b9061 (patch) | |
tree | 595fb74ec9ea0bc7130d18bd7993d719a222d343 /library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php | |
parent | c38c79d71c8ef70ef649f83e322f1984b75ee2dd (diff) | |
parent | 7d897a3f03bd57ed556433eb84a41963ba44e02e (diff) | |
download | volse-hubzilla-744ad84714fe0f7a3d90250a4ff02dc4327b9061.tar.gz volse-hubzilla-744ad84714fe0f7a3d90250a4ff02dc4327b9061.tar.bz2 volse-hubzilla-744ad84714fe0f7a3d90250a4ff02dc4327b9061.zip |
Merge pull request #6 from redmatrix/dev
Dev
Diffstat (limited to 'library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php')
-rw-r--r-- | library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php b/library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php new file mode 100644 index 000000000..cb8bb625f --- /dev/null +++ b/library/kzykhys/git/src/PHPGit/Command/ConfigCommand.php @@ -0,0 +1,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, + )); + } + +}
\ No newline at end of file |