1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Conveyor package. |
5
|
|
|
* |
6
|
|
|
* (c) Jeroen Fiege <[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 Webcreate\Conveyor\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
class UpdateCommand extends AbstractCommand |
19
|
|
|
{ |
20
|
|
|
protected function configure() |
21
|
|
|
{ |
22
|
|
|
$this |
23
|
|
|
->setName('update') |
24
|
|
|
->setDescription('Updates conveyor to the latest version') |
25
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force update of latest version') |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$dsn = 'http://conveyordeploy.com'; |
32
|
|
|
$latest = trim(file_get_contents(sprintf('%s/version', $dsn))); |
33
|
|
|
$current = trim(file_get_contents('phar://conveyor.phar/VERSION')); |
34
|
|
|
$update = version_compare($current, $latest, '<'); |
35
|
|
|
|
36
|
|
|
if ($update || $input->getOption('force')) { |
37
|
|
|
$remoteFile = sprintf('%s/conveyor.phar', $dsn); |
38
|
|
|
$localFile = $_SERVER['argv'][0]; |
39
|
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'conveyor') . '.phar'; |
40
|
|
|
|
41
|
|
|
file_put_contents($tempFile, file_get_contents($remoteFile)); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
chmod($tempFile, 0777 & ~umask()); |
45
|
|
|
// test the phar validity |
46
|
|
|
$phar = new \Phar($tempFile); |
47
|
|
|
// free the variable to unlock the file |
48
|
|
|
unset($phar); |
49
|
|
|
rename($tempFile, $localFile); |
50
|
|
|
|
51
|
|
|
$output->writeln(sprintf("<info>Conveyor has been updated to %s.</info>", $latest)); |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
@unlink($tempFile); |
|
|
|
|
54
|
|
|
if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { |
55
|
|
|
throw $e; |
56
|
|
|
} |
57
|
|
|
$output->writeln('<error>The download is corrupted ('.$e->getMessage().').</error>'); |
58
|
|
|
$output->writeln('<error>Please re-run the update command to try again.</error>'); |
59
|
|
|
} |
60
|
|
|
} else { |
61
|
|
|
$output->writeln("<info>You are using the latest version of Conveyor.</info>"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: