1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of CacheTool. |
5
|
|
|
* |
6
|
|
|
* (c) Samuel Gordalina <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CacheTool\Command; |
13
|
|
|
|
14
|
|
|
use CacheTool\Util\ManifestUpdateStrategy; |
15
|
|
|
use Humbug\SelfUpdate\Updater; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class SelfUpdateCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
21 |
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
21 |
|
->setName('self-update') |
29
|
21 |
|
->setAliases(['selfupdate']) |
30
|
21 |
|
->setDescription('Updates cachetool.phar to the latest version') |
31
|
|
|
; |
32
|
21 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
38
|
|
|
{ |
39
|
|
|
$updater = new Updater(null, false); |
40
|
|
|
$updater->setStrategy(Updater::STRATEGY_GITHUB); |
41
|
|
|
$updater->getStrategy()->setPackageName('gordalina/cachetool'); |
|
|
|
|
42
|
|
|
$updater->getStrategy()->setPharName('cachetool.phar'); |
|
|
|
|
43
|
|
|
$updater->getStrategy()->setCurrentLocalVersion('@package_version@'); |
|
|
|
|
44
|
|
|
|
45
|
|
|
if (!$updater->hasUpdate()) { |
46
|
|
|
$output->writeln(sprintf('You are already using the latest version: <info>%s</info>', $this->getApplication()->getVersion())); |
47
|
|
|
return 0; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$output->writeln(sprintf('Updating to version <info>%s</info>', $updater->getNewVersion())); |
52
|
|
|
|
53
|
|
|
if (!$updater->update()) { |
54
|
|
|
throw new Exception("Failed to update"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$output->writeln('<info>Updated successfully</info>'); |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$updater->rollback(); |
60
|
|
|
$output->writeln(sprintf('An error ocurred during the update process, rolled back to version <info>%s</info>', $updater->getOldVersion())); |
61
|
|
|
|
62
|
|
|
return 1; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return 0; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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: