PHPackages                             egorov/yii2-annotations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. egorov/yii2-annotations

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

egorov/yii2-annotations
=======================

Allows you to use doctrine/annotations in Yii2 projects

1.1.0(5y ago)52.5k1MITPHPPHP ^7.1CI failing

Since Apr 30Pushed 3y ago2 watchersCompare

[ Source](https://github.com/Desure85/yii2-annotations)[ Packagist](https://packagist.org/packages/egorov/yii2-annotations)[ RSS](/packages/egorov-yii2-annotations/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (6)Versions (9)Used By (0)

**Yii2 Annotations extension**

Master: [![Scrutinizer Code Quality](https://camo.githubusercontent.com/223f242f2d6900267050d0c5d3431cab1511063771509d877561f95d2fb3ab52/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f44657375726538352f796969322d616e6e6f746174696f6e732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Desure85/yii2-annotations/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/dee318d7df168150e8e0399a496d2c6e438543b656dd721db78d72252a2bbf51/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f44657375726538352f796969322d616e6e6f746174696f6e732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Desure85/yii2-annotations/?branch=master)[![Build Status](https://camo.githubusercontent.com/26eb83e6a8e8a68ac4c36cf83ac45bd4289bf890824867abe29cf8fc8ebe72a4/68747470733a2f2f7472617669732d63692e6f72672f44657375726538352f796969322d616e6e6f746174696f6e732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Desure85/yii2-annotations)

Develop: [![Scrutinizer Code Quality](https://camo.githubusercontent.com/2088131deffe81da2d9f52cc3750bce1f9f073db8a4b5977830b3ae8cdb0ff02/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f44657375726538352f796969322d616e6e6f746174696f6e732f6261646765732f7175616c6974792d73636f72652e706e673f623d646576656c6f70)](https://scrutinizer-ci.com/g/Desure85/yii2-annotations/?branch=develop)[![Code Coverage](https://camo.githubusercontent.com/9ff5cb6798ec9a50d1607238c4acdc0060e11ba47d9513fb9d0f82c5dc21b92e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f44657375726538352f796969322d616e6e6f746174696f6e732f6261646765732f636f7665726167652e706e673f623d646576656c6f70)](https://scrutinizer-ci.com/g/Desure85/yii2-annotations/?branch=develop)[![Build Status](https://camo.githubusercontent.com/6bcd448087f3ce444bc6de224cab76b8edaf57147af6054699f9e473dde0a574/68747470733a2f2f7472617669732d63692e6f72672f44657375726538352f796969322d616e6e6f746174696f6e732e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/Desure85/yii2-annotations)

Use doctrine/annotations in Yii2 projects with Yii2 caching drivers

**Install**

```
composer require egorov/yii2-annotations

```

**Configuration**

```
...
    'components' => [
        'annotations' => [
            'class' => 'yii\annotations\Annotations',
            'cache' => '\yii\caching\FileCache',
            'path' => '@runtime/annotations',
            'debug' => false,
            'ignoreAnnotations' => [
                'properties', 'relations', 'property'
            ]
        ]
    ],
...

```

where :

```
class - yii\annotations\Annotations class or class implementing yii\annotations\AnnotationsInterface,
cache - Optional. Container with cache component defined in configuration or cache class. By default 'cache',
path -  Optional. Annotations cache dir for \yii\caching\FileCache,
debug - Optional boolean. Debug enable. True means that the cache will be invalid every time the file is changed, false - the cache will be generated once
ignoreAnnotations - List of ignored annotations when parsing a file

```

**Annotation class**

Annotation classes have to contain a class-level docblock with the text @Annotation:

```
/** @Annotation */
class Bar
{
    // some code
}

```

**Inject annotation values**

The annotation parser check if the annotation constructor has arguments, if so then we will pass the value array, otherwise will try to inject values into public properties directly:

```
/**
 * @Annotation
 *
 * Some Annotation using a constructor
 */
class Bar
{
    private $foo;

    public function __construct(array $values)
    {
        $this->foo = $values['foo'];
    }
}

/**
 * @Annotation
 *
 * Some Annotation without a constructor
 */
class Foo
{
    public $bar;
}

```

**Annotation Target**

@Target indicates the kinds of class element to which an annotation type is applicable. Then you could define one or more targets:

CLASS Allowed in the class docblock PROPERTY Allowed in the property docblock METHOD Allowed in the method docblock ALL Allowed in the class, property and method docblock ANNOTATION Allowed inside other annotations If the annotations is not allowed in the current context you got an AnnotationException

```
/**
 * @Annotation
 * @Target({"METHOD","PROPERTY"})
 */
class Bar
{
    // some code
}

/**
 * @Annotation
 * @Target("CLASS")
 */
class Foo
{
    // some code
}

```

**Attribute types**

Annotation parser check the given parameters using the phpdoc annotation @var, The data type could be validated using the @var annotation on the annotation properties or using the annotations @Attributes and @Attribute.

If the data type not match you got an AnnotationException

```
/**
 * @Annotation
 * @Target({"METHOD","PROPERTY"})
 */
class Bar
{
    /** @var mixed */
    public $mixed;

    /** @var boolean */
    public $boolean;

    /** @var bool */
    public $bool;

    /** @var float */
    public $float;

    /** @var string */
    public $string;

    /** @var integer */
    public $integer;

    /** @var array */
    public $array;

    /** @var SomeAnnotationClass */
    public $annotation;

    /** @var array */
    public $arrayOfIntegers;

    /** @var array */
    public $arrayOfAnnotations;
}

/**
 * @Annotation
 * @Target({"METHOD","PROPERTY"})
 * @Attributes({
 *   @Attribute("stringProperty", type = "string"),
 *   @Attribute("annotProperty",  type = "SomeAnnotationClass"),
 * })
 */
class Foo
{
    public function __construct(array $values)
    {
        $this->stringProperty = $values['stringProperty'];
        $this->annotProperty = $values['annotProperty'];
    }

    // some code
}

```

**Annotation Required**

@Required indicates that the field must be specified when the annotation is used. If it is not used you get an AnnotationException stating that this value can not be null.

Declaring a required field:

```
/**
 * @Annotation
 * @Target("ALL")
 */
class Foo
{
    /** @Required */
    public $requiredField;
}

```

Usage:

```

/** @Foo(requiredField="value") */
public $direction;                  // Valid

 /** @Foo */
public $direction;                  // Required field missing, throws an AnnotationException

```

**Enumerated values**

An annotation property marked with @Enum is a field that accept a fixed set of scalar values. You should use @Enum fields any time you need to represent fixed values. The annotation parser check the given value and throws an AnnotationException if the value not match. Declaring an enumerated property:

```
/**
 * @Annotation
 * @Target("ALL")
 */
class Direction
{
    /**
     * @Enum({"NORTH", "SOUTH", "EAST", "WEST"})
     */
    public $value;
}

```

Annotation usage:

```
/** @Direction("NORTH") */
public $direction;                  // Valid value

 /** @Direction("NORTHEAST") */
public $direction;                  // Invalid value, throws an AnnotationException

```

**Constants**

The use of constants and class constants are available on the annotations parser.

The following usage are allowed:

```
use MyCompany\Annotations\Foo;
use MyCompany\Annotations\Bar;
use MyCompany\Entity\SomeClass;

/**
 * @Foo(PHP_EOL)
 * @Bar(Bar::FOO)
 * @Foo({SomeClass::FOO, SomeClass::BAR})
 * @Bar({SomeClass::FOO_KEY = SomeClass::BAR_VALUE})
 */
class User
{
}

```

***Be careful with constants and the cache !***

The cached reader will not re-evaluate each time an annotation is loaded from cache. When a constant is changed the cache must be cleaned.

**Usage Example**

Create annotations class

```
