承接 jan-swiecki/simple-annotations 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jan-swiecki/simple-annotations

Composer 安装命令:

composer require jan-swiecki/simple-annotations

包简介

Simple annotation parser

README 文档

README

Installation

Get composer and learn to use it.

Library is on packagist.

If you refuse to use composer then instead of include_once "vendor/autoload.php" use include_once "src/DocBlockReader/Reader.php".

Test

You need PHPUnit. After you get it run:

> git clone https://github.com/jan-swiecki/php-simple-annotations
> cd php-simple-annotations
> composer install
> phpunit

Introduction

This library gives you the ability to extract and auto-parse DocBlock comment blocks.

Example:

class TestClass {
  /**
   * @x 1
   * @y yes!
   */
  private $myVar;
}

$reader = new \DocBlockReader\Reader('TestClass', 'myVar', 'property');
$x = $reader->getParameter("x"); // 1 (with number type)
$y = $reader->getParameter("y"); // "yes!" (with string type)

So as you can see to do this you need to construct Reader object and target it at something. Then you extract data.

You can point at classes, class methods and class properties.

  • Targeting class: $reader = new \DocBlockReader\Reader(String $className)
  • Targeting method or property: $reader = new \DocBlockReader\Reader(String $className, String $name [, String $type = 'method'])

This will initialize DocBlock Reader on method $className::$name or property $className::$name.

To choose method use only two arguments or provide third argument as method string value. To get property value put property string value in third argument.

To extract parsed properties you have two methods:

  • $reader->getParameter(String $key)

Returns DocBlock value of parameter $key. E.g.

<?php
class MyClass
{
    /**
     * @awesomeVariable "I am a string"
     */
    public function fn()
    {

    }
}

then

$reader = new \DocBlockReader\Reader('MyClass', 'fn');
$reader->getParameter("awesomeVariable")

will return string I am a string (without quotes).

  • $reader->getParameters()

returns array of all parameters (see examples below).

API

  • Constructor $reader = new \DocBlockReader\Reader(String $className [, String $name [, String $type = 'method'] ])

    Creates Reader pointing at class, class method or class property - based on provided arguments (see Introduction).

  • $reader->getParameter(String $key)

Returns value of parameter $key extracted from DocBlock.

  • $reader->getParameters()

returns array of all parameters (see examples below).

  • $reader->getVariableDeclarations() - See last example below.

Examples

Examples based on ReaderTest.php.

Note: DocBlock Reader converts type of values basing on the context (see below).

Type conversion example

<?php

include_once "vendor/autoload.php";

class MyClass
{
	/**
	 * @var0 1.5
	 * @var1 1
	 * @var2 "123"
	 * @var3 abc
	 * @var4 ["a", "b"]
	 * @var5 {"x": "y"}
	 * @var6 {"x": {"y": "z"}}
	 * @var7 {"x": {"y": ["z", "p"]}}
	 *
	 * @var8
	 * @var9 null
	 *
	 * @var10 true
	 * @var11 tRuE
	 * @var12 false
	 * @var13 null
	 * 
	 */
	private function MyMethod()
	{
	}
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print

array (size=14)
  'var0' => float 1.5
  'var1' => int 1
  'var2' => string '123' (length=3)
  'var3' => string 'abc' (length=3)
  'var4' => 
    array (size=2)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
  'var5' => 
    array (size=1)
      'x' => string 'y' (length=1)
  'var6' => 
    array (size=1)
      'x' => 
        array (size=1)
          'y' => string 'z' (length=1)
  'var7' => 
    array (size=1)
      'x' => 
        array (size=1)
          'y' => 
            array (size=2)
              0 => string 'z' (length=1)
              1 => string 'p' (length=1)
  'var8' => boolean true
  'var9' => null
  'var10' => boolean true
  'var11' => boolean true
  'var12' => boolean false
  'var13' => null

Multi value example

<?php

include_once "vendor/autoload.php";

class MyClass
{
	/**
	 * @var x
	 * @var2 1024
	 * @param string x
	 * @param integer y
	 * @param array z
	 */
	private function MyMethod()
	{
	}
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print

array (size=3)
  'var' => string 'x' (length=1)
  'var2' => int 1024
  'param' => 
    array (size=3)
      0 => string 'string x' (length=8)
      1 => string 'integer y' (length=9)
      2 => string 'array z' (length=7)

Variables on the same line

<?php

include_once "vendor/autoload.php";

class MyClass
{
	/**
	 * @get @post
	 * @ajax
	 * @postParam x @postParam y
	 * @postParam z
	 */
	private function MyMethod()
	{
	}
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

will print

array (size=4)
  'get' => boolean true
  'post' => boolean true
  'ajax' => boolean true
  'postParam' => 
    array (size=3)
      0 => string 'x' (length=1)
      1 => string 'y' (length=1)
      2 => string 'z' (length=1)

Variable declarations functionality example

I found below functionality useful for filtering $_GET/$_POST data in CodeIgniter. Hopefully I will soon release my CodeIgniter's modification.

<?php

include_once "vendor/autoload.php";

class MyClass
{
	/**
	 * @param string var1
	 * @param integer var2
	 */
	private function MyMethod()
	{
	}
};

$reader = new DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getVariableDeclarations("param"));

will print

array (size=2)
  0 => 
    array (size=2)
      'type' => string 'string' (length=6)
      'name' => string 'var1' (length=4)
  1 => 
    array (size=2)
      'type' => string 'integer' (length=7)
      'name' => string 'var2' (length=4)

jan-swiecki/simple-annotations 适用场景与选型建议

jan-swiecki/simple-annotations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 636.01k 次下载、GitHub Stars 达 66, 最近一次更新时间为 2013 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「annotations」 「parser」 「annotation」 「docblock」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jan-swiecki/simple-annotations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jan-swiecki/simple-annotations 我们能提供哪些服务?
定制开发 / 二次开发

基于 jan-swiecki/simple-annotations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 636.01k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 68
  • 点击次数: 23
  • 依赖项目数: 23
  • 推荐数: 0

GitHub 信息

  • Stars: 66
  • Watchers: 4
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-04-13