generated from spawnia/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Configuration.php
53 lines (41 loc) · 1.62 KB
/
Configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php declare(strict_types=1);
namespace Spawnia\Sailor;
final class Configuration
{
/** @var array<string, array<string, EndpointConfig>> */
public static array $configs;
private function __construct() {}
public static function endpoint(string $file, string $endpoint): EndpointConfig
{
$endpoints = self::endpoints($file);
if (! isset($endpoints[$endpoint])) {
throw ConfigurationException::missingEndpoint($endpoint);
}
return $endpoints[$endpoint];
}
/** @return array<string, \Spawnia\Sailor\EndpointConfig> */
public static function endpoints(string $file): array
{
return self::$configs[$file] ??= self::loadConfig($file);
}
public static function setEndpoint(string $file, string $name, EndpointConfig $endpointConfig): void
{
self::$configs[$file][$name] = $endpointConfig;
}
/** @param class-string<BelongsToEndpoint> $belongsToEndpoint */
public static function setEndpointFor(string $belongsToEndpoint, EndpointConfig $endpointConfig): void
{
self::$configs[$belongsToEndpoint::config()][$belongsToEndpoint::endpoint()] = $endpointConfig;
}
/** @return array<string, EndpointConfig> */
private static function loadConfig(string $file): array
{
if (! file_exists($file)) {
throw ConfigurationException::missingFile($file, false);
}
$config = require $file;
assert(is_array($config), "Expected configuration file {$file} to return array of EndpointConfig");
/** @var array<string, EndpointConfig> $config */
return $config;
}
}