Yii Addendum
Goal of this extension is to provide additional metadata for any class, their properties or methods. It's based on annotations, so adding this metadata is as easy as adding doc comments. This is build on top of addendum php library
I created this extension, because I wanted to see all attribute properties of my models in one place and also needed more than Yii built in. I mean like validators, labels. This way i have property of model, and just above it i have all what it defines. Some models even does not require any methods, plain attributes with annotations.
As with most extensions add this to components
'components' => [ 'addendum' => [ 'class' => 'application.extensions.addendum.EAddendum', ],
Also you have to import addendum folder
// In config 'import' => [ 'application.extensions.addendum.*' ] // Or manually Yii::import('application.extensions.addendum.*');
Then anywhere in code import folder with your annotations definiotions.
// This is path for default annotations which ships with Yii addendum Yii::import('application.extensions.addendum.annotations.*');
This is still work in progress, however feature ready, and there should be no major changes in terms of usage and interface. Sadly there are no docs yet. Some examples are included, and this page for reference. For downloads see github page
If you are familiar with annotations, skip this chapter.
Annotations are special type of comments, which are parsed and evaluated by annotaion engine. They are similar to php doc tag, but to distinguish they starts with capital letter. They also can have optional params or even can be nested. Simple example:
/** * @Label('First name') * @Persistent * @RequiredValidator * @var string */ $name = '';
@var is well known doc tag, while @Label defines label for this attribute. @Persistent indicates that attribute is persistent and @RequiredValidator might add built in required validator. Notice the word might, as annotations are not classes, what they do can be defined elsewhere. For detailed annotations syntax please visit addendum documentation.
First of all, if you want your class to be used by addendum engine, you have to "implement" IAnnotated interface. Implement is a big word here, as it is just empty interface, used only to allow annotating class.
Now you can add annotations like in example below:
/** * @Label('My application user') */ class User extends CActiveRecord implements IAnnotated { /** * @Label('First name') * @RequiredValidator * @var string */ $name = ''; }
Now you have some annotation added. Each annotation defines some metadadata for it's entity - using entity i will refer to one of class, property or method.
Next we can scrap that metadata from class definition, and put it to lightweight container.
$meta = Yii::app()->addendum->meta(new User); // You can also create container directly // $meta = EComponentMeta::create(new User); - this will return the same as above echo $meta->type()->label; echo $meta->name->label;
This will output My application user First name.
$meta->type() gets class (type) metadata, while class properties are available as fields, and methods with $meta->method('methodName') function.
NOTE: setting @Label does not mean that label field will be set in container - it is annotation responsibility of what to do with it's data.
Creating your own annotation is very easy. Ill demonstrate it on @Label. Just create class with Annotation suffix, and make sure it is imported.
/** * Label * Set translated entity 'label' field * @template Label('${text}') */ class LabelAnnotation extends EComponentMetaAnnotation { public $value = ''; public function init() { $this->value = Yii::t('', $this->value); $this->_entity->label = $this->value; } public function __toString() { return $this->value; } }
Here special property _entity have set label field. _entity is container for class (type), field or method, depending on context.
NOTE: @template is special docblock used to generate netbeans completition files.
Total 2 comments
There is nothing to make screenshot of. Eventually screenshot of netbeans code completition working with annotations.
Hello, nice work.
A screen shot would be nice
Leave a comment
Please login to leave your comment.