File: intro.md

package info (click to toggle)
php-zend-code 4.0.0-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,840 kB
  • sloc: php: 12,078; xml: 2,759; makefile: 16
file content (126 lines) | stat: -rw-r--r-- 3,449 bytes parent folder | download | duplicates (3)
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
# Introduction

`Laminas\Code\Generator` provides facilities to generate arbitrary code using an object-oriented
interface, both to create new code as well as to update existing code. While the current
implementation is limited to generating *PHP* code, you can easily extend the base class in order to
provide code generation for other tasks: JavaScript, configuration files, apache vhosts, etc.

## Theory of Operation

In the most typical use case, you will simply instantiate a code generator class and either pass it
the appropriate configuration or configure it after instantiation. To generate the code, you will
simply echo the object or call its `generate()` method.

```php
// Passing configuration to the constructor:
$file = new Laminas\Code\Generator\FileGenerator(array(
    'classes' => array(
        new Laminas\Code\Generator\ClassGenerator(
            'World',  // name
            null,     // namespace
            null,     // flags
            null,     // extends
            array(),  // interfaces
            array(),  // properties
            array(
                new Laminas\Code\Generator\MethodGenerator(
                    'hello',                  // name
                    array(),                  // parameters
                    'public',                 // visibility
                    'echo \'Hello world!\';'  // body
                ),
            )
        ),
    ),
));

// Render the generated file
echo $file->generate();

// or write it to a file:
file_put_contents('World.php', $file->generate());

// OR

// Configuring after instantiation
$method = new Laminas\Code\Generator\MethodGenerator();
$method->setName('hello')
       ->setBody('echo \'Hello world!\';');

$class = new Laminas\Code\Generator\ClassGenerator();
$class->setName('World')
      ->addMethodFromGenerator($method);

$file = new Laminas\Code\Generator\FileGenerator();
$file->setClass($class);

// Render the generated file
echo $file->generate();

// or write it to a file:
file_put_contents('World.php', $file->generate());
```

Both of the above samples will render the same result:

```php
<?php

class World
{

    public function hello()
    {
        echo 'Hello world!';
    }

}
```

Another common use case is to update existing code -- for instance, to add a method to a class. In
such a case, you must first inspect the existing code using reflection, and then add your new
method. `Laminas\Code\Generator` makes this trivially simple, by leveraging `Laminas\Code\Reflection`.

As an example, let's say we've saved the above to the file `World.php`, and have already included
it. We could then do the following:

```php
$class = Laminas\Code\Generator\ClassGenerator::fromReflection(
    new Laminas\Code\Reflection\ClassReflection('World')
);

$method = new Laminas\Code\Generator\MethodGenerator();
$method->setName('mrMcFeeley')
       ->setBody('echo \'Hello, Mr. McFeeley!\';');
$class->addMethodFromGenerator($method);

$file = new Laminas\Code\Generator\FileGenerator();
$file->setClass($class);

// Render the generated file
echo $file->generate();

// Or, better yet, write it back to the original file:
file_put_contents('World.php', $file->generate());
```

The resulting class file will now look like this:

```php
<?php

class World
{

    public function hello()
    {
        echo 'Hello world!';
    }

    public function mrMcFeeley()
    {
        echo 'Hellow Mr. McFeeley!';
    }

}
```