1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\DatabaseManager; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class Mysql extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The console command name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'mysql'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'mysql console'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* $connection. |
27
|
|
|
* |
28
|
|
|
* @var \Illuminate\Database\DatabaseManager |
29
|
|
|
*/ |
30
|
|
|
protected $databaseManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* __construct. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Database\DatabaseManager $databaseManager |
36
|
|
|
*/ |
37
|
|
|
public function __construct(DatabaseManager $databaseManager) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
$this->databaseManager = $databaseManager; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handle the command. |
46
|
|
|
* |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function handle() |
50
|
|
|
{ |
51
|
|
|
$sql = $this->option('command'); |
52
|
|
|
$connection = $this->databaseManager->connection($this->option('connection')); |
53
|
|
|
$rows = $this->castArray($connection->select($sql, [], true)); |
54
|
|
|
$headers = array_keys(Arr::get($rows, 0, [])); |
55
|
|
|
$this->table($headers, $rows); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* castArray. |
60
|
|
|
* |
61
|
|
|
* @param stdClass[] $rows |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
protected function castArray($rows) |
65
|
|
|
{ |
66
|
|
|
return array_map(function ($row) { |
67
|
|
|
return (array) $row; |
68
|
|
|
}, $rows); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the console command options. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function getOptions() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
['command', null, InputOption::VALUE_REQUIRED], |
80
|
|
|
['connection', null, InputOption::VALUE_OPTIONAL], |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|