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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
# Laminas\\Code\\Generator Examples
## Generating PHP classes
The following example generates an empty class with a class-level DocBlock.
```php
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\DocBlock\Tag\GenericTag;
echo (new ClassGenerator())
->setName('Foo')
->setDocblock(
(new DocBlockGenerator())
->setShortDescription('Sample generated class')
->setLongDescription('This is a class generated with Laminas\Code\Generator.')
->setTags([
new GenericTag('version', '$Rev:$'),
new GenericTag('license', 'New BSD'),
])
)
->generate();
```
The above code will result in the following:
```php
/**
* Sample generated class
*
* This is a class generated with Laminas\Code\Generator.
*
* @version $Rev:$
* @license New BSD
*
*/
class Foo
{
}
```
### Generating PHP classes with class properties
Building on the previous example, we now add properties to our generated class.
```php
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\PropertyGenerator;
echo (new ClassGenerator())
->setName('Foo')
->setDocblock(
(new DocBlockGenerator())
->setShortDescription('Sample generated class')
->setLongDescription('This is a class generated with Laminas\Code\Generator.')
->setTags([
new GenericTag('version', '$Rev:$'),
new GenericTag('license', 'New BSD'),
])
)
->addProperties([
new PropertyGenerator('bar', 'baz', PropertyGenerator::FLAG_PROTECTED),
new PropertyGenerator('baz', 'bat', PropertyGenerator::FLAG_PUBLIC),
])
->addConstants([
new PropertyGenerator('bat', 'foobarbazbat', PropertyGenerator::FLAG_CONSTANT)
]);
->generate();
```
The above results in the following class definition:
```php
/**
* Sample generated class
*
* This is a class generated with Laminas\Code\Generator.
*
* @version $Rev:$
* @license New BSD
*
*/
class Foo
{
protected $bar = 'baz';
public $baz = 'bat';
const bat = 'foobarbazbat';
}
```
### Generating PHP classes with class methods
`Laminas\Code\Generator\ClassGenerator` allows you to attach methods with optional content to your
classes. Methods may be attached as either arrays or concrete `Laminas\Code\Generator\MethodGenerator`
instances.
```php
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\DocBlock\Tag;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\PropertyGenerator;
echo (new ClassGenerator())
->setName('Foo')
->setDocblock(
(new DocBlockGenerator())
->setShortDescription('Sample generated class')
->setLongDescription('This is a class generated with Laminas\Code\Generator.')
->setTags([
new GenericTag('version', '$Rev:$'),
new GenericTag('license', 'New BSD'),
])
)
->addProperties([
new PropertyGenerator('bar', 'baz', PropertyGenerator::FLAG_PROTECTED),
new PropertyGenerator('baz', 'bat', PropertyGenerator::FLAG_PUBLIC),
])
->addConstants([
new PropertyGenerator('bat', 'foobarbazbat', PropertyGenerator::FLAG_CONSTANT)
]);
->addMethods([
// Method built programmatically
(new MethodGenerator())
->setName('setBar')
->setParameters([
new ParameterGenerator('bar')
])
->setBody('$this->bar = $bar;' . "\n" . 'return $this;')
->setDocBlock(
(new DocBlockGenerator())
->setShortDescription('Set the bar property')
->setTags([
new Tag\ParamTag(
'bar',
['string', 'array'],
'parameter description'
),
new Tag\ReturnTag([
'datatype' => 'string',
]),
])
)
]),
// Method passed as concrete instance
new MethodGenerator(
'getBar',
[],
MethodGenerator::FLAG_PUBLIC,
'return $this->bar;',
(new DocBlockGenerator())
->setShortDescription('Retrieve the bar property'),
->setTags([
new Tag\ReturnTag(['string|null']),
])
])
),
]);
->generate();
```
The above generates the following output:
```php
/**
* Sample generated class
*
* This is a class generated with Laminas\Code\Generator.
*
* @version $Rev:$
* @license New BSD
*/
class Foo
{
protected $bar = 'baz';
public $baz = 'bat';
const bat = 'foobarbazbat';
/**
* Set the bar property
*
* @param string|array $bar parameter description
*/
public function setBar($bar)
{
$this->bar = $bar;
return $this;
}
/**
* Retrieve the bar property
*
* @return string|null
*/
public function getBar()
{
return $this->bar;
}
}
```
## Generating PHP files
`Laminas\Code\Generator\FileGenerator` can be used to generate the contents of a *PHP* file. You can
include classes as well as arbitrary content body. When attaching classes, you should attach either
concrete `Laminas\Code\Generator\ClassGenerator` instances or an array defining the class.
In the example below, we will assume you've defined `$foo` per one of the class definitions in a
previous example.
```php
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\DocBlock\Tag\GenericTag;
$file = (new FileGenerator)
->setClasses([$foo])
->setDocBlock(
(new DocBlockGenerator())
->setShortDescription('Foo class file')
->setTag([
new GenericTag('license', 'New BSD')
])
)
->setBody("define('APPLICATION_ENV', 'testing');");
```
Calling `generate()` will generate the code -- but not write it to a file. You will need to capture
the contents and write them to a file yourself. As an example:
```php
$code = $file->generate();
file_put_contents('Foo.php', $code);
```
The above will generate the following file:
```php
<?php
/**
* Foo class file
*
* @license New BSD
*/
/**
* Sample generated class
*
* This is a class generated with Laminas\Code\Generator.
*
* @version $Rev:$
* @license New BSD
*/
class Foo
{
protected $bar = 'baz';
public $baz = 'bat';
const bat = 'foobarbazbat';
/**
* Set the bar property
*
* @param string bar
* @return string
*/
public function setBar($bar)
{
$this->bar = $bar;
return $this;
}
/**
* Retrieve the bar property
*
* @return string|null
*/
public function getBar()
{
return $this->bar;
}
}
define('APPLICATION_ENV', 'testing');
```
## Add code to existing PHP files and classes
### Seeding PHP class generation via reflection
You may add code to an existing class. To do so, first use the static `fromReflection()` method to
map the class into a generator object. From there, you may add additional properties or methods, and
then regenerate the class.
```php
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\DocBlock\Tag;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Reflection\ClassReflection;
$generator = ClassGenerator::fromReflection(
new ClassReflection($class)
);
$generator->addMethod(
'setBaz',
['baz'],
MethodGenerator::FLAG_PUBLIC,
'$this->baz = $baz;' . "\n" . 'return $this;',
(new DocBlockGenerator())
->setShortDescription('Set the baz property')
->setTags([
new Tag\ParamTag('baz', ['string']),
new Tag\ReturnTag('string'),
])
);
$code = $generator->generate();
```
|