Dev Blog

Introducing JSON support for annotations. This allows annotating objects with JSON configuration without need to convert it to PHP array syntax. Additionally new syntax can be mixed with old one. Adding support for JSON is backwards compatible, as in previous version such syntax would throw exception.

The idea came from the MongoDB documentation, where document indexes are defined with JSON. So that syntax known by MongoDB users could be used in Mangan.

Example of annotation with JSON syntax

@Index({score: 1})

Notice the unquoted key. It is possible to use unquoted values, but risky to some extend.

A new release of addendum provides annotations for anonymous classes. One of anonymous classes feature is that these can be wrapped in another class. Fortunately PHP has support for reading doc comments from those classes, thus allowing to parse annotations. Addendum will create annotations container just like if it were named class.
254

Creating annotation class read from begining to learn about configuration

Having addendum configured, let's create annotation class. This will allow us to use @ notation on our project classes. As each annotation is in fact class, which has some simple logic to set up metadata container. This container will be used to obtain values configured by annotations. Please not that this values might be different than raw values on @ notations. As value returned by meta container is solely dependent on values set by annotation definition.

This way, we have opportunity to set initial logic in annotation, thus having already prepared and cached values returned by metadata container. To obtain access to container for currently process class, there getEntity is method in annotation. This returns container instance, which can be configured with any property required.

You might also like: