2022-04-06 17:19:01 +02:00
|
|
|
#!/usr/bin/keyhelp-php81
|
2022-01-26 15:16:46 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-07-20 19:09:40 +02:00
|
|
|
|
2022-01-25 20:29:19 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-01-31 21:02:32 +01:00
|
|
|
// & ~E_DEPRECATED is needed because of a bug in PhpStorm
|
2022-02-20 19:39:54 +01:00
|
|
|
use DI\DependencyException;
|
|
|
|
use DI\NotFoundException;
|
2022-07-20 19:09:40 +02:00
|
|
|
use App\Controller\CLIController;
|
2022-02-20 19:39:54 +01:00
|
|
|
use Exception;
|
|
|
|
|
2022-01-31 21:02:32 +01:00
|
|
|
error_reporting(error_level: E_ALL & ~E_DEPRECATED);
|
2022-01-26 19:01:29 +01:00
|
|
|
|
2022-01-19 21:08:44 +01:00
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
exit;
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 13:48:15 +01:00
|
|
|
// version, store that somewhere else
|
|
|
|
$version = '0.0.1';
|
|
|
|
|
2022-01-22 17:08:05 +01:00
|
|
|
require dirname(path: __DIR__) . '/vendor/autoload.php';
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
$configFile = dirname(path: __DIR__) . "/config.json.local";
|
2022-02-12 19:39:05 +01:00
|
|
|
if (!file_exists(filename: $configFile)) {
|
2022-07-20 19:09:40 +02:00
|
|
|
$configFile = dirname(path: __DIR__) . "/config.json";
|
2022-02-12 19:39:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 15:16:46 +01:00
|
|
|
if (!file_exists(filename: $configFile)) {
|
2022-01-23 15:21:41 +01:00
|
|
|
echo 'Missing config file' . PHP_EOL;
|
|
|
|
if (confirm(message: 'Should I create a new config based on config.json.sample?')) {
|
|
|
|
copy(from: 'config.json.sample', to: 'config.json');
|
|
|
|
echo 'Config file has been generated. Adjust it to your needs, then proceed to database setup.' . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo 'You first have to setup the bindAPI. Bye.' . PHP_EOL;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-26 15:16:46 +01:00
|
|
|
$configJSON = file_get_contents(filename: $configFile);
|
2022-01-23 15:21:41 +01:00
|
|
|
|
2022-01-26 15:16:46 +01:00
|
|
|
if (!$config = json_decode(json: $configJSON, associative: true)) {
|
2022-01-23 15:21:41 +01:00
|
|
|
echo 'Error parsing the config file.' . PHP_EOL;
|
|
|
|
echo $configJSON;
|
|
|
|
}
|
2022-01-19 21:08:44 +01:00
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
$shortOpts = 'v::'; // version
|
|
|
|
$shortOpts .= "V::"; // verbose
|
2022-01-23 13:48:15 +01:00
|
|
|
$shortOpts .= "h::"; // help
|
|
|
|
|
|
|
|
$longOpts = [
|
2022-04-06 16:25:27 +02:00
|
|
|
'version::',
|
|
|
|
'verbose::',
|
|
|
|
'help::'
|
2022-01-23 13:48:15 +01:00
|
|
|
];
|
|
|
|
|
2022-01-26 15:16:46 +01:00
|
|
|
$options = getopt(short_options: $shortOpts, long_options: $longOpts, rest_index: $restIndex);
|
2022-01-23 13:48:15 +01:00
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options)) {
|
2022-01-23 13:48:15 +01:00
|
|
|
print("bindAPI version: $version" . PHP_EOL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options)) {
|
2022-01-23 13:48:15 +01:00
|
|
|
print("Help …" . PHP_EOL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
|
2022-01-23 13:48:15 +01:00
|
|
|
$config['verbose'] = true;
|
|
|
|
} else {
|
|
|
|
$config['verbose'] = false;
|
|
|
|
}
|
|
|
|
|
2022-01-26 15:16:46 +01:00
|
|
|
$arguments = array_slice(array: $argv, offset: $restIndex);
|
2022-01-23 13:48:15 +01:00
|
|
|
|
2022-02-20 19:39:54 +01:00
|
|
|
try {
|
2022-07-20 19:09:40 +02:00
|
|
|
$app = new CLIController(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
|
|
|
|
} catch (DependencyException|NotFoundException|Exception $e) {
|
|
|
|
echo $e->getMessage() . PHP_EOL;
|
2022-02-20 19:39:54 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-07-20 19:09:40 +02:00
|
|
|
$app->runCommand();
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-23 15:21:41 +01:00
|
|
|
/**
|
|
|
|
* @param String $message
|
|
|
|
* @param array $options
|
|
|
|
* @param string $default
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-07-20 19:09:40 +02:00
|
|
|
function confirm(string $message = 'Are you sure? ', array $options = ['y', 'n'], string $default = 'n'): bool
|
2022-01-23 15:21:41 +01:00
|
|
|
{
|
|
|
|
// first $options means true, any other false
|
|
|
|
echo $message, ' (';
|
|
|
|
$first = true;
|
|
|
|
foreach ($options as $option) {
|
|
|
|
// mark default
|
|
|
|
if ($option == $default) {
|
2022-01-26 15:16:46 +01:00
|
|
|
$option = strtoupper(string: $option);
|
2022-01-23 15:21:41 +01:00
|
|
|
}
|
|
|
|
if ($first) {
|
|
|
|
echo $option;
|
|
|
|
$first = false;
|
|
|
|
} else {
|
|
|
|
echo '/', $option;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo '): ';
|
|
|
|
|
2022-01-25 20:29:19 +01:00
|
|
|
$handle = fopen(filename: "php://stdin", mode: 'r');
|
2022-01-26 19:01:29 +01:00
|
|
|
$line = trim(string: fgetc(stream: $handle));
|
2022-01-26 15:16:46 +01:00
|
|
|
fclose(stream: $handle);
|
2022-01-23 15:21:41 +01:00
|
|
|
|
|
|
|
if ($line == '') {
|
|
|
|
// enter
|
|
|
|
$line = $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($line == $options[0]) {
|
|
|
|
$result = true;
|
|
|
|
} else {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|