/
InfoCommand.php
171 lines (144 loc) · 4.74 KB
/
InfoCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation\Console;
use Flarum\Console\AbstractCommand;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application;
use Flarum\Foundation\Config;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Str;
use PDO;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle;
class InfoCommand extends AbstractCommand
{
/**
* @var ExtensionManager
*/
protected $extensions;
/**
* @var Config
*/
protected $config;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var ConnectionInterface
*/
protected $db;
/**
* @var Queue
*/
private $queue;
public function __construct(
ExtensionManager $extensions,
Config $config,
SettingsRepositoryInterface $settings,
ConnectionInterface $db,
Queue $queue
) {
$this->extensions = $extensions;
$this->config = $config;
$this->settings = $settings;
$this->db = $db;
$this->queue = $queue;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('info')
->setDescription("Gather information about Flarum's core and installed extensions");
}
/**
* {@inheritdoc}
*/
protected function fire()
{
$coreVersion = $this->findPackageVersion(__DIR__.'/../../../', Application::VERSION);
$this->output->writeln("<info>Flarum core $coreVersion</info>");
$this->output->writeln('<info>PHP version:</info> '.PHP_VERSION);
$this->output->writeln('<info>MySQL version:</info> '.$this->identifyDatabaseVersion());
$phpExtensions = implode(', ', get_loaded_extensions());
$this->output->writeln("<info>Loaded extensions:</info> $phpExtensions");
$this->getExtensionTable()->render();
$this->output->writeln('<info>Base URL:</info> '.$this->config->url());
$this->output->writeln('<info>Installation path:</info> '.getcwd());
$this->output->writeln('<info>Queue driver:</info> '.$this->identifyQueueDriver());
$this->output->writeln('<info>Mail driver:</info> '.$this->settings->get('mail_driver', 'unknown'));
$this->output->writeln('<info>Debug mode:</info> '.($this->config->inDebugMode() ? '<error>ON</error>' : 'off'));
if ($this->config->inDebugMode()) {
$this->output->writeln('');
$this->error(
"Don't forget to turn off debug mode! It should never be turned on in a production system."
);
}
}
private function getExtensionTable()
{
$table = (new Table($this->output))
->setHeaders([
['Flarum Extensions'],
['ID', 'Version', 'Commit']
])->setStyle(
(new TableStyle)->setCellHeaderFormat('<info>%s</info>')
);
foreach ($this->extensions->getEnabledExtensions() as $extension) {
$table->addRow([
$extension->getId(),
$extension->getVersion(),
$this->findPackageVersion($extension->getPath())
]);
}
return $table;
}
/**
* Try to detect a package's exact version.
*
* If the package seems to be a Git version, we extract the currently
* checked out commit using the command line.
*/
private function findPackageVersion(string $path, string $fallback = null): ?string
{
if (file_exists("$path/.git")) {
$cwd = getcwd();
chdir($path);
$output = [];
$status = null;
exec('git rev-parse HEAD 2>&1', $output, $status);
chdir($cwd);
if ($status == 0) {
return isset($fallback) ? "$fallback ($output[0])" : $output[0];
}
}
return $fallback;
}
private function identifyQueueDriver(): string
{
// Get class name
$queue = get_class($this->queue);
// Drop the namespace
$queue = Str::afterLast($queue, '\\');
// Lowercase the class name
$queue = strtolower($queue);
// Drop everything like queue SyncQueue, RedisQueue
$queue = str_replace('queue', '', $queue);
return $queue;
}
private function identifyDatabaseVersion(): string
{
return $this->db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
}
}