@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorFileChunkIterator
4 extends Phobject
5 implements Iterator {
6
7 private $chunks;
8 private $cursor;
9 private $begin;
10 private $end;
11 private $data;
12
13 public function __construct(array $chunks, $begin = null, $end = null) {
14 $chunks = msort($chunks, 'getByteStart');
15 $this->chunks = $chunks;
16
17 if ($begin !== null) {
18 foreach ($chunks as $key => $chunk) {
19 if ($chunk->getByteEnd() >= $begin) {
20 unset($chunks[$key]);
21 }
22 break;
23 }
24 $this->begin = $begin;
25 }
26
27 if ($end !== null) {
28 foreach ($chunks as $key => $chunk) {
29 if ($chunk->getByteStart() <= $end) {
30 unset($chunks[$key]);
31 }
32 }
33 $this->end = $end;
34 }
35 }
36
37 public function current() {
38 $chunk = head($this->chunks);
39 $data = $chunk->getDataFile()->loadFileData();
40
41 if ($this->end !== null) {
42 if ($chunk->getByteEnd() > $this->end) {
43 $data = substr($data, 0, ($this->end - $chunk->getByteStart()));
44 }
45 }
46
47 if ($this->begin !== null) {
48 if ($chunk->getByteStart() < $this->begin) {
49 $data = substr($data, ($this->begin - $chunk->getByteStart()));
50 }
51 }
52
53 return $data;
54 }
55
56 public function key() {
57 return head_key($this->chunks);
58 }
59
60 public function next() {
61 unset($this->chunks[$this->key()]);
62 }
63
64 public function rewind() {
65 return;
66 }
67
68 public function valid() {
69 return (count($this->chunks) > 0);
70 }
71
72}