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
|
|
|
'configFile' => config('web-tinker.config_file') !== null ? base_path().'/'.config('web-tinker.config_file') : null, |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$config->getPresenter()->addCasters([ |
55
|
|
|
Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection', |
56
|
|
|
Model::class => 'Laravel\Tinker\TinkerCaster::castModel', |
57
|
|
|
Application::class => 'Laravel\Tinker\TinkerCaster::castApplication', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$shell = new Shell($config); |
61
|
|
|
|
62
|
|
|
$shell->setOutput($output); |
63
|
|
|
|
64
|
|
|
$composerClassMap = base_path('vendor/composer/autoload_classmap.php'); |
65
|
|
|
|
66
|
|
|
if (file_exists($composerClassMap)) { |
67
|
|
|
ClassAliasAutoloader::register($shell, $composerClassMap); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $shell; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function removeComments(string $code): string |
74
|
|
|
{ |
75
|
|
|
$tokens = collect(token_get_all("<?php\n".$code.'?>')); |
76
|
|
|
|
77
|
|
|
return $tokens->reduce(function ($carry, $token) { |
78
|
|
|
if (is_string($token)) { |
79
|
|
|
return $carry.$token; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$text = $this->ignoreCommentsAndPhpTags($token); |
83
|
|
|
|
84
|
|
|
return $carry.$text; |
85
|
|
|
}, ''); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function ignoreCommentsAndPhpTags(array $token) |
89
|
|
|
{ |
90
|
|
|
[$id, $text] = $token; |
|
|
|
|
91
|
|
|
|
92
|
|
|
if ($id === T_COMMENT) { |
93
|
|
|
return ''; |
94
|
|
|
} |
95
|
|
|
if ($id === T_DOC_COMMENT) { |
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
if ($id === T_OPEN_TAG) { |
99
|
|
|
return ''; |
100
|
|
|
} |
101
|
|
|
if ($id === T_CLOSE_TAG) { |
102
|
|
|
return ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $text; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function cleanOutput(string $output): string |
109
|
|
|
{ |
110
|
|
|
$output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit: Ctrl\+D/ms', '$2', $output); |
111
|
|
|
|
112
|
|
|
if (config('web-tinker.show_timestamp')) { |
113
|
|
|
$output = $this->getTimestamp().$output; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return trim($output); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function getTimestamp() |
120
|
|
|
{ |
121
|
|
|
return '<span class="text-dimmed">'.now().'</span><br>'; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.