1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Database\DatabaseManager; |
8
|
|
|
use Recca0120\Terminal\Contracts\WebCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
class Mysql extends Command implements WebCommand |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'mysql'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'mysql console'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* $connection. |
29
|
|
|
* |
30
|
|
|
* @var \Illuminate\Database\DatabaseManager |
31
|
|
|
*/ |
32
|
|
|
protected $databaseManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* __construct. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Database\DatabaseManager $databaseManager |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct(DatabaseManager $databaseManager) |
40
|
|
|
{ |
41
|
1 |
|
parent::__construct(); |
42
|
|
|
|
43
|
1 |
|
$this->databaseManager = $databaseManager; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Handle the command. |
48
|
|
|
* |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
1 |
|
public function handle() |
52
|
|
|
{ |
53
|
1 |
|
$query = $this->option('command'); |
54
|
1 |
|
$connection = $this->databaseManager->connection(); |
55
|
1 |
|
$rows = $this->castArray($connection->select($query)); |
|
|
|
|
56
|
1 |
|
$headers = array_keys(Arr::get($rows, 0, [])); |
57
|
1 |
|
$this->table($headers, $rows); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* castArray. |
62
|
|
|
* |
63
|
|
|
* @param stdClass[] $rows |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
protected function castArray($rows) |
67
|
|
|
{ |
68
|
1 |
|
return array_map(function ($row) { |
69
|
1 |
|
return (array) $row; |
70
|
1 |
|
}, $rows); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the console command options. |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
1 |
|
protected function getOptions() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
1 |
|
['command', null, InputOption::VALUE_OPTIONAL], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.