-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Closes issue #4876 - added support for trait generation and trait scanning #6339
Conversation
/* | ||
* @todo test if this works when trait is included or required instead of autoloaded? | ||
*/ | ||
$fileName = str_replace("\\", "/", $traitName) .".php"; |
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.
Currently the unit test is failing because of this. This is attempting to create a FileScanner instance of the Trait that is being used. If it's not on the include_path it isn't going to work. Not quite sure how to accomplish this at the moment.
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.
You could do something like this:
$r = new ReflectionClass($traitName);
if (! $r->isTrait()) {
throw new Exception\RuntimeException('Non-trait detected as a trait: ' . $traitName);
}
$fileName = $r->getFileName();
I'll give that a try shortly to see if it works.
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.
Worked!
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.
Hmmm... this may not be the best approach; the point of the Scanner
subcomponent is to allow scanning without reflection. I'm going to leave with my changes for now; we can see if there's a better way to accomplish this later.
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.
Please be aware that changing this to not using reflection could be a BC-break depending on how it is changed. Code can depend on the fact that the traits are autoloaded by ReflectionClass
.
@steverhoades need to rebase this one |
@Ocramius sorry missed this notification somehow. Will do this as soon as possible. |
7b80e77
to
3a108ef
Compare
@Ocramius I have rebased this commit. |
Any updates on this? |
I'm currently reviewing this: will rewrite some bits. |
@steverhoades what is your take about using a ValueObject instead of different array formats? The current API is messy mainly because of all the different supported parameter types... |
@Ocramius a ValueObject would make much more sense. |
@steverhoades |
@Ocramius Works for me. |
I still didn't find the time to work on this, but I will have to before 2.4 |
it need rebase |
@steverhoades Can you rebase, please? Also, do you or @Ocramius have time to do the changes for:
in the next 2 weeks? If not, I can see if I can carve out some time, but I need to know fairly soon so I can determine if this will be part of 2.4. Thanks! |
@weierophinney My time is going to be tight over the next few weeks. I would like to see this as part of 2.4 though. I'll take another look at whats left and get back to you asap. |
…for trait generation, trait scanning and method reflection
d25bffa
to
ce2df4e
Compare
@weierophinney @samsonasik I rebased the commit. Matthew @Ocramius I have not yet had a chance to look at the refactoring. I will do that asap now that the branch has been rebased. |
@steverhoades awesome; many thanks in advance! |
@weierophinney @Ocramius I've gone ahead and refactored into a TraitUsageGenerator class. @Ocramius let me know if this is what you had in mind. We can tweak as necessary. |
@Ocramius I think I may have mis-interpreted the direction on this. I think you were thinking more along the lines of addTrait(TraitUsageGenerator $trait). Let me know - I may need to go back to the drawing board on this one. /cc @weierophinney |
* @param string|null $useAlias | ||
* @return ClassGenerator | ||
*/ | ||
public function addUse($use, $useAlias = null) |
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.
Was in the middle of commenting that removing this is a BC break, and suggesting to have it proxy to the TraitUsageGenerator
... and then saw that you just moved the method to later in the class. :)
Closes issue #4876 - added support for trait generation and trait scanning
Merged to develop for release with 2.4. |
This is a first attempt at adding generation and scanning support for Traits.
For basic examples see below, for other usage please see ZendTest/Code/Generation/ClassGenerationTest.php
Example 1: Add a trait
Outputs:
Example 2: Add a trait to a class
Outputs:
Example 2: Add multiple traits with aliases and overrides
Outputs: