Git Product home page Git Product logo

annotations-2's Introduction

Annotations Component

This simple component lets you read annotations assigned to a class, method or a property.

Install the component

The best way to install the component is using Composer.

composer require webiny/annotations

For additional versions of the package, visit the Packagist page.

Configuration and setup

The component configuration is minimal, you just need to define the bridge dependency. The built-in bridge uses Minime\Annotations library.

Annotations:
    Bridge: \Webiny\Component\Annotations\Bridge\Minime\Annotations
    # You don't need this part if you are using Composer autoload.
    ClassLoader:
        Minime\Annotations: /var/www/Vendors/Minime/Annotations/src

Once defined, you can start parsing the annotations. The best approach is to use the AnnotationsTrait. Let's say that this is the class that you wish to parse the annotations from.

/**
 * This is a class description with some annotations.
 *
 * @prop SomeProperty that has a string value.
 * @author.name AuthorName
 * @author.email [email protected]
 * @author.website.url http://www.webiny.com
 * @author.website.desc My website
 *
 */
class TestClass
{
    /**
     * @var SomeVarAnnotation
     * @public
     */
    var $someVar;

    /**
     * @access.role ADMIN
     * @access.level 3
     * @name SomeName
     */
    private $_anotherVar;

    /**
     * @post @get
     * @cache.ttl 10
     * @cache.store true @cache.key cacheKey
     * @accept ["json", "xml"]
     */
    function someMethod()
    {

    }
}

In your class, where you wish to do the actually parsing, just use the AnnotationsTrait. Trait methods return an instance of ConfigObject giving you the option to use the get method and to chain the namespaces. Annotation namespaces are fully supported, giving you the possibility to organize the annotations in much cleaner way.

class MyClass
{
    use AnnotationsTrait;

    function getClassAnnotations()
    {
        $classAnnotations = $this->annotationsFromClass('TestClass');
        $classAnnotations->prop; // SomeProperty that has a string value.
        $classAnnotations->author->website->url; // http://www.webiny.com
    }

    function getPropertyAnnotations()
    {
        $someVarPropertyAnnotations = $this->annotationsFromProperty('TestClass', 'someVar');
        $someVarPropertyAnnotations->public; // returns "true"
    }

    function getMethodAnnotations()
    {
        $methodAnnotations = $this->annotationsFromMethod('TestClass', 'someMethod');
        $methodAnnotations->cache->key; // cacheKey
        $methodAnnotations->accept; // [0 => "json", 1 => "xml"]
    }
}

Resources

To run unit tests, you need to use the following command:

$ cd path/to/Webiny/Component/Annotations/
$ composer.phar install
$ phpunit

annotations-2's People

Contributors

svenalhamad avatar marcioalmada avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.