-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeStore.php
More file actions
361 lines (322 loc) · 9.45 KB
/
CodeStore.php
File metadata and controls
361 lines (322 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
declare(strict_types=1);
namespace SetBased\Helper\CodeStore;
/**
* An abstract helper class for automatically generating code.
*/
abstract class CodeStore
{
//--------------------------------------------------------------------------------------------------------------------
/**
* Increment indentation before appending the line.
*
* @since 1.0.0
* @api
*/
const int C_INDENT_INCREMENT_BEFORE = 1;
/**
* Increment indentation after appending the line.
*
* @since 1.0.0
* @api
*/
const int C_INDENT_INCREMENT_AFTER = 2;
/**
* Decrement indentation before appending the line.
*
* @since 1.0.0
* @api
*/
const int C_INDENT_DECREMENT_BEFORE = 4;
/**
* Decrement indentation twice before appending the line.
*
* @since 2.1.0
* @api
*/
const int C_INDENT_DECREMENT_BEFORE_DOUBLE = 16;
/**
* Decrement indentation after appending the line.
*
* @since 1.0.0
* @api
*/
const int C_INDENT_DECREMENT_AFTER = 8;
/**
* No indentation, heredoc.
*
* @since 1.0.0
* @api
*/
const int C_INDENT_HEREDOC = 32;
/**
* String for separating parts of the generated code. In most cases a comment with one character repeated many times.
*
* @var string|null
*
* @since 1.0.0
* @api
*/
protected ?string $separator = null;
/**
* The number of spaces per indentation level.
*
* @var int
*/
private int $indentation;
/**
* The source code. Each element is a line.
*
* @var string[]
*/
private array $lines;
/**
* The maximum width of the generated code (in chars).
*
* @var int
*/
private int $width;
//--------------------------------------------------------------------------------------------------------------------
/**
* Object constructor.
*
* @param int $indentation The number of spaces per indentation level.
* @param int $width The maximum width of the generated code (in chars).
*
* @since 1.0.0
* @api
*/
public function __construct(int $indentation = 2, int $width = 120)
{
$this->indentation = $indentation;
$this->lines = [];
$this->width = $width;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Appends a line or lines of code.
*
* @param null|string|string[] $line The line or lines of code to be appended. Null values will be ignored.
* @param bool $trim If true the line or lines of code will be trimmed before appending.
*
* @throws \InvalidArgumentException
*
* @since 1.0.0
* @api
*/
public function append(mixed $line, bool $trim = true): void
{
switch (true)
{
case is_string($line):
$this->appendLine($line, $trim);
break;
case is_array($line):
$this->appendLines($line, $trim);
break;
case is_null($line):
// Nothing to do.
break;
default:
throw new \InvalidArgumentException('Not a string nor an array.');
}
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Appends the separator to the generated code.
*
* @since 1.0.0
* @api
*/
public function appendSeparator(): void
{
$this->append($this->separator, false);
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Appends a part of code to the last line of code.
*
* @param string $part The part of code to be to the last line.
*
* @since 1.0.0
* @api
*/
public function appendToLastLine(string $part): void
{
$this->lines[count($this->lines) - 1] .= $part;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Removes all code from this code store.
*
* @since 1.0.0
* @api
*/
public function clear(): void
{
$this->lines = [];
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Returns the generated code properly indented as a single string.
*
* @return string
*
* @since 1.0.0
* @api
*/
public function getCode(): string
{
$lines = [];
$indentLevel = 0;
foreach ($this->lines as $line)
{
$mode = $this->indentationMode($line);
// Increment or decrement indentation level.
if ($mode & self::C_INDENT_INCREMENT_BEFORE)
{
$indentLevel++;
}
if ($mode & self::C_INDENT_DECREMENT_BEFORE)
{
$indentLevel = max(0, $indentLevel - 1);
}
if ($mode & self::C_INDENT_DECREMENT_BEFORE_DOUBLE)
{
$indentLevel = max(0, $indentLevel - 2);
}
// If the line is a separator shorten the separator.
if ($this->separator!==null && $line==$this->separator)
{
$line = $this->shortenSeparator($this->width - $this->indentation * $indentLevel);
}
if ($mode & self::C_INDENT_HEREDOC)
{
$lines[] = $line;
}
else
{
// Append the line with indentation.
$lines[] = $this->addIndentation($line, $indentLevel);
}
// Increment or decrement indentation level.
if ($mode & self::C_INDENT_INCREMENT_AFTER)
{
$indentLevel++;
}
if ($mode & self::C_INDENT_DECREMENT_AFTER)
{
$indentLevel = max(0, $indentLevel - 1);
}
}
if (empty($lines))
{
return '';
}
return implode(PHP_EOL, $lines).PHP_EOL;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Returns the last line of code (without indentation applied).
*
* @return string
*
* @since 1.1.0
* @api
*/
public function getLastLine(): string
{
if (empty($this->lines))
{
throw new \LogicException('No code in code store');
}
return $this->lines[count($this->lines) - 1];
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Returns the raw code without indentation as an array of strings.
*
* @return string[]
*
* @since 1.0.0
* @api
*/
public function getRawCode(): array
{
return $this->lines;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Returns the indentation mode based on a line of code.
*
* The indentation mode can be any combination of the following flags (combined with the | bitwise operator).
* <ul>
* <li> self::C_INDENT_INCREMENT_BEFORE: The indentation must be incremented before appending the line of code.
* <li> self::C_INDENT_INCREMENT_AFTER: The indentation must be incremented after appending the line of code.
* <li> self::C_INDENT_DECREMENT_BEFORE: The indentation must be decremented before appending the line of code.
* <li> self::C_INDENT_DECREMENT_AFTER: The indentation must be decremented after appending the line of code.
* </ul>
*
* @param string $line The line of code.
*
* @return int
*
* @since 1.0.0
* @api
*/
abstract protected function indentationMode(string $line): int;
//--------------------------------------------------------------------------------------------------------------------
/**
* Returns the separator to a required length.
*
* @param int $length The required length of the separator.
*
* @return string
*/
protected function shortenSeparator(int $length): string
{
return substr($this->separator, 0, $length);
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Returns a line of code with the proper amount of indentationMode.
*
* @param string $line The line of code.
* @param int $indentLevel The indentation level.
*
* @return string The indented line of code.
*/
private function addIndentation(string $line, int $indentLevel): string
{
return ($line==='') ? '' : str_repeat(' ', $this->indentation * $indentLevel).$line;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Appends a line of code to this code.
*
* @param null|string $line The line of code to append. If null the line will be ignored.
* @param bool $trim If true the line of code will be trimmed before appending.
*/
private function appendLine(?string $line, bool $trim): void
{
if ($line===null) return;
if ($trim) $line = trim($line);
$this->lines[] = $line;
}
//--------------------------------------------------------------------------------------------------------------------
/**
* Appends an array with lines of code to this code.
*
* @param string[] $lines The lines of code to append.
* @param bool $trim If true the lines of code will be trimmed before appending.
*/
private function appendLines(array $lines, bool $trim): void
{
foreach ($lines as $line)
{
$this->appendLine($line, $trim);
}
}
//--------------------------------------------------------------------------------------------------------------------
}
//----------------------------------------------------------------------------------------------------------------------