We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4280604 commit 56e0ce3Copy full SHA for 56e0ce3
src/PhpGenerator/PhpFile.php
@@ -9,6 +9,8 @@
9
10
namespace Nette\PhpGenerator;
11
12
+use JetBrains\PhpStorm\Language;
13
+
14
15
/**
16
* Definition of a PHP file.
@@ -25,6 +27,7 @@ final class PhpFile
25
27
/** @var PhpNamespace[] */
26
28
private array $namespaces = [];
29
private bool $strictTypes = false;
30
+ private string $body = '';
31
32
33
public static function fromCode(string $code): self
@@ -110,6 +113,24 @@ public function addNamespace(string|PhpNamespace $namespace): PhpNamespace
110
113
}
111
114
112
115
116
+ /** @param ?mixed[] $args */
117
+ public function addBody(
118
+ #[Language('PHP')]
119
+ string $code,
120
+ ?array $args = null,
121
+ ): static
122
+ {
123
+ $this->body .= ($args === null ? $code : (new Dumper)->format($code, ...$args)) . "\n";
124
+ return $this;
125
+ }
126
127
128
+ public function getBody(): string
129
130
+ return $this->body;
131
132
133
134
135
* Removes the namespace from the file.
136
*/
src/PhpGenerator/Printer.php
@@ -287,6 +287,10 @@ public function printFile(PhpFile $file): string
287
288
289
290
+ if ($tmp = $file->getBody()) {
291
+ $namespaces[] = $tmp;
292
293
294
return "<?php\n"
295
. ($file->getComment() ? "\n" . $this->printDocComment($file) : '')
296
. "\n"
tests/PhpGenerator/PhpFile.phpt
@@ -78,6 +78,8 @@ $file->addFunction('Baz\f2')
78
->setReturnType('Foo\B');
79
80
81
+$file->addBody('// global contents');
82
83
sameFile(__DIR__ . '/expected/PhpFile.regular.expect', (string) $file);
84
85
$file->addClass('H');
tests/PhpGenerator/expected/PhpFile.bracketed.expect
@@ -83,3 +83,6 @@ namespace FooBar
{
86
87
88
+// global contents
tests/PhpGenerator/expected/PhpFile.regular.expect
@@ -59,3 +59,6 @@ trait G
59
function f2(): \Foo\B
60
61
62
63
64
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments