static-php-cli/src/SPC/util/CustomExt.php

40 lines
1.1 KiB
PHP
Raw Normal View History

2023-04-15 18:45:11 +08:00
<?php
declare(strict_types=1);
namespace SPC\util;
use SPC\builder\Extension;
use SPC\exception\FileSystemException;
use SPC\store\FileSystem;
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)]
class CustomExt
{
private static array $custom_ext_class = [];
2023-09-08 22:12:59 +08:00
public function __construct(protected string $ext_name) {}
2023-04-15 18:45:11 +08:00
/**
* Load all custom extension classes
*
* @throws \ReflectionException
* @throws FileSystemException
*/
public static function loadCustomExt(): void
{
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/builder/extension', 'SPC\\builder\\extension');
foreach ($classes as $class) {
$reflection = new \ReflectionClass($class);
foreach ($reflection->getAttributes(CustomExt::class) as $attribute) {
self::$custom_ext_class[$attribute->getArguments()[0]] = $class;
}
}
}
public static function getExtClass(string $ext_name): string
{
return self::$custom_ext_class[$ext_name] ?? Extension::class;
}
}