Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding trait example into documentation. #75

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/book/generator/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,46 @@ $generator->addMethod(
);
$code = $generator->generate();
```


## Generating Trait

`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.
Comment on lines +377 to +378
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two sentences are copied from the section "Generating PHP files" and it does not the describe the generating of a trait.


```php
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\TraitGenerator;

$traitclass = new TraitGenerator();
$traitclass->addMethod('MethodOne');
$traitclass->addConstant('VERSION','1.0.0');

$traitFile = new Laminas\Code\Generator\FileGenerator();
$traitFile->setClass($traitclass);

```
Comment on lines +380 to +391
Copy link
Member

@froschdesign froschdesign Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code example can be shortened to the essentials.

Suggested change
```php
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\TraitGenerator;
$traitclass = new TraitGenerator();
$traitclass->addMethod('MethodOne');
$traitclass->addConstant('VERSION','1.0.0');
$traitFile = new Laminas\Code\Generator\FileGenerator();
$traitFile->setClass($traitclass);
```
```php
$trait = new Laminas\Code\Generator\TraitGenerator();
$trait->setName('TranslatorAwareTrait');
$trait->addProperty(
'translator', // Name
null, // Default value
Laminas\Code\Generator\PropertyGenerator::FLAG_PRIVATE // Flags
);
$trait->addMethod(
'getTranslator', // Name
[], // Parameters
Laminas\Code\Generator\PropertyGenerator::FLAG_PUBLIC, // Flags
'return $this->translator;' // Body
);
echo $trait->generate();

Copy link

@RSully RSully Apr 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the use statements could still be handy - though I admit I don't know how the rest of the documentation is structured and if there's a standard or not.

Edit: never-mind ignore me :) - I see the classes are fully qualified


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 = $traitFile->generate();
file_put_contents('Trait.php', $traitclass);
```

The above will generate the following file:

```php
<?php

trait
{
public const VERSION = '1.0.0';

public function MethodOne()
{
}
}

```
Comment on lines +393 to +415
Copy link
Member

@froschdesign froschdesign Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 = $traitFile->generate();
file_put_contents('Trait.php', $traitclass);
```
The above will generate the following file:
```php
<?php
trait
{
public const VERSION = '1.0.0';
public function MethodOne()
{
}
}
```
The code generator produces the following trait:
```php
trait TranslatorAwareTrait
{
private $translator = null;
public function getTranslator()
{
return $this->translator;
}
}