|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dock\Doctor; |
|
4
|
|
|
|
|
5
|
|
|
use Dock\Installer\Installable; |
|
6
|
|
|
use Dock\IO\ProcessRunner; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
9
|
|
|
|
|
10
|
|
|
abstract class Task |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ProcessRunner |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $processRunner; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param OutputInterface $output |
|
19
|
|
|
* @param bool $dryRun |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract public function run(OutputInterface $output, $dryRun); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param OutputInterface $output |
|
25
|
|
|
* @param string $command Command to check whether a problem exists |
|
26
|
|
|
* @param string $workingMessage Output when the command passes |
|
27
|
|
|
* @param string $problem Problem description |
|
28
|
|
|
* @param string $suggestedSolution Suggested solution |
|
29
|
|
|
* @param Installable $installable Task to fix the problem |
|
30
|
|
|
* @param bool $dryRun Try to fix the problem? |
|
31
|
|
|
* |
|
32
|
|
|
* @throws CommandFailedException |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function handle(OutputInterface $output, $command, $workingMessage, $problem, $suggestedSolution, Installable $installable, $dryRun) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->testCommand($command)) { |
|
37
|
|
|
$output->writeLn(sprintf('<info>%s</info>', $workingMessage)); |
|
38
|
|
|
} else { |
|
39
|
|
|
if ($dryRun) { |
|
40
|
|
|
$output->writeLn(sprintf('- <error>%s</error>', $problem)); |
|
41
|
|
|
throw new CommandFailedException(sprintf( |
|
42
|
|
|
'Command %s failed. %s'.PHP_EOL.'%s', |
|
43
|
|
|
$command, $problem, $suggestedSolution |
|
44
|
|
|
)); |
|
45
|
|
|
} else { |
|
46
|
|
|
$output->writeLn(sprintf('- <error>%s, attempting to fix that!</error>', $problem)); |
|
47
|
|
|
$installable->run(); |
|
48
|
|
|
$this->handle($output, $command, $workingMessage, $problem, $suggestedSolution, $installable, true); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $command |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool Did the command succeed? |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function testCommand($command) |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
$this->processRunner->run($command); |
|
62
|
|
|
} catch (ProcessFailedException $e) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|