zhamao-framework/src/ZM/Annotation/AnnotationMap.php

64 lines
1.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace ZM\Annotation;
2022-12-19 01:45:27 +08:00
use ZM\Annotation\Interfaces\Level;
/**
* 注解全局存取位置
*/
class AnnotationMap
{
/**
2022-08-13 17:00:29 +08:00
* 存取注解对象的列表key是注解类名value是该注解对应的数组
*
* @var array<string, array<AnnotationBase>>
* @internal
*/
public static array $_list = [];
/**
2022-08-13 17:00:29 +08:00
* 存取注解对象的三维列表key1是注解所在的类名key2是注解所在的方法名value是该方法标注的注解们数组
*
* @var array<string, array<string, array<AnnotationBase>>>
* @internal
*/
public static array $_map = [];
2023-01-02 23:16:08 +08:00
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);
}
/**
2023-01-02 23:16:08 +08:00
* @return AnnotationBase[]
*/
2023-01-02 23:16:08 +08:00
public static function getAnnotationList(string $class_name): array
2022-08-13 17:00:29 +08:00
{
2023-01-02 23:16:08 +08:00
return self::$_list[$class_name] ?? [];
2022-08-13 17:00:29 +08:00
}
2022-12-19 01:45:27 +08:00
/**
* 排序所有的注解
*/
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);
});
}
}
}
}