1
|
|
|
<?php |
2
|
|
|
namespace Kunstmaan\Skylab\Command; |
3
|
|
|
|
4
|
|
|
use Cilex\Command\Command; |
5
|
|
|
use Kunstmaan\Skylab\Application; |
6
|
|
|
use Kunstmaan\Skylab\Exceptions\AccessDeniedException; |
7
|
|
|
use Kunstmaan\Skylab\Exceptions\SkylabException; |
8
|
|
|
use Kunstmaan\Skylab\Provider\UsesProviders; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractCommand extends Command |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
use UsesProviders; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param InputInterface $input |
20
|
|
|
* @param OutputInterface $output |
21
|
|
|
* @return int|null|void |
22
|
|
|
*/ |
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
// handle everything that is not an actual exception |
27
|
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) use ($input) { |
|
|
|
|
28
|
|
|
// error was suppressed with the @-operator |
29
|
|
|
if (0 === error_reporting()) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
$extra = array(); |
33
|
|
|
$tags = array(); |
34
|
|
|
$extra["full_command"] = implode(" ", $_SERVER['argv']); |
35
|
|
|
$arguments = $input->getArguments(); |
36
|
|
|
$tags["command"] = $arguments["command"]; |
37
|
|
|
$this->dialogProvider->logException(new SkylabException($errstr, 0, $errno, $errfile, $errline, null, array()), $tags, $extra); |
38
|
|
|
}, E_ALL); |
39
|
|
|
|
40
|
|
|
/** @var \Cilex\Application $app */ |
41
|
|
|
$app = $this->getContainer(); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$this->setup($app, $input, $output, true); |
45
|
|
|
$this->doPreExecute(); |
46
|
|
|
$this->doExecute(); |
47
|
|
|
$this->doPostExecute(); |
48
|
|
|
} catch (\Exception $ex){ |
49
|
|
|
$extra = array(); |
50
|
|
|
$tags = array(); |
51
|
|
|
$extra["full_command"] = implode(" ", $_SERVER['argv']); |
52
|
|
|
$arguments = $input->getArguments(); |
53
|
|
|
$tags["command"] = $arguments["command"]; |
54
|
|
|
$this->dialogProvider->logException($ex, $tags, $extra); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
abstract protected function doExecute(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
private function doPreExecute() |
|
|
|
|
67
|
|
|
{ |
68
|
|
View Code Duplication |
if (!$this->input->getOption('hideLogo')) { |
|
|
|
|
69
|
|
|
$this->dialogProvider->logo($this->output, OutputInterface::VERBOSITY_NORMAL, "Executing " . get_class($this)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->processProvider->executeCommand('sudo -p "Please enter your sudo password: " -v', true); |
73
|
|
|
|
74
|
|
|
if ('phar:' === substr(__FILE__, 0, 5) || getenv("SU")) { |
75
|
|
|
try { |
76
|
|
|
$json = $this->remoteProvider->curl('https://api.github.com/repos/kunstmaan/skylab/releases', null, null, 60); |
77
|
|
|
} catch (AccessDeniedException $e) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
$data = json_decode($json, true); |
81
|
|
|
|
82
|
|
|
usort($data, function ($a, $b) { |
83
|
|
|
return version_compare($a["tag_name"], $b["tag_name"]) * -1; |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
$latest = $data[0]; |
87
|
|
|
|
88
|
|
|
if ($this->getName() !== 'self-update' && version_compare(Application::VERSION, $latest["tag_name"]) < 0) { |
89
|
|
|
$this->dialogProvider->logWarning('Warning: There is a new release available of Skylab. It is recommended to update it by running "' . $_SERVER['PHP_SELF'] . ' self-update" to get the latest version.'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
*/ |
97
|
|
|
protected function doPostExecute() |
98
|
|
|
{ |
99
|
|
|
$this->dialogProvider->clearLine(); |
100
|
|
|
|
101
|
|
View Code Duplication |
if (!$this->input->getOption('hideLogo')) { |
|
|
|
|
102
|
|
|
$this->dialogProvider->logStatistics($this->output, OutputInterface::VERBOSITY_NORMAL, $this->app['skylab.starttime']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function addDefaults() |
110
|
|
|
{ |
111
|
|
|
$this |
112
|
|
|
->addOption("--hideLogo", null, InputOption::VALUE_NONE, 'If set, no logo or statistics will be shown') |
113
|
|
|
->addOption("--no-interactive", null, InputOption::VALUE_NONE, 'If set, no questions will be asked'); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
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: