1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dock\Cli; |
4
|
|
|
|
5
|
|
|
use Dock\Docker\Compose\Config; |
6
|
|
|
use Dock\Docker\Compose\NotWithinServiceException; |
7
|
|
|
use Dock\IO\ProcessRunner; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
14
|
|
|
|
15
|
|
|
class RunCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ProcessRunner |
19
|
|
|
*/ |
20
|
|
|
private $processRunner; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Config |
24
|
|
|
*/ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ProcessRunner $processRunner |
29
|
|
|
* @param Config $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ProcessRunner $processRunner, Config $config) |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
|
35
|
|
|
$this->processRunner = $processRunner; |
36
|
|
|
$this->config = $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function configure() |
43
|
|
|
{ |
44
|
|
|
$this |
45
|
|
|
->setName('run') |
46
|
|
|
->setDescription('Run a command on a service') |
47
|
|
|
->addOption( |
48
|
|
|
'service', |
49
|
|
|
's', |
50
|
|
|
InputOption::VALUE_REQUIRED, |
51
|
|
|
'Service to run the command on' |
52
|
|
|
) |
53
|
|
|
->addArgument( |
54
|
|
|
'service_command', |
55
|
|
|
InputArgument::IS_ARRAY, |
56
|
|
|
'Command to run on the current service' |
57
|
|
|
) |
58
|
|
|
; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
if ($input->getOption('service') !== null) { |
67
|
|
|
$service = $input->getOption('service'); |
68
|
|
|
} else { |
69
|
|
|
try { |
70
|
|
|
$service = $this->config->getCurrentService(); |
71
|
|
|
} catch (NotWithinServiceException $e) { |
72
|
|
|
$service = $this->askForTheService($input, $output); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$command = implode(' ', $input->getArgument('service_command')); |
77
|
|
|
$this->processRunner->run("docker-compose run $service $command"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function askForTheService(InputInterface $input, OutputInterface $output) |
81
|
|
|
{ |
82
|
|
|
$services = $this->config->getServices(); |
83
|
|
|
|
84
|
|
|
$question = new ChoiceQuestion( |
85
|
|
|
'Please select the service to run your command on', |
86
|
|
|
array_combine($services, $services) // work around a bug in symfony/console 2.7 |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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 implementation 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 interface: