zhamao-framework/src/ZM/Annotation/AnnotationMap.php
2023-01-02 23:16:08 +08:00

64 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace ZM\Annotation;
use ZM\Annotation\Interfaces\Level;
/**
* 注解全局存取位置
*/
class AnnotationMap
{
/**
* 存取注解对象的列表key是注解类名value是该注解对应的数组
*
* @var array<string, array<AnnotationBase>>
* @internal
*/
public static array $_list = [];
/**
* 存取注解对象的三维列表key1是注解所在的类名key2是注解所在的方法名value是该方法标注的注解们数组
*
* @var array<string, array<string, array<AnnotationBase>>>
* @internal
*/
public static array $_map = [];
public static function loadAnnotationList(array $list): void
{
self::$_list = array_merge_recursive(self::$_list, $list);
}
public static function loadAnnotationMap(array $map): void
{
self::$_map = array_merge_recursive(self::$_map, $map);
}
/**
* @return AnnotationBase[]
*/
public static function getAnnotationList(string $class_name): array
{
return self::$_list[$class_name] ?? [];
}
/**
* 排序所有的注解
*/
public static function sortAnnotationList(): void
{
foreach (self::$_list as $class => $annotations) {
if (is_a($class, Level::class, true)) {
usort(self::$_list[$class], function ($a, $b) {
$left = $a->getLevel(); /** @phpstan-ignore-line */
$right = $b->getLevel(); /* @phpstan-ignore-line */
return $left > $right ? -1 : ($left == $right ? 0 : 1);
});
}
}
}
}