Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Created January 30, 2019 23:56
Show Gist options
  • Save calebporzio/9d2c5b68dffd8ae1a30a620e4ddb5db7 to your computer and use it in GitHub Desktop.
Save calebporzio/9d2c5b68dffd8ae1a30a620e4ddb5db7 to your computer and use it in GitHub Desktop.
A quick, memorable way to initiate an "artisan tinker" session and play with variables.
<?php
function tinker(...$args) {
// Because there is no way of knowing what variable names
// the caller of this function used with the php run-time,
// we have to get clever. My solution is to peek at the
// stack trace, open up the file that called "tinker()"
// and parse out any variable names, so I can load
// them in the tinker shell and preserve their names.
$namedParams = collect(debug_backtrace())
->where('function', 'tinker')
->map(function ($slice) { return array_values($slice); })
->mapSpread(function ($filePath, $lineNumber, $function, $args) {
return file($filePath)[$lineNumber - 1];
// " tinker($post, new User);"
})->map(function ($carry) {
return str_before(str_after($carry, 'tinker('), ');');
// "$post, new User"
})->flatMap(function ($carry) {
return array_map('trim', explode(',', $carry));
// ["post", "new User"]
})->map(function ($carry, $index) {
return strpos($carry, '$') === 0
? str_after($carry, '$')
: 'temp'.$index;
// ["post", "temp1"]
})
->combine($args)->all();
// ["post" => $args[0], "temp1" => $args[1]]
echo PHP_EOL;
$sh = new \Psy\Shell();
$sh->setScopeVariables($namedParams);
if ($sh->has('ls')) {
$sh->addInput('ls', true);
}
$sh->run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment