bindAPI/bin/console

121 lines
2.5 KiB
PHP
Executable File

#!/usr/bin/env php
<?php declare(strict_types=1);
namespace App\Controller;
// & ~E_DEPRECATED is needed because of a bug in PhpStorm
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
error_reporting(error_level: E_ALL & ~E_DEPRECATED);
if (php_sapi_name() !== 'cli') {
exit;
}
// version, store that somewhere else
$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';
$shortOpts = 'v::'; // version
$shortOpts = 'q::'; // version
$shortOpts .= "V::"; // verbose
$shortOpts .= "h::"; // help
$longOpts = [
'version::',
'quiet::',
'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: 'q', array: $options) || array_key_exists(key: 'quiet', array: $options)) {
$quiet = true;
} else {
$quiet = false;
}
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$verbose = true;
} else {
$verbose = false;
}
$arguments = array_slice(array: $argv, offset: $restIndex);
try {
$app = new BindAPI(verbose: $verbose, quiet: $quiet);
$app->runCommand(arguments: $arguments);
} catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL;
exit(1);
}
/**
* @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;
}