1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Commands; |
4
|
|
|
|
5
|
|
|
use Joomla\Controller\AbstractController; |
6
|
|
|
use Joomla\Database\DatabaseDriver; |
7
|
|
|
use Stats\CommandInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Update command |
11
|
|
|
* |
12
|
|
|
* @method \Stats\CliApplication getApplication() Get the application object. |
13
|
|
|
* @property-read \Stats\CliApplication $app Application object |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class UpdateCommand extends AbstractController implements CommandInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Execute the controller. |
21
|
|
|
* |
22
|
|
|
* @return boolean |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
|
|
public function execute() |
27
|
|
|
{ |
28
|
|
|
$this->getApplication()->outputTitle('Update Server'); |
29
|
|
|
|
30
|
|
|
$this->getApplication()->out('<info>Updating server to git HEAD</info>'); |
31
|
|
|
|
32
|
|
|
// Pull from remote repo |
33
|
|
|
$this->runCommand('cd ' . APPROOT . ' && git pull 2>&1'); |
34
|
|
|
|
35
|
|
|
$this->getApplication()->out('<info>Updating Composer resources</info>'); |
36
|
|
|
|
37
|
|
|
// Run Composer install |
38
|
|
|
$this->runCommand('cd ' . APPROOT . ' && composer install --no-dev -o 2>&1'); |
39
|
|
|
|
40
|
|
|
$this->getApplication()->out('<info>Update complete</info>'); |
41
|
|
|
|
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the command's description |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
* |
50
|
|
|
* @since 1.0 |
51
|
|
|
*/ |
52
|
|
|
public function getDescription() |
53
|
|
|
{ |
54
|
|
|
return 'Update the server to the current git HEAD.'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the command's title |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
* |
62
|
|
|
* @since 1.0 |
63
|
|
|
*/ |
64
|
|
|
public function getTitle() |
65
|
|
|
{ |
66
|
|
|
return 'Update Server'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Execute a command on the server. |
71
|
|
|
* |
72
|
|
|
* @param string $command The command to execute. |
73
|
|
|
* |
74
|
|
|
* @return string Return data from the command |
75
|
|
|
* |
76
|
|
|
* @since 1.0 |
77
|
|
|
* @throws \RuntimeException |
78
|
|
|
*/ |
79
|
|
|
private function runCommand($command) |
80
|
|
|
{ |
81
|
|
|
$lastLine = system($command, $status); |
82
|
|
|
|
83
|
|
|
if ($status) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
// Command exited with a status != 0 |
86
|
|
|
if ($lastLine) |
87
|
|
|
{ |
88
|
|
|
$this->getApplication()->out($lastLine); |
89
|
|
|
|
90
|
|
|
throw new \RuntimeException($lastLine); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->getApplication()->out('<error>An unknown error occurred</error>'); |
94
|
|
|
|
95
|
|
|
throw new \RuntimeException('An unknown error occurred'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $lastLine; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: