-
-
Save kyleridolfo/d2382a18bca16b1a7bc225b9494f158c to your computer and use it in GitHub Desktop.
Laravel - tinker like a boss (with PsySH)
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 // ~/.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', | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In app's .tinker file: