Skip to content

Commit 9afcdaf

Browse files
committed
updated readme.md
1 parent 5cbe10c commit 9afcdaf

File tree

1 file changed

+79
-43
lines changed

1 file changed

+79
-43
lines changed

readme.md

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Requirements
2525
Examples
2626
--------
2727

28+
[See documentation](https://doc.nette.org/en/php-generator).
29+
30+
Let's start with a straightforward example of generating class:
31+
2832
```php
2933
$class = new Nette\PhpGenerator\ClassType('Demo');
3034

@@ -37,28 +41,7 @@ $class
3741
->addComment("Description of class.\nSecond line\n")
3842
->addComment('@property-read Nette\Forms\Form $form');
3943

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:
6245
echo $class;
6346
```
6447

@@ -74,12 +57,47 @@ It will render this result:
7457
abstract final class Demo extends ParentClass implements Countable
7558
{
7659
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+
```
7773

74+
It generates:
75+
76+
```php
7877
const ID = 123;
7978

8079
/** @var int[] */
8180
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:
8299

100+
```php
83101
/**
84102
* Count it.
85103
* @return int
@@ -88,11 +106,9 @@ abstract final class Demo extends ParentClass implements Countable
88106
{
89107
return count($items ?: $this->items);
90108
}
91-
92-
}
93109
```
94110

95-
PHP Generator supports all new PHP 7.1 features:
111+
PHP Generator supports all new PHP 7.2 features:
96112

97113
```php
98114
$class = new Nette\PhpGenerator\ClassType('Demo');
@@ -123,14 +139,13 @@ class Demo
123139
{
124140
return count($this->items);
125141
}
126-
127142
}
128143
```
129144

130145
Literals
131146
--------
132147

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`:
134149

135150
```php
136151
use Nette\PhpGenerator\PhpLiteral;
@@ -155,20 +170,19 @@ class Demo
155170
public function bar($id = 1 + 2)
156171
{
157172
}
158-
159173
}
160174
```
161175

162-
Interface or trait
176+
Interface or Trait
163177
------------------
164178

165179
```php
166180
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
167181
$class->setType('interface');
168-
$class->setType('trait'); // or trait
182+
// or $class->setType('trait');
169183
```
170184

171-
Trait resolutions and visibility
185+
Trait Resolutions and Visibility
172186
--------------------------------
173187

174188
```php
@@ -188,7 +202,7 @@ class Demo
188202
}
189203
```
190204

191-
Anonymous class
205+
Anonymous Class
192206
---------------
193207

194208
```php
@@ -210,9 +224,11 @@ $obj = new class ($val) {
210224
};
211225
```
212226

213-
Global function
227+
Global Function
214228
---------------
215229

230+
Code of function:
231+
216232
```php
217233
$function = new Nette\PhpGenerator\GlobalFunction('foo');
218234
$function->setBody('return $a + $b;');
@@ -233,6 +249,8 @@ function foo($a, $b)
233249
Closure
234250
-------
235251

252+
Code of closure:
253+
236254
```php
237255
$closure = new Nette\PhpGenerator\Closure;
238256
$closure->setBody('return $a + $b;');
@@ -251,8 +269,8 @@ function ($a, $b) use (&$c) {
251269
}
252270
```
253271

254-
Method body generator
255-
---------------------
272+
Method and Function Body Generator
273+
----------------------------------
256274

257275
You can use special placeholders for handy way to generate method or function body.
258276

@@ -262,8 +280,7 @@ Simple placeholders:
262280
$str = 'any string';
263281
$num = 3;
264282
$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]);
267284
echo $function;
268285
```
269286

@@ -272,8 +289,7 @@ Result:
272289
```php
273290
function foo()
274291
{
275-
$a = strlen('any string', 3);
276-
return $a ? 10 : 3;
292+
return strlen('any string', 3);
277293
}
278294
```
279295

@@ -295,10 +311,30 @@ function foo()
295311
}
296312
```
297313

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+
```
298332

299333
Namespace
300334
---------
301335

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+
302338
```php
303339
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
304340
$namespace->addUse('Bar\AliasedClass');
@@ -328,14 +364,14 @@ class Demo implements A
328364
public function method(\Bar\OtherClass $arg)
329365
{
330366
}
331-
332367
}
333368
```
334369

335-
Factories
336-
---------
337370

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:
339375

340376
```php
341377
$class = Nette\PhpGenerator\ClassType::from(PDO::class);

0 commit comments

Comments
 (0)