File tree 5 files changed +33
-0
lines changed
5 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 9
9
10
10
namespace Nette \PhpGenerator ;
11
11
12
+ use JetBrains \PhpStorm \Language ;
13
+
12
14
13
15
/**
14
16
* Definition of a PHP file.
@@ -25,6 +27,7 @@ final class PhpFile
25
27
/** @var PhpNamespace[] */
26
28
private array $ namespaces = [];
27
29
private bool $ strictTypes = false ;
30
+ private string $ body = '' ;
28
31
29
32
30
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
+
113
134
/**
114
135
* Removes the namespace from the file.
115
136
*/
Original file line number Diff line number Diff line change @@ -287,6 +287,10 @@ public function printFile(PhpFile $file): string
287
287
}
288
288
}
289
289
290
+ if ($ tmp = $ file ->getBody ()) {
291
+ $ namespaces [] = $ tmp ;
292
+ }
293
+
290
294
return "<?php \n"
291
295
. ($ file ->getComment () ? "\n" . $ this ->printDocComment ($ file ) : '' )
292
296
. "\n"
Original file line number Diff line number Diff line change @@ -78,6 +78,8 @@ $file->addFunction('Baz\f2')
78
78
->setReturnType ('Foo\B ' );
79
79
80
80
81
+ $ file ->addBody ('// global contents ' );
82
+
81
83
sameFile (__DIR__ . '/expected/PhpFile.regular.expect ' , (string ) $ file );
82
84
83
85
$ file ->addClass ('H ' );
Original file line number Diff line number Diff line change @@ -83,3 +83,6 @@ namespace FooBar
83
83
{
84
84
}
85
85
}
86
+
87
+
88
+ // global contents
Original file line number Diff line number Diff line change @@ -59,3 +59,6 @@ trait G
59
59
function f2(): \Foo\B
60
60
{
61
61
}
62
+
63
+
64
+ // global contents
You can’t perform that action at this time.
0 commit comments