modified shebang behaviour

This commit is contained in:
tracer 2022-09-16 15:03:19 +02:00
parent 9716ad40d2
commit e12a226b4f
1 changed files with 20 additions and 37 deletions

57
bin/console Executable file → Normal file
View File

@ -1,5 +1,6 @@
#!/usr/bin/keyhelp-php81 #!/usr/bin/env php
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
// & ~E_DEPRECATED is needed because of a bug in PhpStorm // & ~E_DEPRECATED is needed because of a bug in PhpStorm
@ -13,36 +14,19 @@ if (php_sapi_name() !== 'cli') {
exit; exit;
} }
// version, store that somewhere else // version, store that somewhere else
$version = '0.0.1'; $version = '0.0.1';
if (!is_file(filename: dirname(path: __DIR__).'/vendor/autoload.php')) {
die('Required runtime components are missing. Try running "composer install".' . PHP_EOL);
}
require dirname(path: __DIR__) . '/vendor/autoload.php'; require dirname(path: __DIR__) . '/vendor/autoload.php';
$configFile = dirname(path: __DIR__) ."/config.json.local";
if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__) ."/config.json";
}
if (!file_exists(filename: $configFile)) { $shortOpts = 'v::'; // version
echo 'Missing config file' . PHP_EOL; $shortOpts .= "V::"; // verbose
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 $shortOpts .= "h::"; // help
$longOpts = [ $longOpts = [
@ -53,32 +37,31 @@ $longOpts = [
$options = getopt(short_options: $shortOpts, long_options: $longOpts, rest_index: $restIndex); $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) ) { if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options)) {
print("bindAPI version: $version" . PHP_EOL); print("bindAPI version: $version" . PHP_EOL);
exit(0); exit(0);
} }
if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options) ) { if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options)) {
print("Help …" . PHP_EOL); print("Help …" . PHP_EOL);
exit(0); exit(0);
} }
if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'verbose', array: $options) ) { if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$config['verbose'] = true; $verbose = true;
} else { } else {
$config['verbose'] = false; $verbose = false;
} }
$arguments = array_slice(array: $argv, offset: $restIndex); $arguments = array_slice(array: $argv, offset: $restIndex);
try { try {
$app = new BindAPI(config: $config, argumentsCount: count(value: $arguments), arguments: $arguments); $app = new BindAPI(verbose: $verbose );
$app->runCommand(); $app->runCommand(argumentsCount: count(value: $arguments), arguments: $arguments);
} catch (DependencyException|NotFoundException $e) {
echo $e->getMessage(); } catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1); exit(1);
} catch (Exception $e) {
echo $e->getMessage();
} }
@ -89,7 +72,7 @@ try {
* *
* @return bool * @return bool
*/ */
function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n'], string $default ='n'): bool function confirm(string $message = 'Are you sure? ', array $options = ['y', 'n'], string $default = 'n'): bool
{ {
// first $options means true, any other false // first $options means true, any other false
echo $message, ' ('; echo $message, ' (';