|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padawan\Command; |
|
4
|
|
|
|
|
5
|
|
|
use DI\Container; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Exception\ExceptionInterface; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AsyncCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
|
14
|
|
|
{ |
|
15
|
|
|
// force the creation of the synopsis before the merge with the app definition |
|
16
|
|
|
$this->getSynopsis(true); |
|
17
|
|
|
$this->getSynopsis(false); |
|
18
|
|
|
|
|
19
|
|
|
// add the application arguments and options |
|
20
|
|
|
$this->mergeApplicationDefinition(); |
|
21
|
|
|
|
|
22
|
|
|
// bind the input against the command specific arguments/options |
|
23
|
|
|
try { |
|
24
|
|
|
$input->bind($this->getDefinition()); |
|
25
|
|
|
} catch (ExceptionInterface $e) { |
|
26
|
|
|
if (!$this->ignoreValidationErrors()) { |
|
27
|
|
|
throw $e; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->initialize($input, $output); |
|
32
|
|
|
|
|
33
|
|
|
// The command name argument is often omitted when a command is executed directly with its run() method. |
|
34
|
|
|
// It would fail the validation if we didn't make sure the command argument is present, |
|
35
|
|
|
// since it's required by the application. |
|
36
|
|
|
if ($input->hasArgument('command') && null === $input->getArgument('command')) { |
|
37
|
|
|
$input->setArgument('command', $this->getName()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$input->validate(); |
|
41
|
|
|
|
|
42
|
|
|
return $this->execute($input, $output); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return Container |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getContainer() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->getApplication()->getContainer(); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
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: