2017 © Pedro Peláez
 

library annotations

The KISS PHP annotations library

image

minime/annotations

The KISS PHP annotations library

  • Tuesday, September 5, 2017
  • by marcioAlmada
  • Repository
  • 13 Watchers
  • 217 Stars
  • 35,025 Installations
  • PHP
  • 28 Dependents
  • 0 Suggesters
  • 23 Forks
  • 6 Open issues
  • 34 Versions
  • 10 % Grown

The README.md

Minime \ Annotations

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads Reference Status License SensioLabsInsight, (*1)

Minime\Annotations is the first KISS PHP annotations library., (*2)

Composer Installation

{
  "require": {
    "minime/annotations": "~3.0"
  }
}

Through terminal: composer require minime/annotations:~3.0 :8ball:, (*3)

Setup

First grab an instance of the Minime\Annotations\Reader the lazy way:, (*4)

$reader = \Minime\Annotations\Reader::createFromDefaults();

Or instantiate the annotations reader yourself with:, (*5)

use Minime\Annotations\Reader;
use Minime\Annotations\Parser;
use Minime\Annotations\Cache\ArrayCache;

$reader = new Reader(new Parser, new ArrayCache);

Notice that Reader::createFromDefaults() creates a reader instance with array cache enabled. On production you might want to use a persistent cache handler like FileCache instead:, (*6)

use Minime\Annotations\Cache\FileCache;

$reader->setCache(new FileCache('app/storage/path'));

Reading Annotations

Consider the following class with some docblock annotations:, (*7)

<?php
/**
 * @name Foo
 * @accept ["json", "xml", "csv"]
 * @delta .60
 * @cache-duration 60
 */
class FooController
{
    /**
     * @manages Models\Baz
     */
    protected $repository;

    /**
     * @get @post
     * @redirect Controllers\BarController@index
     */
    public function index(){}
}

Let's use the Minime\Annotations\Reader instance to read annotations from classes, properties and methods. Like so:, (*8)

$annotations = $reader->getClassAnnotations('FooController');

$annotations->get('name')     // > string(3) "Foo"
$annotations->get('accept')   // > array(3){ [0] => "json" [1] => "xml" [2] => "csv" }
$annotations->get('delta')           // > double(0.60)
$annotations->get('cache-duration')  // > int(60)

$annotations->get('undefined')  // > null

The same applies to class properties..., (*9)

$annotations = $reader->getPropertyAnnotations('FooController', 'repository');
$annotations->get('manages')   // > string(10) "Models\Baz"

methods..., (*10)

$annotations = $reader->getMethodAnnotations('FooController', 'index');
$annotations->get('get')   // > bool(true)
$annotations->get('post')   // > bool(true)
$annotations->get('auto-redirect')   // > string(19) "BarController@index"

and functions || closures:, (*11)

/** @name Foo */ function foo(){}
$annotations = $reader->getFunctionAnnotations('foo');
$annotations->get('name')   // > string(3) "Foo"

Managing Annotations

The annotations reader Reader::get(*)Annotations always returns AnnotationsBag instances so you can easily manage annotations:, (*12)

/**
 * @response.xml
 * @response.xls
 * @response.json
 * @response.csv
 * @method.get
 * @method.post
 */
class Foo {}

$annotations = $reader->getClassAnnotations('Foo'); // object<AnnotationsBag>

Namespacing

It's a good idea to namespace custom annotations that belong to a package. Later you can retrieve all those namespaced annotations using the AnnotationsBag api:, (*13)

$AnnotationsBag->useNamespace('response')->toArray();
// > array(3){
// >    ["xml"]  => (bool) TRUE,
// >    ["xls"]  => (bool) TRUE,
// >    ["json"] => (bool) TRUE,
// >    ["csv"]  => (bool) TRUE
// > }

Piping Filters

You can also easily "pipe" filters. This time let's "grep" all annotations beginning with "x" and within "response" namespace:, (*14)

$AnnotationsBag->useNamespace('response')->grep('/^x/')->toArray();
// > array(3){
// >    ["xml"]  => (bool) TRUE,
// >    ["xls"]  => (bool) TRUE
// > }

Traversing results

As you might expect, AnnotationsBag is traversable too:, (*15)

foreach($annotations->useNamespace('method') as $annotation => $value)
{
    // some behavior
}

Union

You can also perform union operations between two annotations sets:, (*16)

$annotations->union($defaultAnnotations);

Please refer to annotations bag public API for more operations., (*17)

The Default Syntax

@(<namespace><namespace-delimiter>)?<annotation-identifier> <type>? <value>?, (*18)

Which basically means that:, (*19)

  • @ line must start with a docblock annotation tag
  • must have an annotation identifier
    • annotation identifier can have namespace with segments delimited by . or \
  • whitespace
  • can have an annotation value
    • value can have an optional type [json, string, integer, float, ->]
      • if absent, type is assumed from value
    • whitespace
    • optional value
      • if absent, true is assumed

Some valid examples below:, (*20)

/**
 * Basic docblock showing syntax recognized by the default Minime\Annotations\Parser
 *
 * @implicit-boolean
 * @explicit-boolean true
 * @explicit-boolean false
 *
 * @implicit-string-annotation  hello world!
 * @explicit-string-annotation "hello world!"
 * @string-strong-typed-annotation string 123456
 *
 * @integer-annotation 15
 * @integer-strong-typed-annotation integer 15
 *
 * @float-annotation   0.15
 * @float-strong-typed float 15
 *
 * @json-annotation { "foo" : ["bar", "baz"] }
 * @strong-typed-json-annotation json ["I", "must", "be", "valid", "json"]
 * 
 * @namespaced.annotation hello!
 *
 * @multiline-json-annotation {
 *   "foo" : [
 *      "bar", "baz"
 *    ]
 * }
 *
 * @multiline-indented-string-annotation
 *   ------
 *   < moo >
 *   ------ 
 *         \   ^__^
 *          \  (oo)\_______
 *             (__)\       )\/\
 *                 ||----w |
 *                 ||     ||
 * 
 * @Concrete\Class\Based\Annotation -> { "foo" : ["bar"] }
 */

Concrete Annotations

Sometimes you need your annotations to encapsulate logic and you can only do it by mapping instructions to formal PHP classes. These kind of "concrete" typed annotations can be declared with the -> (arrow symbol):, (*21)

/**
 * @Model\Field\Validation -> {
 *     "rules" : {
 *         "required" : true,
 *         "max-length" : 100
 *     }
 * }
 */

In the example above: when prompted, the annotation parser will instantiate a new \Model\Field\Validation() following the declared JSON prototype { "rules" : {...} }. Voilà! Instantly classy annotations., (*22)

Caching

This package comes with two basic cache handlers. ArrayCache (for testing) and a very simple FileCache handler for persistence. Cache handlers can be set during Minime\Annotations\Reader instantiation:, (*23)

use Minime\Annotations\Reader;
use Minime\Annotations\Parser;
use Minime\Annotations\Cache\FileCache;

$cacheHandler = new FileCache('storage/path');
$reader = new Reader(new Parser, $cacheHandler);

Or later with Reader::setCache():, (*24)

$reader->setCache(new FileCache);

Public API

Minime\Annotations\Reader

::getClassAnnotations($subject)

Get all annotations from a given class:, (*25)

$reader->getClassAnnotations('Full\Qualified\Class');

::getPropertyAnnotations($subject, $propertyName)

Get all annotations from a given class property:, (*26)

$reader->getPropertyAnnotations('Full\Qualified\Class', 'propertyName');

::getMethodAnnotations($subject, $methodName)

Get all annotations from a given class method:, (*27)

$reader->getMethodAnnotations('Full\Qualified\Class', 'methodName');

::getFunctionAnnotations($fn)

Get all annotations from a given full qualified function name or closure:, (*28)

$reader->getFunctionAnnotations('utils\foo');

::getCache()

::setCache(CacheInterface $cache)

::getParser()

::setParser(ParserInterface $cache)

Minime\Annotations\AnnotationsBag

::grep($pattern)

Filters annotations using a valid regular expression and returns a new AnnotationBag with the matching results., (*29)

::useNamespace($pattern)

Isolates a given namespace of annotations. Basically this method filters annotations by a namespace and returns a new AnnotationBag with simplified annotations identifiers., (*30)

::union(AnnotationsBag $bag)

Performs union operation with a subject AnnotationBag:, (*31)

$annotations->union($defaultAnnotations);

::toArray()

::get($key, $default = null)

::getAsArray($key)

::has($key)

::set($key, $value)

::count

Minime\Annotations\Cache\FileCache

::__construct($storagePath = null)

See example in context with Minime\Annotations\Reader:, (*32)

use Minime\Annotations\Cache\FileCache;

$reader->setCache(new FileCache('app/tmp/storage/path'));

If no path is given OS tmp dir is assumed as cache storage path., (*33)

::clear()

Clears entire cache. See example in context with Minime\Annotations\Reader:, (*34)

$reader->getCache()->clear();

Minime\Annotations\Cache\ArrayCache

::clear()

Clears entire cache. See example in context with Minime\Annotations\Reader:, (*35)

$reader->getCache()->clear();

Contributions

Found a bug? Have an improvement? Take a look at the issues., (*36)

Guide

  1. Fork minime\annotations
  2. Clone forked repository
  3. Install composer dependencies $ composer install
  4. Run unit tests $ phpunit
  5. Modify code: correct bug, implement features
  6. Back to step 4

Copyright (c) 2013-2014 Márcio Almada. Distributed under the terms of an MIT-style license. See LICENSE for details., (*37)

The Versions

05/09 2017

dev-master

9999999-dev

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

05/09 2017

3.1.0

3.1.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

31/01 2017

3.0.2

3.0.2.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

31/01 2017

3.0.1

3.0.1.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

19/09 2016

v3.x-dev

3.9999999.9999999.9999999-dev

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

19/09 2016

3.0.0

3.0.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

17/09 2015

2.3.1

2.3.1.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

26/01 2015

2.x-dev

2.9999999.9999999.9999999-dev

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

26/01 2015

2.3.0

2.3.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

06/11 2014

2.2.0

2.2.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

22/09 2014

2.1.0

2.1.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

15/09 2014

2.0.0

2.0.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

 

The Development Requires

php docblock annotations reflection metadata

11/08 2014

1.x-dev

1.9999999.9999999.9999999-dev

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-json *

 

php docblock annotations reflection metadata

11/08 2014

1.14.0

1.14.0.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-json *

 

php docblock annotations reflection metadata

11/04 2014

1.13.17

1.13.17.0

The KISS PHP annotations library

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-json *

 

php docblock annotations reflection metadata

25/01 2014

1.12.16

1.12.16.0

Easy PHP annotations.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-json *

 

php docblock annotations reflection metadata

10/01 2014

1.12.15

1.12.15.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

29/12 2013

1.11.15

1.11.15.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

27/12 2013

1.11.14

1.11.14.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

26/12 2013

1.11.13

1.11.13.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

20/12 2013

1.11.12

1.11.12.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

17/12 2013

1.11.11

1.11.11.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

03/12 2013

1.11.8

1.11.8.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

03/12 2013

1.10.8

1.10.8.0

PHP annotations without the bureaucracy.

  Sources   Download

MIT

The Requires

 

php docblock annotations reflection metadata

11/11 2013

1.8.7

1.8.7.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

30/10 2013

1.7.6

1.7.6.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

29/10 2013

1.7.5

1.7.5.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

22/10 2013

1.6.5

1.6.5.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

11/10 2013

1.5.4

1.5.4.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

04/10 2013

1.5.3

1.5.3.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

27/09 2013

1.4.3

1.4.3.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

26/09 2013

1.3.3

1.3.3.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

23/09 2013

1.2.3

1.2.3.0

Minime / Annotations is a lightweight PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata

07/09 2013

1.0.0

1.0.0.0

Minime / Annotations is a lightweight (dependency free PHP) PHP annotation library

  Sources   Download

MIT

The Requires

 

php annotations reflection metadata