Last active
August 15, 2023 07:08
-
-
Save EDDYMENS/e3181395ba98ef330076e8b246ac539d to your computer and use it in GitHub Desktop.
Script to reload Laravel Tinker sessions without existing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Process; | |
$__tinkerModeDefinedVars__ = get_defined_vars(); | |
$_reload = function ($lineStart = null, $endLine = null) use ($__tinkerModeDefinedVars__) { | |
$histFile = '.__tinkerMode__history.php'; //stores executable history | |
$argsIsSet = ($lineStart !== null && $endLine !== null); | |
if ($argsIsSet) { | |
$oldShell = $__tinkerModeDefinedVars__['__psysh__']; | |
$oldShell->runCommand("hist --show $lineStart..$endLine --save $histFile"); //get history from old shell | |
//replace old executable history | |
$rawHistory = file_get_contents($histFile); | |
$formattedHistory = str_replace("\n", ';', $rawHistory); | |
file_put_contents($histFile, '<?php ' . $formattedHistory); | |
} | |
$tinkerMod = __DIR__ . '/tinkerMod.php'; | |
$histFile = (($argsIsSet) ? $histFile : ''); | |
Process::forever()->tty()->run("clear && php artisan tinker $tinkerMod $histFile"); //start a new shell | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use
tinkerMod.php
file when starting your session i.e.:php artisan tinker tinkerMod.php
$_reload()
functionHow to re-run your test code
tinkerMod.php
type out thehistory
orhist
command to get the history of everything your typed out$_reload(3,8)
Read more
I have put together a blog post explaining other things concerning the above script