Skip to content

Instantly share code, notes, and snippets.

@absent1706
Last active November 14, 2016 15:03
Show Gist options
  • Save absent1706/6c0ff2f0a92f91496de5fa1b1229151d to your computer and use it in GitHub Desktop.
Save absent1706/6c0ff2f0a92f91496de5fa1b1229151d to your computer and use it in GitHub Desktop.
PsySH config file which teaches PsySH how to dump Eloquent model and collection (http://www.qopy.me/D6R2fYEQSw6wjV7NDaPMxw)
<?php
/*
PsySh config file (see http://psysh.org/#configure)
!!!
Copy this file to ~\.config\psysh\config.php, where ~ is your home dir
for example (Windows): C:\Users\MY_USER\.config\psysh\config.php
Result dump will look like http://www.qopy.me/D6R2fYEQSw6wjV7NDaPMxw
*/
return [
'casters' => [
// 'Illuminate\Support\Collection' => 'SomeCaster::castCollection',
// 'Illuminate\Database\Eloquent\Model' => 'SomeCaster::castModel',
'AppBundle\Entity\Base' => function ($obj) {
$skipProps = [];
if (strpos(get_class($obj), 'Proxies\__CG__') === 0) {
$skipProps[] = '__initializer__';
$skipProps[] = '__cloner__';
}
$reflect = new \ReflectionClass($obj);
$props = $reflect->getProperties();
$result = [];
foreach ($props as $prop) {
$name = $prop->name;
if (!in_array($name, $skipProps)) {
// if (strpos($name, '__') === false) {
$prop->setAccessible(true);
$result[$prop->name] = $prop->getValue($obj);
}
}
return $result;
}
],
];
<?php
/*
PsySh config file (see http://psysh.org/#configure)
!!!
Copy this file to ~\.config\psysh\config.php, where ~ is your home dir
for example (Windows): C:\Users\MY_USER\.config\psysh\config.php
Result dump will look like http://www.qopy.me/D6R2fYEQSw6wjV7NDaPMxw
*/
// copy of class Illuminate\Foundation\Console\IlluminateCaster
class IlluminateCaster {
const PREFIX_VIRTUAL = "\0~\0";
const PREFIX_PROTECTED = "\0*\0";
public static function castCollection($collection)
{
return [
self::PREFIX_VIRTUAL.'all' => $collection->all(),
];
}
public static function castModel($model)
{
$attributes = array_merge(
$model->getAttributes(), $model->getRelations()
);
$visible = array_flip(
$model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
);
$results = [];
foreach (array_intersect_key($attributes, $visible) as $key => $value) {
$results[(isset($visible[$key]) ? self::PREFIX_VIRTUAL : self::PREFIX_PROTECTED).$key] = $value;
}
return $results;
}
}
return [
'casters' => [
'Illuminate\Support\Collection' => 'IlluminateCaster::castCollection',
'Illuminate\Database\Eloquent\Model' => 'IlluminateCaster::castModel',
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment