-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Conversation
Adding example for issue laminas#24
Ping @michalbundyra |
4091991 should probably not be part of this patch: was the patch started from this branch? |
yes @Ocramius , I think there is some webhook executed by @michalbundyra which add 4091991 |
@vrkansagara nay, that commit is in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, too much was copied from a previous section. Some corrections are needed.
`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. |
There was a problem hiding this comment.
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); | ||
|
||
``` |
There was a problem hiding this comment.
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.
```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(); |
There was a problem hiding this comment.
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() | ||
{ | ||
} | ||
} | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; | |
} | |
} |
Is it possible to add code to the body of a generated trait method? That would seem important and is missing from these examples, and part of why I made my initial request. |
@RSully I have already explained here 847be56#diff-3d8f5388de37ae1d660bf9198127eb4e4a7d0796e5cd53fab4d602293b388371R406 |
That doesn't address my question. For example, I am asking to show something like this: trait HelloWorld {
private $greeting = 'Hello World';
public function sayHello() {
echo $this->greeting;
}
} Also, according to a quick test in PHP 8, traits cannot have constants - so your example's generated code doesn't compile.
|
I have updated the example from above. |
Houskeeping is here, closing :) |
Description