1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console; |
4
|
|
|
|
5
|
|
|
use Yarak\Console\Output\SymfonyOutput; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
9
|
|
|
|
10
|
|
|
abstract class Command extends SymfonyCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Symfony console output. |
14
|
|
|
* |
15
|
|
|
* @var SymfonyOutput |
16
|
|
|
*/ |
17
|
|
|
protected $output; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Symfony input implementation. |
21
|
|
|
* |
22
|
|
|
* @var InputInterface |
23
|
|
|
*/ |
24
|
|
|
protected $input; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Execute the command. |
28
|
|
|
* |
29
|
|
|
* @param InputInterface $input |
30
|
|
|
* @param OutputInterface $output |
31
|
|
|
*/ |
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
$this->output = new SymfonyOutput($output); |
35
|
|
|
|
36
|
|
|
$this->input = $input; |
37
|
|
|
|
38
|
|
|
if (method_exists($this, 'handle')) { |
39
|
|
|
$this->handle(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the output implementation. |
45
|
|
|
* |
46
|
|
|
* @return SymfonyOutput |
47
|
|
|
*/ |
48
|
|
|
protected function getOutput() |
49
|
|
|
{ |
50
|
|
|
return $this->output; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the value of a command argument. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* |
58
|
|
|
* @return string|array |
59
|
|
|
*/ |
60
|
|
|
protected function argument($key = null) |
61
|
|
|
{ |
62
|
|
|
if (is_null($key)) { |
63
|
|
|
return $this->input->getArguments(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->input->getArgument($key); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Determine if the given argument is present. |
71
|
|
|
* |
72
|
|
|
* @param string|int $name |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function hasArgument($name) |
77
|
|
|
{ |
78
|
|
|
return $this->input->hasArgument($name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the value of a command option. |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* |
86
|
|
|
* @return string|array |
87
|
|
|
*/ |
88
|
|
|
protected function option($key = null) |
89
|
|
|
{ |
90
|
|
|
if (is_null($key)) { |
91
|
|
|
return $this->input->getOptions(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->input->getOption($key); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Determine if the given option is present. |
99
|
|
|
* |
100
|
|
|
* @param string $name |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
protected function hasOption($name) |
105
|
|
|
{ |
106
|
|
|
return $this->input->hasOption($name); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: