Debian Codegraph @

util/docscripts/lib/generator/file/Serializer.php

raw symbols SCIP
1 <?php
2
3 require_once('lib/generator/common/AbstractSerializer.php');
4
5 abstract class Serializer extends AbstractSerializer
6 {
7 private $file_location;
8
9 private $file;
10 private $length = 9999;
11
12 private $queue;
13 private $limit = 50;
14
15 public static function clean($directory, $suffix, $filename='api') {
16 $loc = $directory . '/' . $filename . '.' . $suffix;
17 file_exists($loc) && unlink($loc);
18 }
19
20 public function __construct($directory, $suffix, $filename='api') {
21 $this->queue = array();
22 $this->file_location = $directory . '/' . $filename . '.' . $suffix;
23 touch($this->file_location);
24 $this->file = fopen($this->file_location, 'r');
25 }
26
27 public function __destruct() {
28 $this->flush();
29
30 fclose($this->file);
31 }
32
33 public function ids($flush = TRUE) {
34 if ($flush) {
35 $this->flush();
36 }
37
38 $ids = array();
39 $started = false;
40
41 rewind($this->file);
42 while (!feof($this->file)) {
43 $line = stream_get_line($this->file, $this->length, "\n");
44 if ($started && $this->lineEnds($line)) {
45 $started = false;
46 continue;
47 }
48 elseif ($id = $this->lineStarts($line)) {
49 $started = true;
50 $ids[] = $id;
51 }
52 }
53
54 return $ids;
55 }
56
57 protected function getString($id, $include_queue = TRUE) {
58 if ($include_queue) {
59 foreach (array_reverse($this->queue) as $queue) {
60 list($queue_id, $value) = $queue;
61 if ($queue_id == $id) {
62 return $this->toString($value, $id);
63 break;
64 }
65 }
66 }
67
68 $lines = array();
69 $started = false;
70 $strlen = strlen($this->indent);
71
72 rewind($this->file);
73 while (!feof($this->file)) {
74 $line = stream_get_line($this->file, $this->length, "\n");
75 if ($started) {
76 $lines[] = substr($line, $strlen);
77 if ($this->lineEnds($line)) {
78 return implode("\n", $lines) . "\n";
79 }
80 }
81 elseif ($this->lineStarts($line) == $id) {
82 $started = true;
83 $lines[] = substr($line, $strlen);
84 }
85 }
86 }
87
88 public function set($id, $value) {
89 $this->queue[] = array($id, $value);
90 if (count($this->queue) > $this->limit) {
91 $this->flush();
92 }
93 }
94
95 public function flush() {
96 $deferred = array();
97 $ids = $this->ids(FALSE);
98
99 foreach ($this->queue as $position => $queue) {
100 list($id, $value) = $queue;
101 $last = ($position + 1 == count($this->queue));
102 $tostring = $this->toString($value, $id);
103 if (!in_array($id, $ids)) {
104 if (!$last) {
105 $deferred[$id] = $tostring;
106 continue;
107 }
108 }
109
110 if (!$id) {
111 debug_print_backtrace();
112 die("Called set without an ID\n");
113 }
114
115 if ($tostring == $this->getString($id, FALSE)) {
116 continue;
117 }
118
119 $lines = array();
120 $started = false;
121 $found = false;
122 $header = false;
123 $buffer = array();
124
125 $tmp = fopen($this->file_location . '_tmp', 'w');
126 foreach ($this->header as $header_line) {
127 fwrite($tmp, $header_line . "\n");
128 }
129
130 rewind($this->file);
131 while (!feof($this->file)) {
132 $line = stream_get_line($this->file, $this->length, "\n");
133 if (!trim($line)) {
134 continue;
135 }
136
137 if ($started) {
138 $lines[] = $line;
139 if ($this->lineEnds($line)) {
140 $lines = explode("\n", $tostring);
141 foreach ($lines as $line) {
142 fwrite($tmp, $this->indent . $line . "\n");
143 }
144 $started = false;
145 $found = true;
146 }
147 }
148 elseif (!$found && $this->lineStarts($line) == $id) {
149 foreach ($buffer as $line) {
150 fwrite($tmp, $line . "\n");
151 }
152 $buffer = array();
153 $started = true;
154 $lines[] = $line;
155 }
156 else {
157 // Search through non-block data for headers first, then footers
158 if (!isset($searching)) {
159 $searching = $this->header;
160 }
161
162 $buffer[] = $line;
163 if (count($buffer) == count($searching) && count(array_intersect($buffer, $searching)) == count($searching)) {
164 // Successful match
165 if ($searching === $this->header) {
166 $buffer = array();
167 $searching = $this->footer;
168 }
169 else {
170 // Break before the footer is added
171 break;
172 }
173 }
174 elseif(count($buffer) > count($searching)) {
175 fwrite($tmp, array_shift($buffer) . "\n");
176 }
177 }
178 }
179
180 if (!$found) {
181 if ($last) {
182 foreach ($deferred as $lines) {
183 foreach (explode("\n", $lines) as $line) {
184 fwrite($tmp, $this->indent . $line . "\n");
185 }
186 }
187 }
188 $lines = explode("\n", $tostring);
189 foreach ($lines as $line) {
190 fwrite($tmp, $this->indent . $line . "\n");
191 }
192 }
193
194 foreach ($this->footer as $footer_line) {
195 fwrite($tmp, $footer_line . "\n");
196 }
197
198 fclose($tmp);
199 fclose($this->file);
200
201 unlink($this->file_location);
202 rename($this->file_location . '_tmp', $this->file_location);
203 $this->file = fopen($this->file_location, 'r');
204 }
205
206 $this->queue = array();
207 }
208 }