webman/app/command/ConfigMySQLCommand.php

47 lines
1.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ConfigMySQLCommand extends Command
{
protected static $defaultName = 'config:mysql';
protected static $defaultDescription = '显示当前MySQL服务器配置';
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('MySQL配置信息如下');
$config = config('database');
$headers = ['name', 'default', 'driver', 'host', 'port', 'database', 'username', 'password', 'unix_socket', 'charset', 'collation', 'prefix', 'strict', 'engine', 'schema', 'sslmode'];
$rows = [];
foreach ($config['connections'] as $name => $db_config) {
$row = [];
foreach ($headers as $key) {
switch ($key) {
case 'name':
$row[] = $name;
break;
case 'default':
$row[] = $config['default'] == $name ? 'true' : 'false';
break;
default:
$row[] = $db_config[$key] ?? '';
}
}
if ($config['default'] == $name) {
array_unshift($rows, $row);
} else {
$rows[] = $row;
}
}
$table = new Table($output);
$table->setHeaders($headers);
$table->setRows($rows);
$table->render();
return self::SUCCESS;
}
}