2023-03-15 20:40:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\store;
|
|
|
|
|
|
|
|
|
|
use SPC\exception\FileSystemException;
|
2023-03-29 21:39:36 +08:00
|
|
|
use SPC\exception\WrongUsageException;
|
2023-03-15 20:40:49 +08:00
|
|
|
|
|
|
|
|
class Config
|
|
|
|
|
{
|
2024-02-18 13:54:06 +08:00
|
|
|
public static ?array $pkg = null;
|
|
|
|
|
|
2023-03-15 20:40:49 +08:00
|
|
|
public static ?array $source = null;
|
|
|
|
|
|
|
|
|
|
public static ?array $lib = null;
|
|
|
|
|
|
|
|
|
|
public static ?array $ext = null;
|
|
|
|
|
|
2024-07-07 20:45:18 +08:00
|
|
|
public static ?array $pre_built = null;
|
|
|
|
|
|
2025-03-30 16:53:14 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get pre-built configuration by name
|
|
|
|
|
*
|
|
|
|
|
* @param string $name The name of the pre-built configuration
|
|
|
|
|
* @return mixed The pre-built configuration or null if not found
|
2025-03-30 16:53:14 +08:00
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
2024-07-07 20:45:18 +08:00
|
|
|
public static function getPreBuilt(string $name): mixed
|
|
|
|
|
{
|
|
|
|
|
if (self::$pre_built === null) {
|
|
|
|
|
self::$pre_built = FileSystem::loadConfigArray('pre-built');
|
|
|
|
|
}
|
2025-03-30 20:34:57 +08:00
|
|
|
$supported_sys_based = ['match-pattern', 'prefer-stable', 'repo'];
|
2025-03-30 16:53:14 +08:00
|
|
|
if (in_array($name, $supported_sys_based)) {
|
|
|
|
|
$m_key = match (PHP_OS_FAMILY) {
|
|
|
|
|
'Windows' => ['-windows', '-win', ''],
|
|
|
|
|
'Darwin' => ['-macos', '-unix', ''],
|
|
|
|
|
'Linux' => ['-linux', '-unix', ''],
|
|
|
|
|
'BSD' => ['-freebsd', '-bsd', '-unix', ''],
|
|
|
|
|
default => throw new WrongUsageException('OS ' . PHP_OS_FAMILY . ' is not supported'),
|
|
|
|
|
};
|
|
|
|
|
foreach ($m_key as $v) {
|
|
|
|
|
if (isset(self::$pre_built["{$name}{$v}"])) {
|
|
|
|
|
return self::$pre_built["{$name}{$v}"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-07 20:45:18 +08:00
|
|
|
return self::$pre_built[$name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 20:40:49 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get source configuration by name
|
2023-03-15 20:40:49 +08:00
|
|
|
*
|
2025-07-29 11:08:53 +08:00
|
|
|
* @param string $name The name of the source
|
|
|
|
|
* @return null|array The source configuration or null if not found
|
2023-03-15 20:40:49 +08:00
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
public static function getSource(string $name): ?array
|
|
|
|
|
{
|
|
|
|
|
if (self::$source === null) {
|
|
|
|
|
self::$source = FileSystem::loadConfigArray('source');
|
|
|
|
|
}
|
|
|
|
|
return self::$source[$name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 13:54:06 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get package configuration by name
|
2024-02-18 13:54:06 +08:00
|
|
|
*
|
2025-07-29 11:08:53 +08:00
|
|
|
* @param string $name The name of the package
|
|
|
|
|
* @return null|array The package configuration or null if not found
|
2024-02-18 13:54:06 +08:00
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
public static function getPkg(string $name): ?array
|
|
|
|
|
{
|
|
|
|
|
if (self::$pkg === null) {
|
|
|
|
|
self::$pkg = FileSystem::loadConfigArray('pkg');
|
|
|
|
|
}
|
|
|
|
|
return self::$pkg[$name] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 20:40:49 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get library configuration by name and optional key
|
|
|
|
|
* Supports platform-specific configurations for different operating systems
|
2023-03-15 20:40:49 +08:00
|
|
|
*
|
2025-07-29 11:08:53 +08:00
|
|
|
* @param string $name The name of the library
|
|
|
|
|
* @param null|string $key The configuration key (static-libs, headers, lib-depends, lib-suggests, frameworks, bin)
|
|
|
|
|
* @param mixed $default Default value if key not found
|
|
|
|
|
* @return mixed The library configuration or default value
|
2023-03-15 20:40:49 +08:00
|
|
|
* @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
|
|
|
}
|
2025-03-08 14:29:44 +08:00
|
|
|
$supported_sys_based = ['static-libs', 'headers', 'lib-depends', 'lib-suggests', 'frameworks', 'bin'];
|
2023-03-15 20:40:49 +08:00
|
|
|
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-10-15 13:07:13 +08:00
|
|
|
'BSD' => ['-freebsd', '-bsd', '-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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get all library configurations
|
|
|
|
|
*
|
|
|
|
|
* @return array All library configurations
|
2023-03-15 20:40:49 +08:00
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
public static function getLibs(): array
|
|
|
|
|
{
|
|
|
|
|
if (self::$lib === null) {
|
|
|
|
|
self::$lib = FileSystem::loadConfigArray('lib');
|
|
|
|
|
}
|
|
|
|
|
return self::$lib;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-24 19:25:38 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get extension target configuration by name
|
|
|
|
|
*
|
|
|
|
|
* @param string $name The name of the extension
|
|
|
|
|
* @return null|array The extension target configuration or default ['static', 'shared']
|
2025-03-24 19:25:38 +08:00
|
|
|
* @throws WrongUsageException
|
|
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
|
|
|
|
public static function getExtTarget(string $name): ?array
|
|
|
|
|
{
|
|
|
|
|
if (self::$ext === null) {
|
|
|
|
|
self::$ext = FileSystem::loadConfigArray('ext');
|
|
|
|
|
}
|
|
|
|
|
if (!isset(self::$ext[$name])) {
|
|
|
|
|
throw new WrongUsageException('ext [' . $name . '] is not supported yet');
|
|
|
|
|
}
|
2025-04-18 14:38:42 +08:00
|
|
|
return self::$ext[$name]['target'] ?? ['static', 'shared'];
|
2025-03-24 19:25:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 20:40:49 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get extension configuration by name and optional key
|
|
|
|
|
* Supports platform-specific configurations for different operating systems
|
|
|
|
|
*
|
|
|
|
|
* @param string $name The name of the extension
|
|
|
|
|
* @param null|string $key The configuration key (lib-depends, lib-suggests, ext-depends, ext-suggests, arg-type)
|
|
|
|
|
* @param mixed $default Default value if key not found
|
|
|
|
|
* @return mixed The extension configuration or default value
|
2023-03-15 20:40:49 +08:00
|
|
|
* @throws FileSystemException
|
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-10-15 13:07:13 +08:00
|
|
|
'BSD' => ['-freebsd', '-bsd', '-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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get all extension configurations
|
|
|
|
|
*
|
|
|
|
|
* @return array All extension configurations
|
2023-03-15 20:40:49 +08:00
|
|
|
* @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
|
|
|
|
2023-08-20 19:51:45 +08:00
|
|
|
/**
|
2025-07-29 11:08:53 +08:00
|
|
|
* Get all source configurations
|
|
|
|
|
*
|
|
|
|
|
* @return array All source configurations
|
2023-08-20 19:51:45 +08:00
|
|
|
* @throws FileSystemException
|
|
|
|
|
*/
|
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
|
|
|
}
|