Skip to content
This repository was archived by the owner on Aug 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Http/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public function handle($request, $next)

protected function allowedToUseTinker(): bool
{
if (!config('web-tinker.enabled')) {
return false;
}

return Gate::check('viewWebTinker');
return config('web-tinker.enabled') && Gate::check('viewWebTinker');
}
}
51 changes: 14 additions & 37 deletions src/LaravelWebTinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@

class LaravelWebTinker
{
/** @var BufferedOutput */
protected $output;
protected BufferedOutput $output;

/** @var Shell */
protected $shell;
protected Shell $shell;

/** @var OutputModifier */
protected $outputModifier;
protected OutputModifier $outputModifier;

public function __construct(OutputModifier $outputModifier)
{
Expand All @@ -36,11 +33,9 @@ public function __construct(OutputModifier $outputModifier)
public function execute(string $phpCode): string
{
$phpCode = $this->removeComments($phpCode);

$this->shell->addInput($phpCode);

$closure = new ExecutionLoopClosure($this->shell);

$runtime = Benchmark::measure(fn () => $closure->execute());
$output = $this->cleanOutput($this->output->fetch());

Expand All @@ -49,24 +44,22 @@ public function execute(string $phpCode): string

public function removeComments(string $code): string
{
$tokens = collect(token_get_all("<?php\n" . $code . '?>'));

return $tokens->reduce(function ($carry, $token) {
if (is_string($token)) {
return $carry . $token;
}

$text = $this->ignoreCommentsAndPhpTags($token);

return $carry . $text;
}, '');
return collect(token_get_all("<?php\n" . $code . '?>'))
->reduce(
callback: function ($carry, $token) {
return is_string($token)
? $carry . $token
: $carry . $this->ignoreCommentsAndPhpTags($token);
},
initial: ''
);
}

protected function createShell(BufferedOutput $output): Shell
{
$config = new Configuration([
'updateCheck' => 'never',
'configFile' => config('web-tinker.config_file') !== null ? base_path() . '/' . config('web-tinker.config_file') : null,
'configFile' => config('web-tinker.config_file') ? base_path(config('web-tinker.config_file')) : null,
]);

$config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null');
Expand All @@ -78,11 +71,9 @@ protected function createShell(BufferedOutput $output): Shell
]);

$shell = new Shell($config);

$shell->setOutput($output);

$composerClassMap = base_path('vendor/composer/autoload_classmap.php');

if (file_exists($composerClassMap)) {
ClassAliasAutoloader::register($shell, $composerClassMap, config('tinker.alias', []), config('tinker.dont_alias', []));
}
Expand All @@ -94,26 +85,12 @@ protected function ignoreCommentsAndPhpTags(array $token)
{
[$id, $text] = $token;

if ($id === T_COMMENT) {
return '';
}
if ($id === T_DOC_COMMENT) {
return '';
}
if ($id === T_OPEN_TAG) {
return '';
}
if ($id === T_CLOSE_TAG) {
return '';
}

return $text;
return in_array($id, [T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG]) ? '' : $text;
}

protected function cleanOutput(string $output): string
{
$output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit: {2}Ctrl\+D/ms', '$2', $output);

$output = preg_replace('/(?s)(<whisper.*?<\/whisper>)|INFO {2}Ctrl\+D\./ms', '$2', $output);

return trim($output);
Expand Down
7 changes: 6 additions & 1 deletion src/OutputModifiers/PrefixDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class PrefixDateTime implements OutputModifier
{
public function modify(string $output, float $runtime): string
{
return '<span style="color:rgba(255,255,255,0.2);font-style:italic">' . now()->format('Y-m-d H:i:s') . ' (runtime: ' . number_format($runtime / 1_000, 3) . 's)</span><br>' . $output;
$timestamp = now()->format('Y-m-d H:i:s');
$formattedRuntime = number_format($runtime / 1_000, 3);

return <<<HTML
<span style="color:rgba(255,255,255,0.2);font-style:italic">{$timestamp} (runtime: {$formattedRuntime}s)</span><br>{$output}
HTML;
}
}