Total Complexity | 11 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
5 | class TempFile { |
||
6 | |||
7 | private $id; |
||
8 | private $full_path; |
||
9 | |||
10 | public function __construct(string $id, string $path) { |
||
11 | $this->id = $id; |
||
12 | $this->full_path = $path; |
||
13 | } |
||
14 | |||
15 | public function getId() : string { |
||
17 | } |
||
18 | |||
19 | public function getPath() : string { |
||
21 | } |
||
22 | |||
23 | public function __toString() : string { |
||
24 | return $this->full_path; |
||
25 | } |
||
26 | |||
27 | public function getText() : string { |
||
28 | return file_get_contents($this->full_path); |
||
29 | } |
||
30 | |||
31 | public function getLastLines(int $lines = 10, $buffer = 4096) : string { |
||
32 | $f = fopen($this->full_path, "rb"); |
||
33 | fseek($f, -1, SEEK_END); |
||
|
|||
34 | if(fread($f, 1) != "\n") { |
||
35 | $lines -= 1; |
||
36 | } |
||
37 | $output = ''; |
||
38 | $chunk = ''; |
||
39 | |||
40 | while(ftell($f) > 0 && $lines >= 0) { |
||
41 | $seek = min(ftell($f), $buffer); |
||
42 | fseek($f, -$seek, SEEK_CUR); |
||
43 | $output = ($chunk = fread($f, $seek)).$output; |
||
44 | fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR); |
||
45 | $lines -= substr_count($chunk, "\n"); |
||
46 | } |
||
47 | |||
48 | while($lines++ < 0) { |
||
49 | $output = substr($output, strpos($output, "\n") + 1); |
||
50 | } |
||
51 | |||
52 | fclose($f); |
||
53 | return $output; |
||
54 | } |
||
55 | |||
56 | public function delete() { |
||
58 | } |
||
59 | |||
60 | } |