mirror of
https://github.com/danog/PHP-Parser.git
synced 2025-01-22 13:51:12 +01:00
Add usage example for builders to docs
This commit is contained in:
parent
b8b68a969c
commit
72586235c4
59
doc/3_Code_generation.markdown
Normal file
59
doc/3_Code_generation.markdown
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
Code generation
|
||||||
|
===============
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
Builders
|
||||||
|
--------
|
||||||
|
|
||||||
|
The project provides builders for classes, methods, functions, parameters and properties, which
|
||||||
|
allow creating node trees with a fluid interface, instead of instantiating all nodes manually.
|
||||||
|
|
||||||
|
Here is an example:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
$factory = new PHPParser_BuilderFactory;
|
||||||
|
$node = $factory->class('SomeClass')
|
||||||
|
->extend('SomeOtherClass')
|
||||||
|
->implement('A\Few', 'Interfaces')
|
||||||
|
->makeAbstract() // ->makeFinal()
|
||||||
|
|
||||||
|
->addStmt($factory->method('someMethod')
|
||||||
|
->makeAbstract() // ->makeFinal()
|
||||||
|
->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
||||||
|
)
|
||||||
|
|
||||||
|
->addStmt($factory->method('anotherMethod')
|
||||||
|
->makeProtected() // ->makePublic() [default], ->makePrivate()
|
||||||
|
->addParam($factory->param('someParam')->setDefault('test'))
|
||||||
|
// it is possible to add manually created nodes
|
||||||
|
->addStmt(new PHPParser_Node_Expr_Print(new PHPParser_Node_Expr_Variable('someParam')))
|
||||||
|
)
|
||||||
|
|
||||||
|
// properties will be correctly reordered above the methods
|
||||||
|
->addStmt($factory->property('someProperty')->makeProtected())
|
||||||
|
->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
|
||||||
|
|
||||||
|
->getNode()
|
||||||
|
;
|
||||||
|
|
||||||
|
$stmts = array($node);
|
||||||
|
echo $prettyPrinter->prettyPrint($stmts);
|
||||||
|
```
|
||||||
|
|
||||||
|
This will produce the following output with the default pretty printer:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
abstract class SomeClass extends SomeOtherClass implements A\Few, Interfaces
|
||||||
|
{
|
||||||
|
protected $someProperty;
|
||||||
|
private $anotherProperty = array(1, 2, 3);
|
||||||
|
abstract function someMethod(SomeClass $someParam);
|
||||||
|
protected function anotherMethod($someParam = 'test')
|
||||||
|
{
|
||||||
|
print $someParam;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user