File tree Expand file tree Collapse file tree 7 files changed +77
-12
lines changed Expand file tree Collapse file tree 7 files changed +77
-12
lines changed Original file line number Diff line number Diff line change @@ -641,19 +641,20 @@ echo $printer->printNamespace($namespace);
641
641
PHP Files
642
642
---------
643
643
644
- Classes and namespaces can be grouped into PHP files represented by the class [ PhpFile] ( https://api.nette.org/php-generator/Nette/PhpGenerator/PhpFile.html ) :
644
+ Classes, functions and namespaces can be grouped into PHP files represented by the class [ PhpFile] ( https://api.nette.org/php-generator/Nette/PhpGenerator/PhpFile.html ) :
645
645
646
646
``` php
647
647
$file = new Nette\PhpGenerator\PhpFile;
648
648
$file->addComment('This file is auto-generated.');
649
649
$file->setStrictTypes(); // adds declare(strict_types=1)
650
650
651
- $namespace = $file->addNamespace('Foo');
652
- $class = $namespace->addClass('A');
653
- $class->addMethod('hello');
651
+ $class = $file->addClass('Foo\A');
652
+ $function = $file->addFunction('Foo\foo');
654
653
655
- // or insert an existing namespace into the file
656
- // $file->addNamespace(new Nette\PhpGenerator\PhpNamespace('Foo'));
654
+ // or
655
+ // $namespace = $file->addNamespace('Foo');
656
+ // $class = $namespace->addClass('A');
657
+ // $function = $namespace->addFunction('foo');
657
658
658
659
echo $file;
659
660
@@ -676,9 +677,10 @@ namespace Foo;
676
677
677
678
class A
678
679
{
679
- public function hello()
680
- {
681
- }
680
+ }
681
+
682
+ function foo()
683
+ {
682
684
}
683
685
```
684
686
Original file line number Diff line number Diff line change @@ -84,6 +84,14 @@ public function addNamespace($namespace): PhpNamespace
84
84
}
85
85
86
86
87
+ public function addFunction (string $ name ): GlobalFunction
88
+ {
89
+ return $ this
90
+ ->addNamespace (Helpers::extractNamespace ($ name ))
91
+ ->addFunction (Helpers::extractShortName ($ name ));
92
+ }
93
+
94
+
87
95
/** @return PhpNamespace[] */
88
96
public function getNamespaces (): array
89
97
{
@@ -105,6 +113,20 @@ public function getClasses(): array
105
113
}
106
114
107
115
116
+ /** @return GlobalFunction[] */
117
+ public function getFunctions (): array
118
+ {
119
+ $ functions = [];
120
+ foreach ($ this ->namespaces as $ n => $ namespace ) {
121
+ $ n .= $ n ? '\\' : '' ;
122
+ foreach ($ namespace ->getFunctions () as $ c => $ class ) {
123
+ $ functions [$ n . $ c ] = $ class ;
124
+ }
125
+ }
126
+ return $ functions ;
127
+ }
128
+
129
+
108
130
/** @return static */
109
131
public function addUse (string $ name , string $ alias = null ): self
110
132
{
Original file line number Diff line number Diff line change @@ -44,6 +44,9 @@ final class PhpNamespace
44
44
/** @var ClassType[] */
45
45
private $ classes = [];
46
46
47
+ /** @var GlobalFunction[] */
48
+ private $ functions = [];
49
+
47
50
48
51
public function __construct (string $ name )
49
52
{
@@ -195,13 +198,26 @@ public function addEnum(string $name): ClassType
195
198
}
196
199
197
200
201
+ public function addFunction (string $ name ): GlobalFunction
202
+ {
203
+ return $ this ->functions [$ name ] = new GlobalFunction ($ name );
204
+ }
205
+
206
+
198
207
/** @return ClassType[] */
199
208
public function getClasses (): array
200
209
{
201
210
return $ this ->classes ;
202
211
}
203
212
204
213
214
+ /** @return GlobalFunction[] */
215
+ public function getFunctions (): array
216
+ {
217
+ return $ this ->functions ;
218
+ }
219
+
220
+
205
221
public function __toString (): string
206
222
{
207
223
try {
Original file line number Diff line number Diff line change @@ -203,13 +203,16 @@ public function printNamespace(PhpNamespace $namespace): string
203
203
$ name = $ namespace ->getName ();
204
204
$ uses = $ this ->printUses ($ namespace );
205
205
206
- $ classes = [];
206
+ $ items = [];
207
207
foreach ($ namespace ->getClasses () as $ class ) {
208
- $ classes [] = $ this ->printClass ($ class , $ namespace );
208
+ $ items [] = $ this ->printClass ($ class , $ namespace );
209
+ }
210
+ foreach ($ namespace ->getFunctions () as $ function ) {
211
+ $ items [] = $ this ->printFunction ($ function , $ namespace );
209
212
}
210
213
211
214
$ body = ($ uses ? $ uses . "\n\n" : '' )
212
- . implode ("\n" , $ classes );
215
+ . implode ("\n" , $ items );
213
216
214
217
if ($ namespace ->hasBracketedSyntax ()) {
215
218
return 'namespace ' . ($ name ? " $ name " : '' ) . "\n{ \n"
Original file line number Diff line number Diff line change @@ -70,13 +70,19 @@ $interfaceF
70
70
$ traitG = $ file ->addTrait ('Baz\G ' );
71
71
Assert::same ($ file ->addNamespace ('Baz ' ), $ traitG ->getNamespace ());
72
72
73
+ $ file ->addFunction ('Baz \\f2 ' )
74
+ ->setReturnType ('Foo\B ' );
75
+
73
76
74
77
sameFile (__DIR__ . '/expected/PhpFile.regular.expect ' , (string ) $ file );
75
78
76
79
$ file ->addClass ('H ' );
77
80
78
81
$ file ->addClass ('FooBar\I ' );
79
82
83
+ $ file ->addFunction ('f1 ' )
84
+ ->setBody ('return 1; ' );
85
+
80
86
sameFile (__DIR__ . '/expected/PhpFile.bracketed.expect ' , (string ) $ file );
81
87
82
88
Assert::same ([
@@ -102,6 +108,9 @@ Assert::same([
102
108
'FooBar\I ' ,
103
109
], array_keys ($ file ->getClasses ()));
104
110
111
+ Assert::same (['Baz \\f2 ' , 'f1 ' ], array_keys ($ file ->getFunctions ()));
112
+
113
+
105
114
106
115
107
116
$ file = new PhpFile ;
Original file line number Diff line number Diff line change @@ -57,6 +57,10 @@ namespace Baz
57
57
trait G
58
58
{
59
59
}
60
+
61
+ function f2(): \Foo\B
62
+ {
63
+ }
60
64
}
61
65
62
66
@@ -65,6 +69,11 @@ namespace
65
69
class H
66
70
{
67
71
}
72
+
73
+ function f1()
74
+ {
75
+ return 1;
76
+ }
68
77
}
69
78
70
79
Original file line number Diff line number Diff line change @@ -55,3 +55,7 @@ interface F extends \Foo\B, \Bar\C
55
55
trait G
56
56
{
57
57
}
58
+
59
+ function f2(): \Foo\B
60
+ {
61
+ }
You can’t perform that action at this time.
0 commit comments