|
1 | 1 | <?php |
2 | 2 | declare(strict_types=1); |
3 | 3 |
|
| 4 | +use Firehed\API\Config; |
4 | 5 | use Psr\Container\ContainerInterface; |
5 | 6 |
|
6 | 7 | $root = __DIR__; |
7 | 8 | while (!file_exists($root.'/vendor/autoload.php') && $root != DIRECTORY_SEPARATOR) { |
8 | 9 | $root = dirname($root); |
9 | 10 | } |
10 | 11 |
|
11 | | -require $root.'/vendor/autoload.php'; |
| 12 | +chdir($root); |
12 | 13 |
|
13 | | -$file = '/.apiconfig'; |
14 | | -$config_file = $root.$file; |
| 14 | +require_once 'vendor/autoload.php'; |
15 | 15 |
|
16 | | -if (!file_exists($config_file) || !is_readable($config_file)) { |
17 | | - fwrite(STDERR, ".apiconfig file not found"); |
18 | | - exit(1); |
19 | | -} |
20 | | - |
21 | | -$config = json_decode(file_get_contents($config_file), true); |
22 | | - |
23 | | -if (JSON_ERROR_NONE !== json_last_error()) { |
24 | | - fwrite(STDERR, ".apiconfig contains invalid JSON"); |
25 | | - exit(1); |
26 | | -} |
27 | | - |
28 | | -$config = array_map(function ($val) { |
29 | | - return rtrim($val, '/'); |
30 | | -}, $config); |
31 | | - |
32 | | -$required_keys = [ |
33 | | - 'webroot', |
34 | | - 'namespace', |
35 | | - 'source', |
36 | | -]; |
37 | | -$optionalKeys = [ |
38 | | - 'container', |
39 | | -]; |
40 | | - |
41 | | -$allKeys = array_merge($required_keys, $optionalKeys); |
42 | | - |
43 | | -$keysInConfig = array_keys($config); |
44 | | - |
45 | | -if ($diff = array_diff($keysInConfig, $allKeys)) { |
46 | | - fwrite(STDERR, sprintf( |
47 | | - 'Found unexpected config keys in .apiconfig: %s', |
48 | | - implode(', ', $diff) |
49 | | - )); |
50 | | - exit(1); |
51 | | -} |
52 | | - |
53 | | -foreach ($required_keys as $required_key) { |
54 | | - if (!array_key_exists($required_key, $config)) { |
55 | | - fwrite(STDERR, ".apiconfig is missing value for '$required_key'"); |
56 | | - exit(1); |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -if (array_key_exists('container', $config)) { |
61 | | - $file = $config['container']; |
62 | | - if (!file_exists($file)) { |
63 | | - fwrite(STDERR, ".apiconfig[container] must point to a file returning a PSR-11 container"); |
64 | | - exit(1); |
65 | | - } |
66 | | - |
67 | | - // Require the container file in a closure to avoid any variable scope |
68 | | - // leaking into the current context. |
69 | | - $load = function (string $path): ContainerInterface { |
70 | | - return require $path; |
71 | | - }; |
72 | | - try { |
73 | | - $container = $load($config['container']); |
74 | | - } catch (TypeError $e) { |
75 | | - fwrite(STDERR, ".apiconfig[container] must point to a file returning a PSR-11 container"); |
76 | | - exit(1); |
77 | | - } |
78 | | -} |
79 | | - |
80 | | -$config['local_project_root'] = dirname($config_file); |
81 | | - |
82 | | -return $config; |
| 16 | +return Config::load('.apiconfig'); |
0 commit comments