|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Gitonomy. |
| 5 | + * |
| 6 | + * (c) Alexandre Salomé <[email protected]> |
| 7 | + * (c) Julien DIDIER <[email protected]> |
| 8 | + * |
| 9 | + * This source file is subject to the GPL license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Gitonomy\Git; |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * @author Alexandre Salomé <[email protected]> |
| 18 | + */ |
| 19 | +class RemoteBag implements \IteratorAggregate |
| 20 | +{ |
| 21 | + protected $repository; |
| 22 | + |
| 23 | + protected $remotes; |
| 24 | + |
| 25 | + public function __construct(Repository $repository) |
| 26 | + { |
| 27 | + $this->repository = $repository; |
| 28 | + } |
| 29 | + |
| 30 | + public function getAll() |
| 31 | + { |
| 32 | + if (null !== $this->remotes) { |
| 33 | + return $this->remotes; |
| 34 | + } |
| 35 | + |
| 36 | + $read = array(); |
| 37 | + $output = $this->repository->run('remote', array('-v')); |
| 38 | + $exp = explode("\n", $output); |
| 39 | + foreach ($exp as $i => $line) { |
| 40 | + if ($line == "") { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (!preg_match('/(?P<name>[^\s]+)\s(?P<url>[^\s]+)\s\((?P<type>\w+)\)$/', $line, $vars)) { |
| 45 | + throw new \RuntimeException(sprintf('Unexpected git remote output. Expected "name url (type)", got "%s" at line %s :'."\n".$output, $line, $i + 1)); |
| 46 | + } |
| 47 | + |
| 48 | + $name = $vars['name']; |
| 49 | + $type = $vars['type']; |
| 50 | + $url = $vars['url']; |
| 51 | + $read[$name][$type] = $url; |
| 52 | + } |
| 53 | + |
| 54 | + foreach ($read as $name => $urls) { |
| 55 | + $this->remotes[$name] = new Remote($this->repository, $name, $urls); |
| 56 | + } |
| 57 | + |
| 58 | + return $this->remotes; |
| 59 | + } |
| 60 | + |
| 61 | + public function fetch($name = null) |
| 62 | + { |
| 63 | + if (null === $name) { |
| 64 | + $args = array('--all'); |
| 65 | + } else { |
| 66 | + $args = array($name); |
| 67 | + } |
| 68 | + |
| 69 | + $this->repository->run('fetch', $args); |
| 70 | + |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + public function getIterator() |
| 75 | + { |
| 76 | + return new \ArrayIterator($this->getAll()); |
| 77 | + } |
| 78 | +} |
0 commit comments