|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use function dirname; |
|
8
|
|
|
use function file_put_contents; |
|
9
|
|
|
use function implode; |
|
10
|
|
|
use function ksort; |
|
11
|
|
|
use function mkdir; |
|
12
|
|
|
|
|
13
|
|
|
class PhpFile extends DependencyAwareGenerator |
|
14
|
|
|
{ |
|
15
|
|
|
protected string $namespace = ''; |
|
16
|
|
|
protected string $name; |
|
17
|
|
|
protected ?Comment $comment; |
|
18
|
|
|
|
|
19
|
|
|
/** @var PhpClass[] */ |
|
20
|
|
|
protected array $classes = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string[] */ |
|
23
|
|
|
protected array $declares = []; |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(string $name = '') |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->name = $name; |
|
28
|
1 |
|
$this->dependencyAwareChildren = [&$this->classes]; |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public static function new(string $name = ''): self |
|
32
|
|
|
{ |
|
33
|
1 |
|
return new self($name); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function generate(): string |
|
37
|
|
|
{ |
|
38
|
3 |
|
$namespace = $this->namespace ? "\nnamespace $this->namespace;\n" : ''; |
|
39
|
3 |
|
$classes = implode("\n\n", $this->classes); |
|
40
|
|
|
|
|
41
|
|
|
return <<<CODE |
|
42
|
3 |
|
<?php |
|
43
|
3 |
|
$namespace{$this->buildUseStatements()} |
|
44
|
3 |
|
$classes |
|
45
|
|
|
CODE; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
public function __toString(): string |
|
49
|
|
|
{ |
|
50
|
3 |
|
return $this->generate(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function addClass(PhpClass $class): self |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->classes[] = $class; |
|
56
|
|
|
|
|
57
|
1 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function createClass(string $name): PhpClass |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->classes[] = PhpClass::new($name); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function removeClass(string $name): self |
|
66
|
|
|
{ |
|
67
|
1 |
|
foreach ($this->classes as $key => $class) { |
|
68
|
1 |
|
if ($class->name === $name) { |
|
69
|
1 |
|
unset($this->classes[$key]); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function getClass(string $name): ?PhpClass |
|
77
|
|
|
{ |
|
78
|
2 |
|
foreach ($this->classes as $key => $class) { |
|
79
|
2 |
|
if ($class->name === $name) { |
|
80
|
1 |
|
return $this->classes[$key]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
public function getNamespace(): string |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->namespace; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function setNamespace(string $namespace): self |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->namespace = $namespace; |
|
95
|
|
|
|
|
96
|
1 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
private function buildUseStatements(): string |
|
100
|
|
|
{ |
|
101
|
3 |
|
$code = ''; |
|
102
|
|
|
|
|
103
|
3 |
|
$paths = $this->getUsePaths(); |
|
104
|
|
|
|
|
105
|
3 |
|
if (empty($paths)) { |
|
106
|
2 |
|
return $code; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
if (!empty(ksort($paths))) { |
|
110
|
1 |
|
$code = "\n"; |
|
111
|
|
|
|
|
112
|
1 |
|
foreach ($paths as $path => $aliases) { |
|
113
|
1 |
|
$code .= "use $path"; |
|
114
|
|
|
|
|
115
|
1 |
|
if ($aliases) { |
|
116
|
1 |
|
$code .= " as $aliases"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
$code .= ";\n"; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return $code; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function getComment(): ?Comment |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->comment; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
public function createComment(string $text): Comment |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->comment = Comment::block($text); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
public function setComment(string $text): self |
|
137
|
|
|
{ |
|
138
|
2 |
|
$this->comment = Comment::block($text); |
|
139
|
|
|
|
|
140
|
2 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
public function removeComment(): self |
|
144
|
|
|
{ |
|
145
|
1 |
|
$this->comment = null; |
|
146
|
|
|
|
|
147
|
1 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return false|int |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function save(string $path, int $mask = 0775) |
|
154
|
|
|
{ |
|
155
|
1 |
|
$dir = dirname($path); |
|
156
|
|
|
|
|
157
|
1 |
|
if (!is_dir($dir)) { |
|
158
|
1 |
|
mkdir($dir, $mask, true); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return file_put_contents($path, $this); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|