137 lines
4.2 KiB
PHP
Raw Normal View History

2023-03-15 20:40:49 +08:00
<?php
declare(strict_types=1);
namespace SPC\store;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
2023-03-29 21:39:36 +08:00
use SPC\exception\WrongUsageException;
2023-03-15 20:40:49 +08:00
/**
* 一个读取 config 配置的操作类
*/
class Config
{
public static ?array $source = null;
public static ?array $lib = null;
public static ?array $ext = null;
/**
* 从配置文件读取一个资源(source)的元信息
*
* @throws FileSystemException
*/
public static function getSource(string $name): ?array
{
if (self::$source === null) {
self::$source = FileSystem::loadConfigArray('source');
}
return self::$source[$name] ?? null;
}
/**
* 根据不同的操作系统分别选择不同的 lib 库依赖项
* 如果 key null,那么直接返回整个 meta。
* 如果 key 不为 null,则可以使用的 key static-libs、headers、lib-depends、lib-suggests。
* 对于 macOS 平台,支持 frameworks。
*
* @throws FileSystemException
2023-03-29 21:39:36 +08:00
* @throws WrongUsageException
2023-03-15 20:40:49 +08:00
*/
public static function getLib(string $name, ?string $key = null, mixed $default = null)
{
if (self::$lib === null) {
self::$lib = FileSystem::loadConfigArray('lib');
}
if (!isset(self::$lib[$name])) {
2023-03-29 21:39:36 +08:00
throw new WrongUsageException('lib [' . $name . '] is not supported yet');
2023-03-15 20:40:49 +08:00
}
$supported_sys_based = ['static-libs', 'headers', 'lib-depends', 'lib-suggests', 'frameworks'];
if ($key !== null && in_array($key, $supported_sys_based)) {
$m_key = match (PHP_OS_FAMILY) {
'Windows' => ['-windows', '-win', ''],
'Darwin' => ['-macos', '-unix', ''],
'Linux' => ['-linux', '-unix', ''],
2023-03-29 21:39:36 +08:00
default => throw new WrongUsageException('OS ' . PHP_OS_FAMILY . ' is not supported'),
2023-03-15 20:40:49 +08:00
};
foreach ($m_key as $v) {
if (isset(self::$lib[$name][$key . $v])) {
return self::$lib[$name][$key . $v];
}
}
return $default;
}
if ($key !== null) {
return self::$lib[$name][$key] ?? $default;
}
return self::$lib[$name];
}
/**
* @throws FileSystemException
*/
public static function getLibs(): array
{
if (self::$lib === null) {
self::$lib = FileSystem::loadConfigArray('lib');
}
return self::$lib;
}
/**
* @throws FileSystemException
* @throws RuntimeException
2023-03-29 21:39:36 +08:00
* @throws WrongUsageException
2023-03-15 20:40:49 +08:00
*/
public static function getExt(string $name, ?string $key = null, mixed $default = null)
{
if (self::$ext === null) {
self::$ext = FileSystem::loadConfigArray('ext');
}
if (!isset(self::$ext[$name])) {
2023-03-29 21:39:36 +08:00
throw new WrongUsageException('ext [' . $name . '] is not supported yet');
2023-03-15 20:40:49 +08:00
}
$supported_sys_based = ['lib-depends', 'lib-suggests', 'ext-depends', 'ext-suggests', 'arg-type'];
if ($key !== null && in_array($key, $supported_sys_based)) {
$m_key = match (PHP_OS_FAMILY) {
'Windows' => ['-windows', '-win', ''],
'Darwin' => ['-macos', '-unix', ''],
'Linux' => ['-linux', '-unix', ''],
2023-03-29 21:39:36 +08:00
default => throw new WrongUsageException('OS ' . PHP_OS_FAMILY . ' is not supported'),
2023-03-15 20:40:49 +08:00
};
foreach ($m_key as $v) {
if (isset(self::$ext[$name][$key . $v])) {
return self::$ext[$name][$key . $v];
}
}
return $default;
}
if ($key !== null) {
return self::$ext[$name][$key] ?? $default;
}
return self::$ext[$name];
}
/**
* @throws FileSystemException
*/
public static function getExts(): array
{
if (self::$ext === null) {
self::$ext = FileSystem::loadConfigArray('ext');
}
return self::$ext;
}
2023-04-30 12:42:19 +08:00
public static function getSources(): array
{
if (self::$source === null) {
self::$source = FileSystem::loadConfigArray('source');
}
return self::$source;
}
2023-03-15 20:40:49 +08:00
}