1 | <?php |
||
32 | class Log implements \ArrayAccess, \Countable, \Iterator |
||
33 | { |
||
34 | /** |
||
35 | * @var \GitElephant\Repository |
||
36 | */ |
||
37 | private $repository; |
||
38 | |||
39 | /** |
||
40 | * the commits related to this log |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $commits = array(); |
||
45 | |||
46 | /** |
||
47 | * the cursor position |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | private $position = 0; |
||
52 | |||
53 | /** |
||
54 | * static method to generate standalone log |
||
55 | * |
||
56 | * @param \GitElephant\Repository $repository repo |
||
57 | * @param array $outputLines output lines from command.log |
||
58 | * |
||
59 | * @return \GitElephant\Objects\Log |
||
60 | */ |
||
61 | 1 | public static function createFromOutputLines(Repository $repository, $outputLines) |
|
62 | { |
||
63 | 1 | $log = new self($repository); |
|
64 | 1 | $log->parseOutputLines($outputLines); |
|
65 | |||
66 | 1 | return $log; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Class constructor |
||
71 | * |
||
72 | * @param \GitElephant\Repository $repository repo |
||
73 | * @param string $ref treeish reference |
||
74 | * @param null $path path |
||
75 | * @param int $limit limit |
||
76 | * @param null $offset offset |
||
77 | * @param boolean $firstParent first parent |
||
78 | * |
||
79 | * @throws \RuntimeException |
||
80 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
81 | */ |
||
82 | 19 | public function __construct( |
|
83 | Repository $repository, |
||
84 | $ref = 'HEAD', |
||
85 | $path = null, |
||
86 | $limit = 15, |
||
87 | $offset = null, |
||
88 | $firstParent = false |
||
89 | ) { |
||
90 | 19 | $this->repository = $repository; |
|
91 | 19 | $this->createFromCommand($ref, $path, $limit, $offset, $firstParent); |
|
92 | 19 | } |
|
93 | |||
94 | /** |
||
95 | * get the commit properties from command |
||
96 | * |
||
97 | * @param string $ref treeish reference |
||
98 | * @param string $path path |
||
99 | * @param int $limit limit |
||
100 | * @param string $offset offset |
||
101 | * @param boolean $firstParent first parent |
||
102 | * |
||
103 | * @throws \RuntimeException |
||
104 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
105 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
106 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
107 | * @see ShowCommand::commitInfo |
||
108 | */ |
||
109 | 19 | private function createFromCommand($ref, $path, $limit, $offset, $firstParent) |
|
110 | { |
||
111 | 19 | $command = LogCommand::getInstance($this->getRepository())->showLog($ref, $path, $limit, $offset, $firstParent); |
|
112 | 19 | $outputLines = $this->getRepository()->getCaller()->execute($command)->getOutputLines(true); |
|
113 | 19 | $this->parseOutputLines($outputLines); |
|
114 | 19 | } |
|
115 | |||
116 | 19 | private function parseOutputLines($outputLines) |
|
117 | { |
||
118 | 19 | $this->commits = array(); |
|
119 | 19 | $commits = Utilities::pregSplitFlatArray($outputLines, '/^commit (\w+)$/'); |
|
120 | 19 | foreach ($commits as $commitOutputLines) { |
|
121 | 19 | $this->commits[] = Commit::createFromOutputLines($this->getRepository(), $commitOutputLines); |
|
122 | 19 | } |
|
123 | 19 | } |
|
124 | |||
125 | /** |
||
126 | * Get array representation |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 1 | public function toArray() |
|
134 | |||
135 | /** |
||
136 | * Get the first commit |
||
137 | * |
||
138 | * @return Commit|null |
||
139 | */ |
||
140 | public function first() |
||
144 | |||
145 | /** |
||
146 | * Get the last commit |
||
147 | * |
||
148 | * @return Commit|null |
||
149 | */ |
||
150 | 3 | public function last() |
|
154 | |||
155 | /** |
||
156 | * Get commit at index |
||
157 | * |
||
158 | * @param int $index the commit index |
||
159 | * |
||
160 | * @return Commit|null |
||
161 | */ |
||
162 | 1 | public function index($index) |
|
166 | |||
167 | /** |
||
168 | * ArrayAccess interface |
||
169 | * |
||
170 | * @param int $offset offset |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function offsetExists($offset) |
||
178 | |||
179 | /** |
||
180 | * ArrayAccess interface |
||
181 | * |
||
182 | * @param int $offset offset |
||
183 | * |
||
184 | * @return Commit|null |
||
185 | */ |
||
186 | 14 | public function offsetGet($offset) |
|
190 | |||
191 | /** |
||
192 | * ArrayAccess interface |
||
193 | * |
||
194 | * @param int $offset offset |
||
195 | * @param mixed $value value |
||
196 | * |
||
197 | * @throws \RuntimeException |
||
198 | */ |
||
199 | public function offsetSet($offset, $value) |
||
203 | |||
204 | /** |
||
205 | * ArrayAccess interface |
||
206 | * |
||
207 | * @param int $offset offset |
||
208 | * |
||
209 | * @throws \RuntimeException |
||
210 | */ |
||
211 | public function offsetUnset($offset) |
||
215 | |||
216 | /** |
||
217 | * Countable interface |
||
218 | * |
||
219 | * @return int|void |
||
220 | */ |
||
221 | 8 | public function count() |
|
225 | |||
226 | /** |
||
227 | * Iterator interface |
||
228 | * |
||
229 | * @return Commit|null |
||
230 | */ |
||
231 | public function current() |
||
235 | |||
236 | /** |
||
237 | * Iterator interface |
||
238 | */ |
||
239 | public function next() |
||
243 | |||
244 | /** |
||
245 | * Iterator interface |
||
246 | * |
||
247 | * @return int |
||
248 | */ |
||
249 | public function key() |
||
253 | |||
254 | /** |
||
255 | * Iterator interface |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function valid() |
||
263 | |||
264 | /** |
||
265 | * Iterator interface |
||
266 | */ |
||
267 | public function rewind() |
||
271 | |||
272 | /** |
||
273 | * Repository setter |
||
274 | * |
||
275 | * @param \GitElephant\Repository $repository the repository variable |
||
276 | */ |
||
277 | public function setRepository($repository) |
||
281 | |||
282 | /** |
||
283 | * Repository getter |
||
284 | * |
||
285 | * @return \GitElephant\Repository |
||
286 | */ |
||
287 | 19 | public function getRepository() |
|
291 | } |
||
292 |