|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace { |
| 4 | + require_once __DIR__ . '/../vendor/autoload.php'; |
| 5 | +} |
| 6 | + |
| 7 | +namespace { |
| 8 | + use GetOpt\GetOpt; |
| 9 | + use GetOpt\Option; |
| 10 | + use GetOpt\Command; |
| 11 | + use GetOpt\ArgumentException; |
| 12 | + use GetOpt\ArgumentException\Missing; |
| 13 | + use Legalweb\MortgageSourceClientExample\Lib\Commands\GetClientToken; |
| 14 | + |
| 15 | + define('NAME', 'run'); |
| 16 | + define('VERSION', '1.0-alpha'); |
| 17 | + |
| 18 | + $opt = new GetOpt(); |
| 19 | + $opt->addOptions([ |
| 20 | + Option::create(null, 'version', GetOpt::NO_ARGUMENT)->setDescription("Show version information and quit"), |
| 21 | + Option::create('?', 'help', GetOpt::NO_ARGUMENT)->setDescription("Show this help and quit"), |
| 22 | + Option::create('f', 'config', GetOpt::OPTIONAL_ARGUMENT)->setValidation('is_readable')->setDescription("Specify configuration file to use"), |
| 23 | + Option::create('c', 'client', GetOpt::OPTIONAL_ARGUMENT)->setDescription("Provide client login if no config file provided"), |
| 24 | + Option::create('s', 'secret', GetOpt::OPTIONAL_ARGUMENT)->setDescription("Provide secret if no config file provided"), |
| 25 | + Option::create('e', 'endpoint', GetOpt::OPTIONAL_ARGUMENT)->setDescription("Provide endpoint if no config file provided"), |
| 26 | + ]); |
| 27 | + |
| 28 | + $opt->addCommand(Command::create('test-setup', function() { |
| 29 | + echo "When you see this message the setup works." . PHP_EOL; |
| 30 | + })->setDescription("Check if setup works")); |
| 31 | + |
| 32 | + $opt->addCommand(new GetClientToken()); |
| 33 | + |
| 34 | + try { |
| 35 | + try { |
| 36 | + $opt->process(); |
| 37 | + } catch(Missing $exception) { |
| 38 | + if ($opt->getOption('help')) { |
| 39 | + throw $exception; |
| 40 | + } |
| 41 | + } |
| 42 | + } catch(ArgumentException $exception) { |
| 43 | + file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL); |
| 44 | + echo PHP_EOL . $opt->getHelpText(); |
| 45 | + exit; |
| 46 | + } |
| 47 | + |
| 48 | + // show version and quit |
| 49 | + if ($opt->getOption('version')) { |
| 50 | + echo sprintf('%s: %s' . PHP_EOL, NAME, VERSION); |
| 51 | + exit; |
| 52 | + } |
| 53 | + |
| 54 | + // show help and quit |
| 55 | + $command = $opt->getCommand(); |
| 56 | + if (!$command || $opt->getOption('help')) { |
| 57 | + echo $opt->getHelpText(); |
| 58 | + exit; |
| 59 | + } |
| 60 | + |
| 61 | + // call the requested command |
| 62 | + call_user_func($command->getHandler(), $opt); |
| 63 | +} |
0 commit comments