Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Powerfull and easy to use annotations with lightweight container
PHP HTML

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
extras
src
tests
toc
.gitattributes
.gitignore
.hedron.yml
.scrunitizer.yml
.travis.yml
CHANGELOG.html
CONTRIBUTORS.html
INSTALL.html
LICENSE-AGPL.html
LICENSE-COMMERCIAL-SAAS.html
LICENSE-COMMERCIAL-WEBSITE.html
LICENSE-COMMERCIAL-WEBSITES.html
README.html
README.md
UPGRADE.html
codeception.yml
composer.json
composer.lock
debug-test.php
make-junctions.bat
make-toc
make-unit
push-tests
run-test
run-tests
run-tests-coverage

README.md

Maslosoft Addendum

Easy to use php annotations

Latest Stable Version License Scrutinizer Code Quality

Quick Install

composer require maslosoft/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

Background

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.

Key features

  • Easy add metadata
  • Lightweight container
  • Extendable
  • Netbeans completition support

Setup

Use composer to install extension:

composer require maslosoft/addendum:"*"

Yii integration

Addendum uses EmbeDi for configuration, so configure and preload embedi too:

'preload' => ['embedi'],
'components' => [
    'embedi' => [
        'class' => \Maslosoft\EmbeDi\Adapters\YiiEmbeDi::class
    ],
    'addendum' => [
        'class' => Maslosoft\Addendum\Addendum::class,
    ],
]

Basic usage

This only in short summary, please refer to full yii addendum documentation or docs folder of this exteion for detailed explantion and php addendum documentation for in-depth annotations docs.

What are annotations

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.

Using annotations in your application

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.

Defining annotation

Creating your own annotation is very easy. Ill demonstrate it on @Label. Just create class with Annotation suffix, and make sure it is imported.

<?php

/**
 * 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.

Requirements

  • PHP 5.5

Known bugs

Resources

Big thanks goes to

  • Jan Suchal for creating php addendum
  • be next here:)
Something went wrong with that request. Please try again.