1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Mouf\Utils\Patcher\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Mouf\Utils\Patcher\PatchService; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class AbstractApplyAllCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var PatchService |
17
|
|
|
*/ |
18
|
|
|
protected $patchService; |
19
|
|
|
|
20
|
|
|
public function __construct(PatchService $patchService) |
21
|
|
|
{ |
22
|
|
|
$this->patchService = $patchService; |
23
|
|
|
parent::__construct(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function registerOptions(): void |
27
|
|
|
{ |
28
|
|
|
foreach ($this->patchService->getTypes() as $type) { |
29
|
|
|
if ($type->getName() !== '') { |
|
|
|
|
30
|
|
|
$this->addOption($type->getName(), null, InputOption::VALUE_NONE, 'Applies patches of type "'.$type->getName().'". '.$type->getDescription()); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function applyAll(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$types = []; |
38
|
|
|
foreach ($this->patchService->getTypes() as $type) { |
39
|
|
|
if ($type->getName() !== '' && $input->getOption($type->getName())) { |
|
|
|
|
40
|
|
|
$types[] = $type->getName(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
|
46
|
|
|
[ |
47
|
|
|
'applied' => $appliedPatchArray, |
|
|
|
|
48
|
|
|
'skipped' => $skippedPatchArray |
|
|
|
|
49
|
|
|
] = $this->patchService->applyAll($types); |
50
|
|
|
|
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
$output->writeln(sprintf( |
53
|
|
|
'An error occurred while applying patch: <error>%s</error>', $e->getMessage() |
54
|
|
|
)); |
55
|
|
|
throw $e; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$msg = $this->getNotificationMessage($appliedPatchArray, $skippedPatchArray); |
59
|
|
|
if ($msg) { |
60
|
|
|
$output->writeln($msg); |
61
|
|
|
} else { |
62
|
|
|
$output->writeln('<info>No patches to apply</info>'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getNotificationMessage(array $appliedPatchArray, array $skippedPatchArray): string |
67
|
|
|
{ |
68
|
|
|
$nbPatchesApplied = array_sum($appliedPatchArray); |
69
|
|
|
$nbPatchesSkipped = array_sum($skippedPatchArray); |
70
|
|
|
$msg = ''; |
71
|
|
|
if ($nbPatchesApplied !== 0) { |
72
|
|
|
$patchArr = []; |
73
|
|
|
foreach ($appliedPatchArray as $name => $number) { |
74
|
|
|
$name = $name ?: 'default'; |
75
|
|
|
$patchArr[] = $name.': <info>'.$number.'</info>'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$msg .= sprintf('<info>%d</info> patch%s applied (%s)', $nbPatchesApplied, ($nbPatchesApplied > 1)?'es':'', implode(', ', $patchArr))."\n"; |
79
|
|
|
} |
80
|
|
|
if ($nbPatchesSkipped !== 0) { |
81
|
|
|
$patchArr = []; |
82
|
|
|
foreach ($skippedPatchArray as $name => $number) { |
83
|
|
|
$name = $name ?: 'default'; |
84
|
|
|
$patchArr[] = $name.': <info>'.$number.'</info>'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$msg .= sprintf('<info>%d</info><comment> patch%s skipped</comment> (%s)', $nbPatchesSkipped, ($nbPatchesSkipped > 1)?'es':'', implode(', ', $patchArr)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $msg; |
91
|
|
|
} |
92
|
|
|
} |