webiny/annotations

Webiny 注解组件

v1.6.1 2017-09-29 08:12 UTC

README

这个简单的组件让您能够读取分配给 classmethodproperty 的注解。

安装组件

安装组件的最佳方式是使用 Composer。

composer require webiny/annotations

要获取包的附加版本,请访问Packagist 页面

配置和设置

组件配置最小化,您只需定义桥梁依赖项。内置的桥梁使用Minime\Annotations 库。

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

定义后,您就可以开始解析注解了。最佳方法是使用 AnnotationsTrait。假设这是您希望解析注解的类。

/**
 * This is a class description with some annotations.
 *
 * @prop SomeProperty that has a string value.
 * @author.name AuthorName
 * @author.email author@name.com
 * @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()
    {

    }
}

在您希望实际进行解析的类中,只需使用 AnnotationsTrait。特质方法返回一个 ConfigObject 实例,让您可以选择使用 get 方法并链命名空间。完全支持注解命名空间,让您能够以更整洁的方式组织注解。

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"]
    }
}

资源

要运行单元测试,您需要使用以下命令

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