Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
Code generation
===============

It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the
pretty printer to convert it to PHP code. To simplify code generation, the project comes with a set of builders for
common structures as well as simple templating support. Both features are described in the following:

Builders
--------

The project provides builders for classes, interfaces, 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;
    }
}
```

Templates
---------

> **DEPRECATED**: This feature is deprecated and will be removed in PHP-Parser 1.0.

Additionally it is possible to generate code from reusable templates.

As an example consider the following template, which defines a general getter/setter skeleton in terms of a property
`__name__` and its `__type__`:

```php
<?php

class GetterSetterTemplate
{
    /**
     * @var __type__ The __name__
     */
    protected $__name__;

    /**
     * Gets the __name__.
     *
     * @return __type__ The __name__
     */
    public function get__Name__() {
        return $this->__name__;
    }

    /**
     * Sets the __name__.
     *
     * @param __type__ $__name__ The new __name__
     */
    public function set__Name__($__name__) {
        $this->__name__ = $__name__;
    }
}
```

Using this template we can easily create a class with multiple properties and their respective getters and setters:

```php
<?php

// $templateString contains the above template
$template = new PHPParser_Template($parser, $templateString);

// We only have to specify the __name__ placeholder, as the
// capitalized __Name__ placeholder is automatically created
$properties = [
    ['name' => 'title',     'type' => 'string'],
    ['name' => 'body',      'type' => 'string'],
    ['name' => 'author',    'type' => 'User'],
    ['name' => 'timestamp', 'type' => 'DateTime'],
];

$class = $factory->class('BlogPost')->implement('Post');

foreach ($properties as $propertyPlaceholders) {
    $stmts = $template->getStmts($propertyPlaceholders);

    $class->addStmts(
        // $stmts contains all statements from the template. So [0] fetches the class statement
        // and ->stmts retrieves the methods.
        $stmts[0]->stmts
    );
}

echo $prettyPrinter->prettyPrint(array($class->getNode()));
```

The result would look roughly like this:

```php
<?php

class BlogPost implements Post
{
    /**
     * @var string The title
     */
    protected $title;

    /**
     * @var string The body
     */
    protected $body;

    /**
     * @var User The author
     */
    protected $author;

    /**
     * @var DateTime The timestamp
     */
    protected $timestamp;

    /**
     * Gets the title.
     *
     * @return string The title
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Sets the title.
     *
     * @param string $title The new title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Gets the body.
     *
     * @return string The body
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * Sets the body.
     *
     * @param string $body The new body
     */
    public function setBody($body)
    {
        $this->body = $body;
    }

    /**
     * Gets the author.
     *
     * @return User The author
     */
    public function getAuthor()
    {
        return $this->author;
    }

    /**
     * Sets the author.
     *
     * @param User $author The new author
     */
    public function setAuthor($author)
    {
        $this->author = $author;
    }

    /**
     * Gets the timestamp.
     *
     * @return DateTime The timestamp
     */
    public function getTimestamp()
    {
        return $this->timestamp;
    }

    /**
     * Sets the timestamp.
     *
     * @param DateTime $timestamp The new timestamp
     */
    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
    }
}
```

When using multiple templates it is easier to manage them on the filesystem. They can be loaded using the
`TemplateLoader`:

```php
<?php

// We'll store our templates in ./templates and give them a .php suffix
$loader = new PHPParser_TemplateLoader($parser, './templates', '.php');

// loads ./templates/GetterSetter.php
$getterSetterTemplate = $loader->load('GetterSetter');

// loads ./templates/Collection.php
$collectionTemplate = $loader->load('Collection');

// The use of a suffix is optional. The following code for example is equivalent:
$loader = new PHPParser_TemplateLoader($parser, './templates');

// loads ./templates/GetterSetter.php
$getterSetterTemplate = $loader->load('GetterSetter.php');

// loads ./templates/Collection.php
$collectionTemplate = $loader->load('Collection.php');
```

Commits for Nextrek/Aiba_backup/vendor/nikic/php-parser/doc/4_Code_generation.markdown

Diff revisions: vs.
Revision Author Commited Message
1464 MOliva picture MOliva Tue 13 Oct, 2020 11:16:56 +0000