|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Gitter library. |
| 5 | + * |
| 6 | + * (c) Klaus Silveira <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Gitter; |
| 13 | + |
| 14 | +use Symfony\Component\Process\Process; |
| 15 | + |
| 16 | +class Client |
| 17 | +{ |
| 18 | + protected $path; |
| 19 | + protected $hidden; |
| 20 | + |
| 21 | + public function __construct($options = null) |
| 22 | + { |
| 23 | + $this->setPath((isset($options['path'])) ? $options['path'] : '/usr/bin/git'); |
| 24 | + $this->setHidden((isset($options['hidden'])) ? $options['hidden'] : array()); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates a new repository on the specified path |
| 29 | + * |
| 30 | + * @param string $path Path where the new repository will be created |
| 31 | + * @return Repository Instance of Repository |
| 32 | + */ |
| 33 | + public function createRepository($path, $bare = null) |
| 34 | + { |
| 35 | + if (file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) { |
| 36 | + throw new \RuntimeException('A GIT repository already exists at ' . $path); |
| 37 | + } |
| 38 | + |
| 39 | + $repository = new Repository($path, $this); |
| 40 | + |
| 41 | + return $repository->create($bare); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Opens a repository at the specified path |
| 46 | + * |
| 47 | + * @param string $path Path where the repository is located |
| 48 | + * @return Repository Instance of Repository |
| 49 | + */ |
| 50 | + public function getRepository($path) |
| 51 | + { |
| 52 | + if (!file_exists($path) || !file_exists($path . '/.git/HEAD') && !file_exists($path . '/HEAD')) { |
| 53 | + throw new \RuntimeException('There is no GIT repository at ' . $path); |
| 54 | + } |
| 55 | + |
| 56 | + if (in_array($path, $this->getHidden())) { |
| 57 | + throw new \RuntimeException('You don\'t have access to this repository'); |
| 58 | + } |
| 59 | + |
| 60 | + return new Repository($path, $this); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Searches for valid repositories on the specified path |
| 65 | + * |
| 66 | + * @param string $path Path where repositories will be searched |
| 67 | + * @return array Found repositories, containing their name, path and description |
| 68 | + */ |
| 69 | + public function getRepositories($path) |
| 70 | + { |
| 71 | + $repositories = $this->recurseDirectory($path); |
| 72 | + |
| 73 | + if (empty($repositories)) { |
| 74 | + throw new \RuntimeException('There are no GIT repositories in ' . $path); |
| 75 | + } |
| 76 | + |
| 77 | + sort($repositories); |
| 78 | + |
| 79 | + return $repositories; |
| 80 | + } |
| 81 | + |
| 82 | + private function recurseDirectory($path) |
| 83 | + { |
| 84 | + $dir = new \DirectoryIterator($path); |
| 85 | + |
| 86 | + $repositories = array(); |
| 87 | + |
| 88 | + foreach ($dir as $file) { |
| 89 | + if ($file->isDot()) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (strrpos($file->getFilename(), '.') === 0) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if ($file->isDir()) { |
| 98 | + $isBare = file_exists($file->getPathname() . '/HEAD'); |
| 99 | + $isRepository = file_exists($file->getPathname() . '/.git/HEAD'); |
| 100 | + |
| 101 | + if ($isRepository || $isBare) { |
| 102 | + if (in_array($file->getPathname(), $this->getHidden())) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if ($isBare) { |
| 107 | + $description = $file->getPathname() . '/description'; |
| 108 | + } else { |
| 109 | + $description = $file->getPathname() . '/.git/description'; |
| 110 | + } |
| 111 | + |
| 112 | + if (file_exists($description)) { |
| 113 | + $description = file_get_contents($description); |
| 114 | + } else { |
| 115 | + $description = 'There is no repository description file. Please, create one to remove this message.'; |
| 116 | + } |
| 117 | + |
| 118 | + $repositories[] = array('name' => $file->getFilename(), 'path' => $file->getPathname(), 'description' => $description); |
| 119 | + continue; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return $repositories; |
| 125 | + } |
| 126 | + |
| 127 | + public function run($repository, $command) |
| 128 | + { |
| 129 | + $process = new Process($this->getPath() . ' -c "color.ui"=false ' . $command, $repository->getPath()); |
| 130 | + $process->setTimeout(180); |
| 131 | + $process->run(); |
| 132 | + |
| 133 | + if (!$process->isSuccessful()) { |
| 134 | + throw new \RuntimeException($process->getErrorOutput()); |
| 135 | + } |
| 136 | + |
| 137 | + return $process->getOutput(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get the current Git binary path |
| 142 | + * |
| 143 | + * @return string Path where the Git binary is located |
| 144 | + */ |
| 145 | + protected function getPath() |
| 146 | + { |
| 147 | + return $this->path; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Set the current Git binary path |
| 152 | + * |
| 153 | + * @param string $path Path where the Git binary is located |
| 154 | + */ |
| 155 | + protected function setPath($path) |
| 156 | + { |
| 157 | + $this->path = $path; |
| 158 | + |
| 159 | + return $this; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Get hidden repository list |
| 164 | + * |
| 165 | + * @return array List of repositories to hide |
| 166 | + */ |
| 167 | + protected function getHidden() |
| 168 | + { |
| 169 | + return $this->hidden; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Set the hidden repository list |
| 174 | + * |
| 175 | + * @param array $hidden List of repositories to hide |
| 176 | + */ |
| 177 | + protected function setHidden($hidden) |
| 178 | + { |
| 179 | + $this->hidden = $hidden; |
| 180 | + |
| 181 | + return $this; |
| 182 | + } |
| 183 | +} |
0 commit comments