1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 7.1 |
4
|
|
|
* |
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PhUml\Console\Commands; |
9
|
|
|
|
10
|
|
|
use Exception; |
11
|
|
|
use Humbug\SelfUpdate\Strategy\ShaStrategy; |
12
|
|
|
use Humbug\SelfUpdate\Updater; |
13
|
|
|
use PhUml\Console\UpdaterDisplay; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class SelfUpdateCommand extends Command |
20
|
|
|
{ |
21
|
|
|
const VERSION_URL = 'https://montealegreluis.com/phuml/phuml.phar.version'; |
22
|
|
|
const PHAR_URL = 'https://montealegreluis.com/phuml/phuml.phar'; |
23
|
|
|
|
24
|
|
|
/** @var Updater */ |
25
|
|
|
private $updater; |
26
|
|
|
|
27
|
|
|
/** @var UpdaterDisplay */ |
28
|
|
|
private $display; |
29
|
|
|
|
30
|
|
|
/** @throws \Symfony\Component\Console\Exception\LogicException */ |
31
|
|
|
public function __construct(Updater $updater = null, UpdaterDisplay $display = null) |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
$this->updater = $updater ?? new Updater(); |
35
|
|
|
$this->display = $display ?? new UpdaterDisplay(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
|
|
->setName('self-update') |
42
|
|
|
->setDescription('Update phuml.phar to most recent stable version.') |
43
|
|
|
->addOption( |
44
|
|
|
'rollback', |
45
|
|
|
'r', |
46
|
|
|
InputOption::VALUE_NONE, |
47
|
|
|
'Rollback to previous version of phUML if available on filesystem.' |
48
|
|
|
) |
49
|
|
|
->addOption( |
50
|
|
|
'check', |
51
|
|
|
'c', |
52
|
|
|
InputOption::VALUE_NONE, |
53
|
|
|
'Checks if there is an updated version available.' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
|
|
$this->configureUpdaterStrategy(); |
60
|
|
|
|
61
|
|
|
if ($input->getOption('rollback')) { |
62
|
|
|
$this->tryToRollback(); |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($input->getOption('check')) { |
67
|
|
|
$this->tryToCheckForUpdates(); |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->tryToUpdate($output); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function tryToRollback(): void |
75
|
|
|
{ |
76
|
|
|
$this->tryAction([$this, 'rollback']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function rollback(): void |
80
|
|
|
{ |
81
|
|
|
$result = $this->updater->rollback(); |
82
|
|
|
$this->display->rollbackMessage($result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function tryToCheckForUpdates(): void |
86
|
|
|
{ |
87
|
|
|
$this->display->currentLocalVersion($this->getApplication()->getVersion()); |
88
|
|
|
$this->tryAction([$this, 'showAvailableUpdates']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function showAvailableUpdates(): void |
92
|
|
|
{ |
93
|
|
|
if ($this->updater->hasUpdate()) { |
94
|
|
|
$this->display->newVersion($this->updater->getNewVersion()); |
95
|
|
|
} elseif (false === $this->updater->getNewVersion()) { |
96
|
|
|
$this->display->noUpdatesAvailable(); |
97
|
|
|
} else { |
98
|
|
|
$this->display->alreadyUpToDate(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function tryToUpdate(OutputInterface $output): void |
103
|
|
|
{ |
104
|
|
|
$output->writeln('Updating...' . PHP_EOL); |
105
|
|
|
$this->tryAction([$this, 'update']); |
106
|
|
|
$output->write(PHP_EOL); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function update(): void |
110
|
|
|
{ |
111
|
|
|
$result = $this->updater->update(); |
112
|
|
|
if ($result) { |
113
|
|
|
$this->display->updateApplied($this->updater->getOldVersion(), $this->updater->getNewVersion()); |
114
|
|
|
} else { |
115
|
|
|
$this->display->noUpdateApplied($this->updater->getOldVersion()); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function configureUpdaterStrategy(): void |
120
|
|
|
{ |
121
|
|
|
$strategy = new ShaStrategy(); |
122
|
|
|
$strategy->setPharUrl(self::PHAR_URL); |
123
|
|
|
$strategy->setVersionUrl(self::VERSION_URL); |
124
|
|
|
$this->updater->setStrategyObject($strategy); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function tryAction(callable $action): void |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
$action(); |
131
|
|
|
} catch (Exception $exception) { |
132
|
|
|
$this->display->error($exception); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|