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