1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\Console\Command\Manager; |
4
|
|
|
|
5
|
|
|
use Buttress\Concrete\Console\Command\Argument\Parser; |
6
|
|
|
use Buttress\Concrete\Console\Command\Argument\Summary; |
7
|
|
|
use Buttress\Concrete\Exception\RuntimeException; |
8
|
|
|
use League\CLImate\Argument\Argument; |
9
|
|
|
use League\CLImate\Argument\Filter; |
10
|
|
|
use League\CLImate\Argument\Manager; |
11
|
|
|
use League\CLImate\CLImate; |
12
|
|
|
|
13
|
|
|
class CommandManager extends Manager |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
public function __construct($command = '') |
19
|
|
|
{ |
20
|
|
|
$this->command($command); |
21
|
|
|
|
22
|
|
|
$this->filter = new Filter(); |
23
|
|
|
$this->summary = new Summary(); |
24
|
|
|
$this->parser = new Parser(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the name of this command |
29
|
|
|
* @param string $name |
30
|
|
|
*/ |
31
|
|
|
public function command($name) |
32
|
|
|
{ |
33
|
|
|
$this->name = $name; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getCommand() |
37
|
|
|
{ |
38
|
|
|
return $this->name; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add an argument. |
43
|
|
|
* |
44
|
|
|
* @throws \Exception if $argument isn't an array or Argument object. |
45
|
|
|
* @param Argument|string|array $argument |
46
|
|
|
* @param $options |
47
|
|
|
*/ |
48
|
|
|
public function add($argument, array $options = []) |
49
|
|
|
{ |
50
|
|
|
if (is_array($argument)) { |
51
|
|
|
$this->addMany($argument); |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (is_string($argument)) { |
56
|
|
|
$argument = Argument::createFromArray($argument, $options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!($argument instanceof Argument)) { |
60
|
|
|
throw new RuntimeException('Please provide an argument name or object.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->arguments[$argument->name()] = $argument; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Output a script's usage statement. |
68
|
|
|
* |
69
|
|
|
* @param CLImate $climate |
70
|
|
|
* @param array $argv |
71
|
|
|
*/ |
72
|
|
|
public function usage(CLImate $climate, array $argv = null) |
73
|
|
|
{ |
74
|
|
|
$this->summary |
75
|
|
|
->setClimate($climate) |
76
|
|
|
->setDescription($this->description) |
77
|
|
|
->setCommand($this->name) |
78
|
|
|
->setFilter($this->filter, $this->all()) |
79
|
|
|
->output(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get a script's short statement. |
84
|
|
|
*/ |
85
|
|
|
public function shortUsage() |
86
|
|
|
{ |
87
|
|
|
return $this->summary |
|
|
|
|
88
|
|
|
->setDescription($this->description) |
89
|
|
|
->setCommand($this->name) |
90
|
|
|
->setFilter($this->filter, $this->all()) |
91
|
|
|
->summarize(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: