PHP code example of easyswoole / annotation

1. Go to this page and download the library: Download easyswoole/annotation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

easyswoole / annotation example snippets






namespace EasySwoole\Validate;

/**
 * 注解标注文件
 * @Annotation
 * 需要向上方这样使用Annotation标记这是一个注解提示类
 */
final class ValidateRule
{
    /**
     * 括号内会提示这些字段
     * @var string
     */
    protected $name;

    /**
     * 括号内会提示这些字段
     * @var string
     */
    protected $column;

    /**
     * 括号内会提示这些字段
     * @var string
     */
    protected $alias;
}




use EasySwoole\Validate as Validate;

class a
{
    /**
     * @Validate\ValidateRule(column="name",alias="账号名称")
     */
    function aaa(){

    }
}

use EasySwoole\Annotation\Annotation;
use EasySwoole\Annotation\AbstractAnnotationTag;

/*
 * 定义param渲染方法
 */

class param extends AbstractAnnotationTag
{
    public $raw;
    public function tagName(): string
    {
        return 'param';
    }

    public function assetValue(?string $raw)
    {
        $this->raw = $raw;
    }
}

/*
 * 定义timeout渲染方法
 */

class timeout extends AbstractAnnotationTag
{
    public $timeout;

    public function tagName(): string
    {
        return 'timeout';
    }

    public function assetValue(?string $raw)
    {
        $this->timeout = floatval($raw);
    }

    public function aliasMap(): array
    {
        return [
            static::class,'timeout_alias'
        ];
    }
}


class A
{
    /** @timeout()  */
    protected $a;

    /**
     * asdasdasd
     * @param(
        {
            "machineId": "X0592-0010",
           "time": 1582795808,
            "data": {
            "action": "newPhone()",
            "phone": "15505923573",
            "ismi": "ismi12456"
            },
            "signature": "023e301373b4226e6a0ea1bbdadeaa06"
        })
     */
    function test()
    {

    }
}



/*
 * 实例化渲染器,并注册要解析的渲染方法
 */
$annotation = new Annotation();
$ref = new \ReflectionClass(A::class);
//不注册fuck 解析
$annotation->addParserTag(new param());
$annotation->addParserTag(new timeout());
$annotation->addAlias('timeout_alias','timeout');

$list = $annotation->getAnnotation($ref->getMethod('test'));
var_dump($list);

$list = $annotation->getAnnotation($ref->getProperty('a'));

var_dump($list);