Skip to content

Instantly share code, notes, and snippets.

@EDDYMENS
Last active August 15, 2023 07:08
Show Gist options
  • Save EDDYMENS/e3181395ba98ef330076e8b246ac539d to your computer and use it in GitHub Desktop.
Save EDDYMENS/e3181395ba98ef330076e8b246ac539d to your computer and use it in GitHub Desktop.
Script to reload Laravel Tinker sessions without existing
<?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
};
@EDDYMENS
Copy link
Author

EDDYMENS commented Aug 15, 2023

How to use

  • Include the tinkerMod.php file when starting your session i.e.: php artisan tinker tinkerMod.php
  • To restart your session and load code changes, call on the $_reload() function

How to re-run your test code

  • Once your session is loaded with tinkerMod.php type out the history or hist command to get the history of everything your typed out
  • Note the start and end line numbers you would like to run
  • You can now pass this into the reload function to reload your session and replay the desired history i.e.: $_reload(3,8)

Read more

I have put together a blog post explaining other things concerning the above script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment