|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Tracker\Vendor\Laravel\Artisan; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
|
|
9
|
|
|
class Base extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The table helper set. |
|
13
|
|
|
* |
|
14
|
|
|
* @var \Symfony\Component\Console\Helper\TableHelper |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $table; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Display all messages. |
|
20
|
|
|
* |
|
21
|
|
|
* @param $type |
|
22
|
|
|
* @param $messages |
|
23
|
|
|
*/ |
|
24
|
|
|
public function displayMessages($type, $messages) |
|
25
|
|
|
{ |
|
26
|
|
|
foreach ($messages as $message) { |
|
27
|
|
|
$this->$type($message); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the console command arguments. |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getArguments() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
['query', InputArgument::IS_ARRAY, 'The SQL query to be executed'], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the console command options. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function getOptions() |
|
49
|
|
|
{ |
|
50
|
|
|
$baseOptions = [ |
|
51
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'], |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
return array_merge($baseOptions, isset($this->options) ? $this->options : []); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Display results. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $result |
|
61
|
|
|
* @param string $method |
|
62
|
|
|
*/ |
|
63
|
|
|
public function display($result, $method = 'info') |
|
64
|
|
|
{ |
|
65
|
|
|
if ($result) { |
|
66
|
|
|
if (is_array($result)) { |
|
67
|
|
|
$this->displayTable($result); |
|
68
|
|
|
} elseif (is_bool($result)) { |
|
69
|
|
|
$this->{$method}($result ? 'Statement executed sucessfully.' : 'And error ocurred while executing the statement.'); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->{$method}($result); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Display results in table format. |
|
78
|
|
|
* |
|
79
|
|
|
* @param $table |
|
80
|
|
|
*/ |
|
81
|
|
|
public function displayTable($table) |
|
82
|
|
|
{ |
|
83
|
|
|
$headers = $this->makeHeaders($table[0]); |
|
84
|
|
|
|
|
85
|
|
|
$rows = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($table as $row) { |
|
88
|
|
|
$rows[] = (array) $row; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->table = $this->getHelperSet()->get('table'); |
|
92
|
|
|
|
|
93
|
|
|
$this->table->setHeaders($headers)->setRows($rows); |
|
94
|
|
|
|
|
95
|
|
|
$this->table->render($this->getOutput()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Extract headers from result. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $items |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
private function makeHeaders($items) |
|
106
|
|
|
{ |
|
107
|
|
|
return array_keys((array) $items); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: