115 lines
2.7 KiB
PHP
Executable File
115 lines
2.7 KiB
PHP
Executable File
#!/usr/local/bin/php
|
|
<?php declare(strict_types=1);
|
|
namespace App\Controller;
|
|
//#!/usr/bin/keyhelp-php81
|
|
|
|
// & ~E_DEPRECATED is needed because of a bug in PhpStorm
|
|
error_reporting(error_level: E_ALL & ~E_DEPRECATED);
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
exit;
|
|
}
|
|
|
|
// version, store that somewhere else
|
|
$version = '0.0.1';
|
|
|
|
require dirname(path: __DIR__) . '/vendor/autoload.php';
|
|
|
|
$configFile = dirname(path: __DIR__) ."/config.json";
|
|
if (!file_exists(filename: $configFile)) {
|
|
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);
|
|
}
|
|
$configJSON = file_get_contents(filename: $configFile);
|
|
|
|
if (!$config = json_decode(json: $configJSON, associative: true)) {
|
|
echo 'Error parsing the config file.' . PHP_EOL;
|
|
echo $configJSON;
|
|
}
|
|
|
|
$shortOpts = 'v::'; // version
|
|
$shortOpts .= "V::"; // verbose
|
|
$shortOpts .= "h::"; // help
|
|
|
|
$longOpts = [
|
|
'version::',
|
|
'verbose::',
|
|
'help::'
|
|
];
|
|
|
|
$options = getopt(short_options: $shortOpts, long_options: $longOpts, rest_index: $restIndex);
|
|
|
|
if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options) ) {
|
|
print("bindAPI version: $version" . PHP_EOL);
|
|
exit(0);
|
|
}
|
|
|
|
if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options) ) {
|
|
print("Help …" . PHP_EOL);
|
|
exit(0);
|
|
}
|
|
|
|
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options) ) {
|
|
$config['verbose'] = true;
|
|
} else {
|
|
$config['verbose'] = false;
|
|
}
|
|
|
|
$arguments = array_slice(array: $argv, offset: $restIndex);
|
|
|
|
$app = new BindAPI(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments);
|
|
$app->runCommand();
|
|
|
|
|
|
/**
|
|
* @param String $message
|
|
* @param array $options
|
|
* @param string $default
|
|
*
|
|
* @return bool
|
|
*/
|
|
function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n'], string $default ='n'): bool
|
|
{
|
|
// first $options means true, any other false
|
|
echo $message, ' (';
|
|
$first = true;
|
|
foreach ($options as $option) {
|
|
// mark default
|
|
if ($option == $default) {
|
|
$option = strtoupper(string: $option);
|
|
}
|
|
if ($first) {
|
|
echo $option;
|
|
$first = false;
|
|
} else {
|
|
echo '/', $option;
|
|
}
|
|
}
|
|
echo '): ';
|
|
|
|
$handle = fopen(filename: "php://stdin", mode: 'r');
|
|
$line = trim(string: fgetc(stream: $handle));
|
|
fclose(stream: $handle);
|
|
|
|
if ($line == '') {
|
|
// enter
|
|
$line = $default;
|
|
}
|
|
|
|
if ($line == $options[0]) {
|
|
$result = true;
|
|
} else {
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|