跳转到内容

Phalcon注解

注意

所有类都以前缀命名Phalcon

注解\适配器\AbstractAdapterAbstract

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations\Adapter
  • 使用

    • Phalcon\Annotations\Collection
    • Phalcon\Annotations\Exception
    • Phalcon\Annotations\Reader
    • Phalcon\Annotations\ReaderInterface
    • Phalcon\Annotations\Reflection
  • 继承

  • 实现

    • AdapterInterface

这是Phalcon\Annotations适配器的基础类

属性

/**
 * @var array
 */
protected $annotations;

/**
 * @var Reader
 */
protected $reader;

方法

public function get( mixed $className ): Reflection;
解析或检索在类中找到的所有注解

public function getConstant( string $className, string $constantName ): Collection;
返回在特定常量中找到的注解

public function getConstants( string $className ): array;
返回在类的所有常量中找到的注解

public function getMethod( string $className, string $methodName ): Collection;
返回在特定方法中找到的注解

public function getMethods( string $className ): array;
返回在类的所有方法中找到的注解

public function getProperties( string $className ): array;
返回在类的所有属性中找到的注解

public function getProperty( string $className, string $propertyName ): Collection;
返回在特定属性中找到的注解

public function getReader(): ReaderInterface;
返回注解读取器

public function setReader( ReaderInterface $reader );
设置注解解析器

注解\适配器\AdapterInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations\Adapter
  • 使用

    • Phalcon\Annotations\Collection
    • Phalcon\Annotations\ReaderInterface
    • Phalcon\Annotations\Reflection
  • 继承

  • 实现

此接口必须由Phalcon\Annotations中的适配器实现

方法

public function get( string $className ): Reflection;
解析或检索在类中找到的所有注解

public function getConstant( string $className, string $constantName ): Collection;
返回在特定常量中找到的注解

public function getConstants( string $className ): array;
返回在类的所有常量中找到的注解

public function getMethod( string $className, string $methodName ): Collection;
返回在特定方法中找到的注解

public function getMethods( string $className ): array;
返回在类的所有方法中找到的注解

public function getProperties( string $className ): array;
返回在类的所有方法中找到的注解

public function getProperty( string $className, string $propertyName ): Collection;
返回在特定属性中找到的注解

public function getReader(): ReaderInterface;
返回注解读取器

public function setReader( ReaderInterface $reader );
设置注解解析器

注解\适配器\Apcu

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations\Adapter
  • 使用

    • Phalcon\Annotations\Reflection
  • 继承

    AbstractAdapter

  • 实现

将解析后的注解存储在APCu中。此适配器适用于生产环境

use Phalcon\Annotations\Adapter\Apcu;

$annotations = new Apcu();

属性

/**
 * @var string
 */
protected $prefix = ;

/**
 * @var int
 */
protected $ttl = 172800;

方法

public function __construct( array $options = [] );

public function read( string $key ): Reflection | bool;
从APCu读取解析后的注解

public function write( string $key, Reflection $data ): bool;
将解析后的注解写入APCu

注解\适配器\Memory

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations\Adapter
  • 使用

    • Phalcon\Annotations\Reflection
  • 继承

    AbstractAdapter

  • 实现

将解析后的注解存储在内存中。此适配器适用于开发/测试

属性

/**
 * @var mixed
 */
protected $data;

方法

public function __construct();

public function read( string $key ): Reflection | bool;
从内存中读取解析后的注解

public function write( string $key, Reflection $data ): void;
将解析后的注解写入内存

注解\适配器\Stream

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations\Adapter
  • 使用

    • Phalcon\Annotations\Exception
    • Phalcon\Annotations\Reflection
    • RuntimeException
  • 继承

    AbstractAdapter

  • 实现

将解析后的注解存储在文件中。此适配器适用于生产环境

use Phalcon\Annotations\Adapter\Stream;

$annotations = new Stream(
    [
        "annotationsDir" => "app/cache/annotations/",
    ]
);

属性

/**
 * @var string
 */
protected $annotationsDir = ./;

方法

public function __construct( array $options = [] );

public function read( string $key ): Reflection | bool | int;
从文件中读取解析后的注解

public function write( string $key, Reflection $data ): void;
将解析后的注解写入文件

注解\Annotation

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

  • 继承

  • 实现

表示注解集合中的单个注解

属性

/**
 * Annotation Arguments
 *
 * @var array
 */
protected $arguments;

/**
 * Annotation ExprArguments
 *
 * @var array
 */
protected $exprArguments;

/**
 * Annotation Name
 *
 * @var string|null
 */
protected $name;

方法

public function __construct( array $reflectionData );
Phalcon\Annotations\Annotation构造函数

public function getArgument( mixed $position ): mixed | null;
返回特定位置的参数

public function getArguments(): array;
返回表达式的参数

public function getExprArguments(): array;
返回未解析的表达式参数

public function getExpression( array $expr ): mixed;
解析注解表达式

public function getName(): null | string;
返回注解名称

public function getNamedArgument( string $name ): mixed | null;
返回命名参数

public function getNamedParameter( string $name ): mixed;
返回命名参数

public function hasArgument( mixed $position ): bool;
返回特定位置的参数

public function numberArguments(): int;
返回注解的参数数量

注解\AnnotationsFactory

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

    • Phalcon\Annotations\Adapter\AdapterInterface
    • Phalcon\Factory\AbstractFactory
    • Phalcon\Support\Helper\Arr\Get
  • 继承

    AbstractFactory

  • 实现

创建注解组件的工厂

方法

public function __construct( array $services = [] );
AdapterFactory构造函数。

public function load( mixed $config ): mixed;

public function newInstance( string $name, array $options = [] ): AdapterInterface;
创建适配器的新实例

protected function getExceptionClass(): string;

protected function getServices(): array;
返回可用的适配器

注解\Collection

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

    • Countable
    • Iterator
  • 继承

  • 实现

    • Countable
    • Iterator

表示注解集合。此类允许轻松遍历一组注解

// Traverse annotations
foreach ($classAnnotations as $annotation) {
    echo "Name=", $annotation->getName(), PHP_EOL;
}

// Check if the annotations has a specific
var_dump($classAnnotations->has("Cacheable"));

// Get an specific annotation in the collection
$annotation = $classAnnotations->get("Cacheable");

属性

/**
 * @var array
 */
protected $annotations;

/**
 * @var int
 */
protected $position = ;

方法

public function __construct( array $reflectionData = [] );
Phalcon\Annotations\Collection构造函数

public function count(): int;
返回集合中的注解数量

public function current(): mixed;
返回迭代器中的当前注解

public function get( string $name ): Annotation;
返回与名称匹配的第一个注解

public function getAll( string $name ): Annotation[];
返回与名称匹配的所有注解

public function getAnnotations(): Annotation[];
将内部注解作为数组返回

public function has( string $name ): bool;
检查集合中是否存在某个注解

public function key(): int;
返回迭代器中的当前位置/键

public function next(): void;
将内部迭代指针移动到下一个位置

public function rewind(): void;
重绕内部迭代器

public function valid(): bool;
检查迭代器中的当前注解是否有效

注解\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

  • 继承

    \Exception

  • 实现

Phalcon\Annotations抛出的异常类

注解\Reader

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

    • ReflectionClass
  • 继承

  • 实现

    • ReaderInterface

解析文档块,返回包含找到的注解的数组

方法

public function parse( string $className ): array;
从类文档块、其方法和/或属性中读取注解

public static function parseDocBlock( string $docBlock, mixed $file = null, mixed $line = null ): array;
解析原始文档块,返回找到的注解

注解\ReaderInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

  • 继承

  • 实现

解析文档块,返回包含找到的注解的数组

方法

public function parse( string $className ): array;
从类文档块、其常量、属性和方法中读取注解

public static function parseDocBlock( string $docBlock, mixed $file = null, mixed $line = null ): array;
解析原始文档块,返回找到的注解

注解\Reflection

GitHub上的源码

  • 命名空间

    • Phalcon\Annotations
  • 使用

  • 继承

  • 实现

允许以面向对象的方式操作注解反射

use Phalcon\Annotations\Reader;
use Phalcon\Annotations\Reflection;

// Parse the annotations in a class
$reader = new Reader();
$parsing = $reader->parse("MyComponent");

// Create the reflection
$reflection = new Reflection($parsing);

// Get the annotations in the class docblock
$classAnnotations = $reflection->getClassAnnotations();

属性

/**
 * @var Collection|null
 */
protected $classAnnotations;

/**
 * @var array
 */
protected $constantAnnotations;

/**
 * @var array
 */
protected $propertyAnnotations;

/**
 * @var array
 */
protected $methodAnnotations;

/**
 * @var array
 */
protected $reflectionData;

方法

public function __construct( array $reflectionData = [] );

public function getClassAnnotations(): Collection | null;
返回在类文档块中找到的注解

public function getConstantsAnnotations(): Collection[];
返回在常量文档块中找到的注解

public function getMethodsAnnotations(): Collection[];
返回在方法文档块中找到的注解

public function getPropertiesAnnotations(): Collection[];
返回在属性文档块中找到的注解

public function getReflectionData(): array;
返回用于构造反射的原始解析中间定义