霜之哀伤/php-simple-annotations

简单的注释解析器

0.5.2 2019-11-15 06:42 UTC

This package is not auto-updated.

Last update: 2024-09-24 04:23:34 UTC


README

安装

获取 composer 并学习如何使用它。

库在 packagist 上。

如果您拒绝使用 composer,则使用 include_once "vendor/autoload.php" 的替代方案是使用 include_once "src/DocBlockReader/Reader.php"

测试

您需要 PHPUnit。获取它后运行

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

简介

此库使您能够提取并自动解析 DocBlock 注释块。

示例

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

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

所以如您所见,要完成此操作,您需要构造 Reader 对象并将其指向某个对象。然后提取数据。

您可以将目标指向类、类方法和类属性。

  • 针对类: $reader = new \frostbane\DocBlockReader\Reader(String $className)
  • 针对方法或属性: $reader = new \frostbane\DocBlockReader\Reader(String $className, String $name [, String $type = 'method'])

这将初始化 DocBlock Reader 在方法 $className::$name 或属性 $className::$name 上。

要选择方法,只需提供两个参数,或提供第三个参数作为 method 字符串值。要获取属性值,请将 property 字符串值放在第三个参数中。

要提取解析的属性,您有两种方法

  • $reader->getParameter(String $key)

返回参数 $key 的 DocBlock 值。例如。

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

     }
 }

然后

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

将返回字符串 I am a string(不带引号)。

  • $reader->getParameters()

返回所有参数的数组(请参阅下面的示例)。

API

  • 构造函数 $reader = new \frostbane\DocBlockReader\Reader(String $className [, String $name [, String $type = 'method'] ])

    根据提供的参数创建指向类、类方法或类属性的 Reader(请参阅简介)。

  • $reader->getParameter(String $key)

返回从 DocBlock 中提取的 $key 参数的值。

  • $reader->getParameters()

返回所有参数的数组(请参阅下面的示例)。

  • $reader->getVariableDeclarations() - 请参阅下面的最后一个示例。

示例

基于 ReaderTest.php 的示例。

注意:DocBlock Reader 根据上下文转换值的类型(请参阅下面)。

类型转换示例

<?php

include_once "../vendor/autoload.php";

class MyClass
{
    /**
     * @float_0-0         0.0
     * @float_1-5         1.5
     * @int_1             1
     * @int_0             0
     * @string_2-3 "2.3"
     * @string_1   "1"
     * @string_0   "0"
     * @string_0-0 "0.0"
     * @string_123 "123"
     * @string_4-5 "4.5"
     *
     * @string_abc        abc
     * @string_def  "def"
     *
     * @array1 ["a", "b"]
     * @obj1 {"x": "y"}
     * @obj2 {"x": {"y": "z"}}
     * @obj_array1 {"x": {"y": ["z", "p"]}}
     *
     * @empty1
     * @null1             null
     * @string_null "null"
     *
     * @bool_true         true
     * @bool_false        false
     *
     * @string_tRuE       tRuE
     * @string_fAlSe      fAlSe
     * @string_true  "true"
     * @string_false "false"
     *
     */
    private function MyMethod()
    {
    }
}

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

var_dump($reader->getParameters());

将打印

array (size=25)
  'float_0-0'  float 
  'float_1-5'  float 
  'int_1'  int 
  'int_0'  int 
  'string_2-3'  string  (length=3)
  'string_1'  string  (length=1)
  'string_0'  string  (length=1)
  'string_0-0'  string  (length=3)
  'string_123'  string  (length=3)
  'string_4-5'  string  (length=3)
  'string_abc'  string  (length=3)
  'string_def'  string  (length=3)
  'array1' 
    array (size=2)
      0  string  (length=1)
      1  string  (length=1)
  'obj1' 
    array (size=1)
      'x'  string  (length=1)
  'obj2' 
    array (size=1)
      'x' 
        array (size=1)
          'y'  string  (length=1)
  'obj_array1' 
    array (size=1)
      'x' 
        array (size=1)
          'y' 
            array (size=2)
              ...
  'empty1'  boolean 
  'null1'  
  'string_null'  string  (length=4)
  'bool_true'  boolean 
  'bool_false'  boolean 
  'string_tRuE'  string  (length=4)
  'string_fAlSe'  string  (length=5)
  'string_true'  string  (length=4)
  'string_false'  string  (length=5)

多值示例

<?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 \frostbane\DocBlockReader\Reader("MyClass", "MyMethod");

var_dump($reader->getParameters());

将打印

array (size=3)
  'var'  string  (length=1)
  'var2'  int 
  'param' 
    array (size=3)
      0  string  (length=8)
      1  string  (length=9)
      2  string  (length=7)

同一行上的变量

<?php

include_once "vendor/autoload.php";

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

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

var_dump($reader->getParameters());

将打印

array (size=4)
  'get'  boolean 
  'post'  boolean 
  'ajax'  boolean 
  'postParam' 
    array (size=3)
      0  string  (length=1)
      1  string  (length=1)
      2  string  (length=1)

变量声明功能示例

我发现以下功能对于在 CodeIgniter 中过滤 $_GET/$_POST 数据非常有用。希望我很快会发布我的 CodeIgniter 修改版。

<?php

include_once "vendor/autoload.php";

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

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

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

将打印

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