1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kasifi\Gitaski\Command; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Humbug\SelfUpdate\Updater; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
|
14
|
|
|
class SelfUpdateCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* return void |
18
|
|
|
*/ |
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this |
22
|
|
|
->setName('self-update') |
23
|
|
|
->setDescription('Self update of Gitaski binary.'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param InputInterface $input |
28
|
|
|
* @param OutputInterface $output |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
$updater = new Updater(); |
37
|
|
|
$updater->getStrategy()->setPharUrl('http://lucascherifi.github.io/gitaski/gitaski.phar'); |
|
|
|
|
38
|
|
|
$updater->getStrategy()->setVersionUrl('http://lucascherifi.github.io/gitaski/gitaski.phar.version'); |
|
|
|
|
39
|
|
|
$io = new SymfonyStyle($input, $output); |
40
|
|
|
try { |
41
|
|
|
$result = $updater->update(); |
42
|
|
|
if (!$result) { |
43
|
|
|
$io = new SymfonyStyle($input, $output); |
44
|
|
|
$io->success('No update needed.'); |
45
|
|
|
exit(0); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
$new = $updater->getNewVersion(); |
48
|
|
|
$old = $updater->getOldVersion(); |
49
|
|
|
$io->success(sprintf('Updated from %s to %s', $old, $new)); |
50
|
|
|
exit(0); |
|
|
|
|
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
$io->error($e->getMessage()); |
53
|
|
|
exit(1); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
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: