@@ -25,6 +25,10 @@ Requirements
25
25
Examples
26
26
--------
27
27
28
+ [ See documentation] ( https://doc.nette.org/en/php-generator ) .
29
+
30
+ Let's start with a straightforward example of generating class:
31
+
28
32
``` php
29
33
$class = new Nette\PhpGenerator\ClassType('Demo');
30
34
@@ -37,28 +41,7 @@ $class
37
41
->addComment("Description of class.\nSecond line\n")
38
42
->addComment('@property-read Nette\Forms\Form $form');
39
43
40
- $class->addConstant('ID', 123);
41
-
42
- $class->addProperty('items', [1, 2, 3])
43
- ->setVisibility('private')
44
- ->setStatic()
45
- ->addComment('@var int[]');
46
-
47
- $method = $class->addMethod('count')
48
- ->addComment('Count it.')
49
- ->addComment('@return int')
50
- ->setFinal()
51
- ->setVisibility('protected')
52
- ->setBody('return count($items ?: $this->items);');
53
-
54
- $method->addParameter('items', []) // $items = []
55
- ->setReference() // & $items = []
56
- ->setTypeHint('array'); // array & $items = []
57
- ```
58
-
59
- To generate PHP code simply cast to string or use echo:
60
-
61
- ``` php
44
+ // to generate PHP code simply cast to string or use echo:
62
45
echo $class;
63
46
```
64
47
@@ -74,12 +57,47 @@ It will render this result:
74
57
abstract final class Demo extends ParentClass implements Countable
75
58
{
76
59
use Nette\SmartObject;
60
+ }
61
+ ```
62
+
63
+ We can add constants and properties:
64
+
65
+ ``` php
66
+ $class->addConstant('ID', 123);
67
+
68
+ $class->addProperty('items', [1, 2, 3])
69
+ ->setVisibility('private')
70
+ ->setStatic()
71
+ ->addComment('@var int[]');
72
+ ```
77
73
74
+ It generates:
75
+
76
+ ``` php
78
77
const ID = 123;
79
78
80
79
/** @var int[] */
81
80
private static $items = [1, 2, 3];
81
+ ```
82
+
83
+ And we can add methods with parameters:
84
+
85
+ ``` php
86
+ $method = $class->addMethod('count')
87
+ ->addComment('Count it.')
88
+ ->addComment('@return int')
89
+ ->setFinal()
90
+ ->setVisibility('protected')
91
+ ->setBody('return count($items ?: $this->items);');
92
+
93
+ $method->addParameter('items', []) // $items = []
94
+ ->setReference() // & $items = []
95
+ ->setTypeHint('array'); // array & $items = []
96
+ ```
97
+
98
+ It results in:
82
99
100
+ ``` php
83
101
/**
84
102
* Count it.
85
103
* @return int
@@ -88,11 +106,9 @@ abstract final class Demo extends ParentClass implements Countable
88
106
{
89
107
return count($items ?: $this->items);
90
108
}
91
-
92
- }
93
109
```
94
110
95
- PHP Generator supports all new PHP 7.1 features:
111
+ PHP Generator supports all new PHP 7.2 features:
96
112
97
113
``` php
98
114
$class = new Nette\PhpGenerator\ClassType('Demo');
@@ -123,14 +139,13 @@ class Demo
123
139
{
124
140
return count($this->items);
125
141
}
126
-
127
142
}
128
143
```
129
144
130
145
Literals
131
146
--------
132
147
133
- You can pass any PHP code to property or parameter default values via ` Nette\PhpGenerator\ PhpLiteral` :
148
+ You can pass any PHP code to property or parameter default values via ` PhpLiteral ` :
134
149
135
150
``` php
136
151
use Nette\PhpGenerator\PhpLiteral;
@@ -155,20 +170,19 @@ class Demo
155
170
public function bar($id = 1 + 2)
156
171
{
157
172
}
158
-
159
173
}
160
174
```
161
175
162
- Interface or trait
176
+ Interface or Trait
163
177
------------------
164
178
165
179
``` php
166
180
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
167
181
$class->setType('interface');
168
- $class->setType('trait'); // or trait
182
+ // or $class->setType('trait');
169
183
```
170
184
171
- Trait resolutions and visibility
185
+ Trait Resolutions and Visibility
172
186
--------------------------------
173
187
174
188
``` php
@@ -188,7 +202,7 @@ class Demo
188
202
}
189
203
```
190
204
191
- Anonymous class
205
+ Anonymous Class
192
206
---------------
193
207
194
208
``` php
@@ -210,9 +224,11 @@ $obj = new class ($val) {
210
224
};
211
225
```
212
226
213
- Global function
227
+ Global Function
214
228
---------------
215
229
230
+ Code of function:
231
+
216
232
``` php
217
233
$function = new Nette\PhpGenerator\GlobalFunction('foo');
218
234
$function->setBody('return $a + $b;');
@@ -233,6 +249,8 @@ function foo($a, $b)
233
249
Closure
234
250
-------
235
251
252
+ Code of closure:
253
+
236
254
``` php
237
255
$closure = new Nette\PhpGenerator\Closure;
238
256
$closure->setBody('return $a + $b;');
@@ -251,8 +269,8 @@ function ($a, $b) use (&$c) {
251
269
}
252
270
```
253
271
254
- Method body generator
255
- ---------------------
272
+ Method and Function Body Generator
273
+ ----------------------------------
256
274
257
275
You can use special placeholders for handy way to generate method or function body.
258
276
@@ -262,8 +280,7 @@ Simple placeholders:
262
280
$str = 'any string';
263
281
$num = 3;
264
282
$function = new Nette\PhpGenerator\GlobalFunction('foo');
265
- $function->addBody('$a = strlen(?, ?);', [$str, $num]);
266
- $function->addBody('return $a \? 10 : ?;', [$num]); // escaping
283
+ $function->addBody('return strlen(?, ?);', [$str, $num]);
267
284
echo $function;
268
285
```
269
286
@@ -272,8 +289,7 @@ Result:
272
289
``` php
273
290
function foo()
274
291
{
275
- $a = strlen('any string', 3);
276
- return $a ? 10 : 3;
292
+ return strlen('any string', 3);
277
293
}
278
294
```
279
295
@@ -295,10 +311,30 @@ function foo()
295
311
}
296
312
```
297
313
314
+ Escape placeholder using slash:
315
+
316
+ ``` php
317
+ $num = 3;
318
+ $function = new Nette\PhpGenerator\GlobalFunction('foo');
319
+ $function->addParameter('a');
320
+ $function->addBody('return $a \? 10 : ?;', [$num]);
321
+ echo $function;
322
+ ```
323
+
324
+ Result:
325
+
326
+ ``` php
327
+ function foo($a)
328
+ {
329
+ return $a ? 10 : 3;
330
+ }
331
+ ```
298
332
299
333
Namespace
300
334
---------
301
335
336
+ Namespaces may include ` use ` statements in addition to classes, interfaces, or traits. These statements are used when generating code, so use full class names in the definitions and they will be replace with aliases or fully qualified names in the resulting code.
337
+
302
338
``` php
303
339
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
304
340
$namespace->addUse('Bar\AliasedClass');
@@ -328,14 +364,14 @@ class Demo implements A
328
364
public function method(\Bar\OtherClass $arg)
329
365
{
330
366
}
331
-
332
367
}
333
368
```
334
369
335
- Factories
336
- ---------
337
370
338
- Another common use case is to create class or method form existing ones:
371
+ Generate using Reflection
372
+ -------------------------
373
+
374
+ Another common use case is to create class or method based on existing ones:
339
375
340
376
``` php
341
377
$class = Nette\PhpGenerator\ClassType::from(PDO::class);
0 commit comments