|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the humbug/php-scoper package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
|
9
|
|
|
* Pádraic Brady <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Console\Command; |
|
16
|
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\Logger\UpdateConsoleLogger; |
|
18
|
|
|
use Humbug\SelfUpdate\Updater; |
|
19
|
|
|
use Symfony\Component\Console\Command\Command; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
23
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
24
|
|
|
|
|
25
|
|
|
final class SelfUpdateCommand extends Command |
|
26
|
|
|
{ |
|
27
|
|
|
/** @internal */ |
|
28
|
|
|
const REMOTE_FILENAME = 'php-scoper.phar'; |
|
29
|
|
|
/** @internal */ |
|
30
|
|
|
const STABILITY_STABLE = 'stable'; |
|
31
|
|
|
/** @internal */ |
|
32
|
|
|
const PACKAGIST_PACKAGE_NAME = 'humbug/php-scoper'; |
|
33
|
|
|
/** @internal */ |
|
34
|
|
|
const ROLLBACK_OPT = 'rollback'; |
|
35
|
|
|
/** @internal */ |
|
36
|
|
|
const CHECK_OPT = 'check'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Updater |
|
40
|
|
|
*/ |
|
41
|
|
|
private $updater; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $version; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var UpdateConsoleLogger |
|
50
|
|
|
*/ |
|
51
|
|
|
private $logger; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param Updater $updater |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(Updater $updater) |
|
57
|
|
|
{ |
|
58
|
|
|
parent::__construct(); |
|
59
|
|
|
|
|
60
|
|
|
$this->updater = $updater; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function configure() |
|
67
|
|
|
{ |
|
68
|
|
|
$this |
|
69
|
|
|
->setName('self-update') |
|
70
|
|
|
->setDescription(sprintf( |
|
71
|
|
|
'Update %s to most recent stable build.', |
|
72
|
|
|
$this->getLocalPharName() |
|
73
|
|
|
)) |
|
74
|
|
|
->addOption( |
|
75
|
|
|
self::ROLLBACK_OPT, |
|
76
|
|
|
'r', |
|
77
|
|
|
InputOption::VALUE_NONE, |
|
78
|
|
|
'Rollback to previous version of PHP-Scoper if available on filesystem.' |
|
79
|
|
|
) |
|
80
|
|
|
->addOption( |
|
81
|
|
|
self::CHECK_OPT, |
|
82
|
|
|
'c', |
|
83
|
|
|
InputOption::VALUE_NONE, |
|
84
|
|
|
'Checks whether an update is available.' |
|
85
|
|
|
) |
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @inheritdoc |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->logger = new UpdateConsoleLogger( |
|
95
|
|
|
new SymfonyStyle($input, $output) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$this->version = $this->getApplication()->getVersion(); |
|
99
|
|
|
|
|
100
|
|
|
$this->configureUpdater(); |
|
101
|
|
|
|
|
102
|
|
|
if ($input->getOption('rollback')) { |
|
103
|
|
|
$this->rollback(); |
|
104
|
|
|
|
|
105
|
|
|
return 0; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($input->getOption('check')) { |
|
109
|
|
|
$this->printAvailableUpdates(); |
|
110
|
|
|
|
|
111
|
|
|
return 0; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->update(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function configureUpdater() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->updater->setStrategy(Updater::STRATEGY_GITHUB); |
|
120
|
|
|
$this->updater->getStrategy()->setPackageName(self::PACKAGIST_PACKAGE_NAME); |
|
121
|
|
|
$this->updater->getStrategy()->setPharName(self::REMOTE_FILENAME); |
|
122
|
|
|
$this->updater->getStrategy()->setCurrentLocalVersion($this->version); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function update() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->logger->startUpdating(); |
|
128
|
|
|
try { |
|
129
|
|
|
$result = $this->updater->update(); |
|
130
|
|
|
|
|
131
|
|
|
$newVersion = $this->updater->getNewVersion(); |
|
132
|
|
|
$oldVersion = $this->updater->getOldVersion(); |
|
133
|
|
|
|
|
134
|
|
|
if ($result) { |
|
135
|
|
|
$this->logger->updateSuccess($newVersion, $oldVersion); |
|
136
|
|
|
} else { |
|
137
|
|
|
$this->logger->updateNotNeeded($oldVersion); |
|
138
|
|
|
} |
|
139
|
|
|
} catch (\Throwable $e) { |
|
140
|
|
|
$this->logger->error($e); |
|
141
|
|
|
throw $e; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
private function rollback() |
|
146
|
|
|
{ |
|
147
|
|
|
try { |
|
148
|
|
|
$result = $this->updater->rollback(); |
|
149
|
|
|
if ($result) { |
|
150
|
|
|
$this->logger->rollbackSuccess(); |
|
151
|
|
|
} else { |
|
152
|
|
|
$this->logger->rollbackFail(); |
|
153
|
|
|
} |
|
154
|
|
|
} catch (\Throwable $e) { |
|
155
|
|
|
$this->logger->error($e); |
|
156
|
|
|
throw $e; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
private function printAvailableUpdates() |
|
161
|
|
|
{ |
|
162
|
|
|
$this->logger->printLocalVersion($this->version); |
|
163
|
|
|
$this->printCurrentStableVersion(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private function printCurrentStableVersion() |
|
167
|
|
|
{ |
|
168
|
|
|
$stability = self::STABILITY_STABLE; |
|
169
|
|
|
|
|
170
|
|
|
try { |
|
171
|
|
|
if ($this->updater->hasUpdate()) { |
|
172
|
|
|
$this->logger->printRemoteVersion( |
|
173
|
|
|
$stability, |
|
174
|
|
|
$updater->getNewVersion() |
|
|
|
|
|
|
175
|
|
|
); |
|
176
|
|
|
} elseif (false == $this->updater->getNewVersion()) { |
|
|
|
|
|
|
177
|
|
|
$this->logger->noNewRemoteVersions($stability); |
|
178
|
|
|
} else { |
|
179
|
|
|
$this->logger->currentVersionInstalled($stability); |
|
180
|
|
|
} |
|
181
|
|
|
} catch (\Throwable $e) { |
|
182
|
|
|
$this->logger->error($e); |
|
183
|
|
|
throw $e; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
private function getLocalPharName(): string |
|
188
|
|
|
{ |
|
189
|
|
|
return basename(\PHAR::running()); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.