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