1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\WebTinker; |
4
|
|
|
|
5
|
|
|
use Psy\Shell; |
6
|
|
|
use Psy\Configuration; |
7
|
|
|
use Psy\ExecutionLoopClosure; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Foundation\Application; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Laravel\Tinker\ClassAliasAutoloader; |
12
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
13
|
|
|
|
14
|
|
|
class Tinker |
15
|
|
|
{ |
16
|
|
|
/** @var \Symfony\Component\Console\Output\BufferedOutput */ |
17
|
|
|
protected $output; |
18
|
|
|
|
19
|
|
|
/** @var \Psy\Shell */ |
20
|
|
|
protected $shell; |
21
|
|
|
|
22
|
|
|
public static function execute(string $phpCode): string |
23
|
|
|
{ |
24
|
|
|
return(new static())->run($phpCode); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->output = new BufferedOutput(); |
30
|
|
|
|
31
|
|
|
$this->shell = $this->createShell($this->output); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function run(string $phpCode): string |
35
|
|
|
{ |
36
|
|
|
$phpCode = $this->removeComments($phpCode); |
37
|
|
|
|
38
|
|
|
$this->shell->addInput($phpCode); |
39
|
|
|
|
40
|
|
|
$closure = new ExecutionLoopClosure($this->shell); |
41
|
|
|
|
42
|
|
|
$closure->execute(); |
43
|
|
|
|
44
|
|
|
return $this->cleanOutput($this->output->fetch()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function createShell(BufferedOutput $output): Shell |
48
|
|
|
{ |
49
|
|
|
$config = new Configuration([ |
50
|
|
|
'updateCheck' => 'never', |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
$config->getPresenter()->addCasters([ |
54
|
|
|
Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection', |
55
|
|
|
Model::class => 'Laravel\Tinker\TinkerCaster::castModel', |
56
|
|
|
Application::class => 'Laravel\Tinker\TinkerCaster::castApplication', |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$shell = new Shell($config); |
60
|
|
|
|
61
|
|
|
$shell->setOutput($output); |
62
|
|
|
|
63
|
|
|
$composerClassMap = base_path('vendor/composer/autoload_classmap.php'); |
64
|
|
|
|
65
|
|
|
if (file_exists($composerClassMap)) { |
66
|
|
|
ClassAliasAutoloader::register($shell, $composerClassMap); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $shell; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function removeComments(string $code): string |
73
|
|
|
{ |
74
|
|
|
$tokens = token_get_all("<?php\n".$code."?>"); |
75
|
|
|
$cleanCode = ''; |
76
|
|
|
foreach ($tokens as $token) { |
77
|
|
|
if (is_string($token)) { |
78
|
|
|
// simple 1-character token |
79
|
|
|
$cleanCode .= $token; |
80
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
// token array |
83
|
|
|
list($id, $text) = $token; |
84
|
|
|
|
85
|
|
|
switch ($id) { |
86
|
|
|
case T_COMMENT: |
87
|
|
|
case T_DOC_COMMENT: |
88
|
|
|
case T_OPEN_TAG: |
89
|
|
|
case T_CLOSE_TAG: |
90
|
|
|
// no action on comments and php tags |
91
|
|
|
break; |
92
|
|
|
default: |
93
|
|
|
// anything else -> output "as is" |
94
|
|
|
$cleanCode .= $text; |
95
|
|
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $cleanCode; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function cleanOutput(string $output): string |
103
|
|
|
{ |
104
|
|
|
$output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit: Ctrl\+D/ms', '$2', $output); |
105
|
|
|
|
106
|
|
|
return trim($output); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|