This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleApp.php
95 lines (80 loc) · 2.44 KB
/
ConsoleApp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Lib;
use GetOpt\Command;
use GetOpt\GetOpt;
use GetOpt\Option;
use Invoker\InvokerInterface;
/**
* Integrates GetOpt and invokes the matched command with dependencies injected.
*
* @package Lib
*/
class ConsoleApp {
/**
* @var array the options currently used
*/
protected array $options = [];
/**
* @var array the operands currently used
*/
protected array $operands = [];
/**
* @var GetOpt
*/
protected GetOpt $get_opt;
/**
* ConsoleApp constructor.
*
* This constructor uses `constructor Injection` to get current `GetOpt` service
*
* @param \GetOpt\GetOpt $get_opt
*/
public function __construct(GetOpt $get_opt) {
$this->get_opt = $get_opt;
// add default help
$this->get_opt->addOption((new Option('h', 'help', Getopt::NO_ARGUMENT))->setDescription('Displays help with all commands.'));
}
/**
* Matching of the console request to a command, setting matched data and invoking it.
*
* @param \Invoker\InvokerInterface $invoker
*
* @throws \GetOpt\ArgumentException
* @throws \Invoker\Exception\InvocationException
* @throws \Invoker\Exception\NotCallableException
* @throws \Invoker\Exception\NotEnoughParametersException
*/
public function handle(InvokerInterface $invoker) {
// you can type-hint also for the ContainerInterface or Invoker anywhere else (which is called from an invoker)
$this->get_opt->process();
if($this->get_opt->getOption('h')) {
// handle default help
echo $this->get_opt->getHelpText();
exit;
}
$command = $this->get_opt->getCommand();
if(!$command || !$command->getHandler()) {
// no command or command handler
throw new \Exception('Console: no command match.');
}
$this->options = $this->get_opt->getOptions();
$this->operands = $this->get_opt->getOperands();
// Calling the matched handler with the DI enabled invoker
// add current command as optional receiver for only this handler
$invoker->call($command->getHandler(), [
Command::class => $command,
]);
}
/**
* @return array
*/
public function getOptions() {
return $this->options;
}
/**
* @return array
*/
public function getOperands() {
return $this->operands;
}
}