gabrielfs7 / annotation
一个用于解析注解的PHP库
1.0.0
2014-10-26 23:57 UTC
Requires
- php: >=5.5
Requires (Dev)
- phpmd/phpmd: *
- phpunit/phpunit: 4.0.x
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-09-27 00:39:39 UTC
README
创建和检索您自己的PHP类注解内容!
示例
<?php namespace GSoares\Annotation\Sample; /** * @package GSoares\Annotation\Sample * @myAnnotationBoolean(true) * @myAnnotationString('String name') * @myAnnotationString2(String name) * @myAnnotationNumber(123) * @myAnnotationNumber2(-123.84) * @myAnnotationNumber3(123.456) * @myAnnotationArray( * [ * 'a' => 'b', * 'c' => 'c', * [ * 'd' => ['e', 'f'] * ] * ] * ) * @myAnnotationArray2( * 'a' => 'b', * 'c' => 'c', * [ * 'd' => ['e', 'f'] * ] * ) */ class SampleClass { /** * @myAnnotationBoolean(true) * @myAnnotationString('String name') * @myAnnotationString2(String name) * @myAnnotationNumber(123) * @myAnnotationNumber2(-123.84) * @myAnnotationNumber3(123.456) * @myAnnotationArray( * [ * 'a' => 'b', * 'c' => 'c', * [ * 'd' => ['e', 'f'] * ] * ] * ) * @myAnnotationArray2( * 'a' => 'b', * 'c' => 'c', * [ * 'd' => ['e', 'f'] * ] * ) * @var teste */ private $mySampleProperty; /** * @myAnnotationBoolean(true) * @myAnnotationString('String name') * @myAnnotationString2(String name) * @myAnnotationNumber(123) * @myAnnotationNumber2(-123.84) * @myAnnotationNumber3(123.456) * @myAnnotationArray( * [ * 'a' => 'b', * 'c' => 'c', * [ * 'd' => ['e', 'f'] * ] * ] * ) * @myAnnotationArray2( * 'a' => 'b', * 'c' => 'c', * [ * 'd' => ['e', 'f'] * ] * ) */ public function mySampleMethod() { } } ?>
读取类注解
<?php $reader = new GSoares\Annotation\Reader(); $annotationBag = $reader->readClass('GSoares\Annotation\Sample\SampleClass'); $annotationBag->get('myAnnotationBoolean'); //(boolean) true $annotationBag->get('myAnnotationString'); //(string) String name $annotationBag->get('myAnnotationString2'); //(string) String name $annotationBag->get('myAnnotationNumber'); //(int) 123 $annotationBag->get('myAnnotationNumber2'); //(int) -123.84 $annotationBag->get('myAnnotationNumber3'); //(float) 123.456 $annotationBag->get('myAnnotationArray'); //(Array) ['a' => 'b', 'c' => 'c', ['d' => ['e', 'f']]] $annotationBag->get('myAnnotationArray2'); //(Array) ['a' => 'b', 'c' => 'c', ['d' => ['e', 'f']]] ?>
读取类属性注解
<?php $reader = new GSoares\Annotation\Reader(); $annotationBag = $reader->readProperty( 'GSoares\Annotation\Sample\SampleClass', 'mySampleProperty' ); $annotationBag->get('myAnnotationBoolean'); //(boolean) true $annotationBag->get('myAnnotationString'); //(string) String name $annotationBag->get('myAnnotationString2'); //(string) String name $annotationBag->get('myAnnotationNumber'); //(int) 123 $annotationBag->get('myAnnotationNumber2'); //(int) -123.84 $annotationBag->get('myAnnotationNumber3'); //(float) 123.456 $annotationBag->get('myAnnotationArray'); //(Array) ['a' => 'b', 'c' => 'c', ['d' => ['e', 'f']]] $annotationBag->get('myAnnotationArray2'); //(Array) ['a' => 'b', 'c' => 'c', ['d' => ['e', 'f']]] ?>
读取类方法注解
<?php $reader = new GSoares\Annotation\Reader(); $annotationBag = $reader->readMethod( 'GSoares\Annotation\Sample\SampleClass', 'mySampleMethod' ); $annotationBag->get('myAnnotationBoolean'); //(boolean) true $annotationBag->get('myAnnotationString'); //(string) String name $annotationBag->get('myAnnotationString2'); //(string) String name $annotationBag->get('myAnnotationNumber'); //(int) 123 $annotationBag->get('myAnnotationNumber2'); //(int) -123.84 $annotationBag->get('myAnnotationNumber3'); //(float) 123.456 $annotationBag->get('myAnnotationArray'); //(Array) ['a' => 'b', 'c' => 'c', ['d' => ['e', 'f']]] $annotationBag->get('myAnnotationArray2'); //(Array) ['a' => 'b', 'c' => 'c', ['d' => ['e', 'f']]] ?>
安装
- 该项目可在https://packagist.org.cn/packages/gabrielfs7/annotation上找到,可通过composer进行安装。