diff options
author | Andrew Manning <tamanning@zoho.com> | 2016-05-01 22:29:51 -0400 |
---|---|---|
committer | Andrew Manning <tamanning@zoho.com> | 2016-05-01 22:29:51 -0400 |
commit | c2d15e6c3bd8a29bae89d184a999ddac15fcb807 (patch) | |
tree | 96cbbfa98f131d830fbfd154f82e37383bb37bd8 /library/kzykhys/git/src/PHPGit/Command.php | |
parent | b1ae4d776c7c093c7f3ff6905e1a5302fbd5e3f6 (diff) | |
download | volse-hubzilla-c2d15e6c3bd8a29bae89d184a999ddac15fcb807.tar.gz volse-hubzilla-c2d15e6c3bd8a29bae89d184a999ddac15fcb807.tar.bz2 volse-hubzilla-c2d15e6c3bd8a29bae89d184a999ddac15fcb807.zip |
New plugin repo is cloned to /store/pluginrepos/REPONAME for analysis
Diffstat (limited to 'library/kzykhys/git/src/PHPGit/Command.php')
-rw-r--r-- | library/kzykhys/git/src/PHPGit/Command.php | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/library/kzykhys/git/src/PHPGit/Command.php b/library/kzykhys/git/src/PHPGit/Command.php new file mode 100644 index 000000000..9238a5454 --- /dev/null +++ b/library/kzykhys/git/src/PHPGit/Command.php @@ -0,0 +1,123 @@ +<?php + +namespace PHPGit; + +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\Process\ProcessBuilder; + +/** + * Base class for git commands + * + * @author Kazuyuki Hayashi <hayashi@valnur.net> + */ +abstract class Command +{ + + /** + * @var Git + */ + protected $git; + + /** + * @param Git $git + */ + public function __construct(Git $git) + { + $this->git = $git; + } + + /** + * Returns the combination of the default and the passed options + * + * @param array $options An array of options + * + * @return array + */ + public function resolve(array $options = array()) + { + $resolver = new OptionsResolver(); + $this->setDefaultOptions($resolver); + + return $resolver->resolve($options); + } + + /** + * Sets the default options + * + * @param OptionsResolverInterface $resolver The resolver for the options + * + * @codeCoverageIgnore + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + } + + /** + * Split string by new line or null(\0) + * + * @param string $input The string to split + * @param bool $useNull True to split by new line, otherwise null + * + * @return array + */ + protected function split($input, $useNull = false) + { + if ($useNull) { + $pattern = '/\0/'; + } else { + $pattern = '/\r?\n/'; + } + + return preg_split($pattern, rtrim($input), -1, PREG_SPLIT_NO_EMPTY); + } + + /** + * Adds boolean options to command arguments + * + * @param ProcessBuilder $builder A ProcessBuilder object + * @param array $options An array of options + * @param array $optionNames The names of options to add + */ + protected function addFlags(ProcessBuilder $builder, array $options = array(), array $optionNames = null) + { + if ($optionNames) { + foreach ($optionNames as $name) { + if (isset($options[$name]) && is_bool($options[$name]) && $options[$name]) { + $builder->add('--' . $name); + } + } + } else { + foreach ($options as $name => $option) { + if ($option) { + $builder->add('--' . $name); + } + } + } + } + + /** + * Adds options with values to command arguments + * + * @param ProcessBuilder $builder A ProcessBuilder object + * @param array $options An array of options + * @param array $optionNames The names of options to add + */ + protected function addValues(ProcessBuilder $builder, array $options = array(), array $optionNames = null) + { + if ($optionNames) { + foreach ($optionNames as $name) { + if (isset($options[$name]) && $options[$name]) { + $builder->add('--' . $name . '=' . $options[$name]); + } + } + } else { + foreach ($options as $name => $option) { + if ($option) { + $builder->add('--' . $name . '=' . $option); + } + } + } + } + +}
\ No newline at end of file |