Skip to content

Commit 68529e6

Browse files
authored
Actually use Config class (#71)
1 parent 41b3b2f commit 68529e6

4 files changed

Lines changed: 25 additions & 96 deletions

File tree

bin/generate_endpoint

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<?php
33
declare(strict_types=1);
44

5+
use Firehed\API\Config;
6+
57
$config = require 'load_config.php';
68

79
if ($argc < 2) {
@@ -64,23 +66,10 @@ php;
6466

6567
$class = sprintf(
6668
$template,
67-
$config['namespace'],
69+
$config->get(Config::KEY_NAMESPACE),
6870
$namespace ? '\\' . implode('\\', array_map('ucfirst', $namespace)) : '',
6971
ucfirst($className),
7072
implode('/', $defaultUrl)
7173
);
7274

7375
echo $class;
74-
75-
// For now, just echo the class skeleton and allow the user to handle it as
76-
// they want. A future version of this should intelligently resolve paths and
77-
// write the file directly.
78-
/*
79-
$targetFile = sprintf(
80-
'%s/%s/%s/%s.php',
81-
$config['source'],
82-
strtr($config['namespace'], '\\', '/'),
83-
implode('/', $namespace),
84-
$className
85-
);
86-
*/

bin/generate_endpoint_list

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
<?php
33
declare(strict_types=1);
44

5+
use Firehed\API\Config;
6+
57
$config = require 'load_config.php';
68

79
// Build out the endpoint map
810
(new Firehed\Common\ClassMapGenerator())
9-
->setPath(getcwd().DIRECTORY_SEPARATOR.$config['source'])
11+
->setPath(getcwd().DIRECTORY_SEPARATOR.$config->get(Config::KEY_SOURCE))
1012
->setInterface('Firehed\API\Interfaces\EndpointInterface')
1113
->addCategory('getMethod')
1214
->setMethod('getURI')
13-
->setNamespace($config['namespace'])
15+
->setNamespace($config->get(Config::KEY_NAMESPACE))
1416
->setOutputFile(getcwd().'/__endpoint_list__.json')
1517
->generate();
1618

bin/generate_front_controller

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
<?php
33
declare(strict_types=1);
44

5+
use Firehed\API\Config;
6+
57
$config = require 'load_config.php';
68

79
// Returns a series of '/..'s to get from the directory of the webroot to that
810
// of the project root
9-
function resolveProjectRoot(string $webroot): string {
11+
$resolveProjectRoot = function (string $webroot): string {
1012
$dirs = explode('/', $webroot);
1113
return str_repeat('/..', count($dirs));
12-
}
14+
};
1315

1416
$template = <<<'php'
1517
<?php
@@ -56,24 +58,26 @@ $response = (new Dispatcher())
5658
Firehed\API\ResponseRenderer::render($response);
5759
php;
5860

59-
$container = array_key_exists('container', $config)
60-
? sprintf(
61+
if ($config->has(Config::KEY_CONTAINER)) {
62+
$container = sprintf(
6163
'require \'%s\'',
6264
$config['container']
63-
)
64-
: 'null';
65+
);
66+
} else {
67+
$container = 'null';
68+
}
6569

6670
// TODO: write to file
6771
$fc = sprintf(
6872
$template,
69-
resolveProjectRoot($config['webroot']),
73+
$resolveProjectRoot($config->get(Config::KEY_WEBROOT)),
7074
$container
7175
);
7276

73-
$dir = $config['local_project_root'].'/'.$config['webroot'];
77+
$dir = $config->get(Config::KEY_WEBROOT);
7478
if (!file_exists($dir)) {
75-
fwrite(STDOUT, "Making '$dir' with 0755 permissions");
79+
fwrite(STDOUT, "Making '$dir' with 0755 permissions\n");
7680
mkdir($dir, 0755, true);
7781
}
7882
file_put_contents($dir.'/index.php', $fc);
79-
fwrite(STDOUT, "Wrote front controller to $dir/index.php");
83+
fwrite(STDOUT, "Wrote front controller to $dir/index.php\n");

bin/load_config.php

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,16 @@
11
<?php
22
declare(strict_types=1);
33

4+
use Firehed\API\Config;
45
use Psr\Container\ContainerInterface;
56

67
$root = __DIR__;
78
while (!file_exists($root.'/vendor/autoload.php') && $root != DIRECTORY_SEPARATOR) {
89
$root = dirname($root);
910
}
1011

11-
require $root.'/vendor/autoload.php';
12+
chdir($root);
1213

13-
$file = '/.apiconfig';
14-
$config_file = $root.$file;
14+
require_once 'vendor/autoload.php';
1515

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

Comments
 (0)