Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Last active February 23, 2023 03:26
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jarektkaczyk/9a540a81ee7d40ce20c78c9f2197a1a9 to your computer and use it in GitHub Desktop.
Save jarektkaczyk/9a540a81ee7d40ce20c78c9f2197a1a9 to your computer and use it in GitHub Desktop.
Laravel - tinker like a boss (with PsySH)
<?php // ~/.config/psysh/config.php
// Anything not Laravel - let's try to autoload something likely to exist
if (!defined('LARAVEL_START')) {
return [
'defaultIncludes' => [
getcwd().'/vendor/autoload.php',
getcwd().'/bootstrap/autoload.php',
],
];
}
/*
|--------------------------------------------------------------------------
| Laravel magic begins
|--------------------------------------------------------------------------
*/
// Register toRawSql() macro which will get nice, full sql of the query (with bound values)
\Illuminate\Database\Query\Builder::macro('toRawSql', function () {
$bindings = array_map(function ($binding) {
return is_int($binding) || is_float($binding) ? $binding : "'{$binding}'";
}, $this->getBindings());
return vsprintf(str_replace('?', "%s", $this->toSql()), $bindings);
});
/*
|--------------------------------------------------------------------------
| Custom casters (presenters) and aliases registration for PsySH
|--------------------------------------------------------------------------
*/
_Tinker::alias('Carbon\Carbon');
class _Tinker
{
static function castQuery($query)
{
if ($query instanceof \Illuminate\Database\Eloquent\Builder) {
$query = $query->getQuery();
}
return [
'sql' => $query->toSql(),
'bindings' => $query->getBindings(),
'raw' => $query->toRawSql(),
];
}
static function register($path)
{
foreach (glob($path.'/*.php') as $file) {
$class = trim(app()->getNamespace(), '\\') . str_replace([app_path(), '/', '.php'], ['', '\\', ''], $file);
self::alias($class);
}
}
static function alias($class, $alias = null)
{
if (!class_exists($alias = $alias ?: class_basename($class)) && class_exists($class)) {
class_alias($class, $alias);
}
}
}
/*
|--------------------------------------------------------------------------
| Application HTTP requests helper
|--------------------------------------------------------------------------
*/
// Why extending TestCase here? Just because it's sooo easy and consistent across all L versions ;)
class _LocalRequest extends \TestCase
{
function __construct()
{
$this->setUp();
}
function response()
{
return $this->response;
}
function __call($method, $params)
{
return call_user_func_array([$this, $method], $params);
}
}
/*
|--------------------------------------------------------------------------
| Helper functions for common tasks
|--------------------------------------------------------------------------
*/
if (!function_exists('local')) {
function local($uri = null) { return $uri ? (new _LocalRequest)->get($uri) : new _LocalRequest; }
}
if (!function_exists('guzzle')) {
function guzzle($url = null) { return $url ? (new \GuzzleHttp\Client)->get($url) : new \GuzzleHttp\Client; }
}
if (!function_exists('http')) {
function http($url = null) { return guzzle($url); }
}
if (!function_exists('www')) {
function www($url = null) { return guzzle($url); }
}
if (!function_exists('now')) {
function now($timezone = null) { return \Carbon\Carbon::now($timezone); }
}
/*
|--------------------------------------------------------------------------
| Finally let's return PsySH config customizations
|--------------------------------------------------------------------------
*/
return [
'casters' => [
'Illuminate\Database\Eloquent\Builder' => '_Tinker::castQuery',
'Illuminate\Database\Query\Builder' => '_Tinker::castQuery',
],
'defaultIncludes' => [
getcwd().'/.tinker',
],
];
@ndberg
Copy link

ndberg commented Feb 25, 2017

Thanks for this config file. I've put in ~/.config/psysh/config.php.
Unfortunatelly I don't know how to get it to work. Do I have to call this file from somewhere in my laravel app, like config/app.php?

Thanks for your help.

@yarcowang
Copy link

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