|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
7
|
|
|
|
|
8
|
|
|
class ArtisanTinker extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The console command name. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $name = 'tinker'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command description. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'artisn tinker'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Handle the command. |
|
26
|
|
|
* |
|
27
|
6 |
|
* @throws \InvalidArgumentException |
|
28
|
|
|
*/ |
|
29
|
6 |
|
public function handle() |
|
30
|
|
|
{ |
|
31
|
6 |
|
$command = $this->option('command'); |
|
32
|
6 |
|
|
|
33
|
6 |
|
ob_start(); |
|
34
|
6 |
|
$result = $this->executeCode( |
|
35
|
6 |
|
trim(trim($command), ';').';' |
|
36
|
|
|
); |
|
37
|
6 |
|
$output = ob_get_clean(); |
|
38
|
2 |
|
|
|
39
|
2 |
|
if (empty($output) === false) { |
|
40
|
2 |
|
$this->line($output); |
|
41
|
|
|
$result = null; |
|
42
|
6 |
|
} |
|
43
|
6 |
|
|
|
44
|
6 |
|
$this->getOutput()->write('=> '); |
|
45
|
6 |
|
switch (gettype($result)) { |
|
46
|
2 |
|
case 'object': |
|
47
|
2 |
|
case 'array': |
|
48
|
4 |
|
$this->line(var_export($result, true)); |
|
49
|
1 |
|
break; |
|
50
|
1 |
|
case 'string': |
|
51
|
3 |
|
$this->comment($result); |
|
52
|
3 |
|
break; |
|
53
|
3 |
|
default: |
|
54
|
6 |
|
is_numeric($result) === true ? $this->info($result) : $this->line($result); |
|
55
|
6 |
|
break; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* executeCode. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $code |
|
63
|
6 |
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
6 |
|
protected function executeCode($code) |
|
66
|
6 |
|
{ |
|
67
|
4 |
|
$result = null; |
|
68
|
4 |
|
if (strpos($code, 'echo') === false && strpos($code, 'var_dump') === false) { |
|
69
|
|
|
$code = 'return '.$code; |
|
70
|
6 |
|
} |
|
71
|
6 |
|
|
|
72
|
6 |
|
$tmpfname = tempnam(sys_get_temp_dir(), 'artisan-thinker'); |
|
73
|
6 |
|
$handle = fopen($tmpfname, 'w+'); |
|
74
|
6 |
|
fwrite($handle, "<?php\n".$code); |
|
75
|
6 |
|
fclose($handle); |
|
76
|
|
|
$result = require $tmpfname; |
|
77
|
6 |
|
unlink($tmpfname); |
|
78
|
|
|
|
|
79
|
|
|
return $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the console command options. |
|
84
|
|
|
* |
|
85
|
6 |
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getOptions() |
|
88
|
6 |
|
{ |
|
89
|
6 |
|
return [ |
|
90
|
|
|
['command', null, InputOption::VALUE_OPTIONAL], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|