1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
6
|
|
|
|
7
|
|
|
use function array_map; |
8
|
|
|
use function array_unshift; |
9
|
|
|
use function join; |
10
|
|
|
|
11
|
|
|
trait ScopedContentTrait |
12
|
|
|
{ |
13
|
|
|
private array $content = []; |
14
|
|
|
private int $emptyLinesBuffer = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param GeneratorInterface|string ...$values |
18
|
|
|
*/ |
19
|
|
|
public function append(...$values): self |
20
|
|
|
{ |
21
|
|
|
if (end($values) instanceof BlockInterface) { |
22
|
|
|
$this->content[] = [...$values]; |
23
|
|
|
} else { |
24
|
|
|
$this->content[] = [...$values, ';']; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
foreach ($values as $value) { |
28
|
|
|
if ($value instanceof DependencyAwareGenerator) { |
29
|
|
|
$this->dependencyAwareChildren[] = $value; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param GeneratorInterface|string ...$values |
38
|
|
|
*/ |
39
|
|
|
public function prepend(...$values): self |
40
|
|
|
{ |
41
|
|
|
if (end($values) instanceof BlockInterface) { |
42
|
|
|
array_unshift($this->content, [...$values]); |
43
|
|
|
} else { |
44
|
|
|
array_unshift($this->content, [...$values, ';']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach ($values as $value) { |
48
|
|
|
if ($value instanceof DependencyAwareGenerator) { |
49
|
|
|
$this->dependencyAwareChildren[] = $value; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function emptyLine(): self |
57
|
|
|
{ |
58
|
|
|
$this->content[] = []; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function clearContent(): self |
64
|
|
|
{ |
65
|
|
|
$this->content = []; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function generateContent(): string |
71
|
|
|
{ |
72
|
|
|
$content = ''; |
73
|
|
|
|
74
|
|
|
if (!empty($this->content)) { |
75
|
|
|
$content = Utils::indent(join( |
76
|
|
|
"\n", |
77
|
|
|
array_map(fn($line) => join($line), $this->content) |
|
|
|
|
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return rtrim($content); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|