Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Automatically alias Laravel Model's to their base classname.
* Ex: "App\Models\User" now can just be accessed by "User"
*/
if (! function_exists('aliasModels')) {
function aliasModels() {
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->name('*.php')->in(base_path().'/app');
foreach ($finder as $file) {
$namespace = 'App\\';
if ($relativePath = $file->getRelativePath()) {
$namespace .= strtr($relativePath, '/', '\\') . '\\';
}
$class = $namespace . $file->getBasename('.php');
try {
$r = new \ReflectionClass($class);
if ($r->isSubclassOf('Illuminate\\Database\\Eloquent\\Model')) {
class_alias($class, $file->getBasename('.php'));
}
} catch (Exception $e) {
//
}
}
}
}
aliasModels();
return [
'startupMessage' => '<info>Using local config file (tinker.config.php)</info>',
'commands' => [
// new \App\Tinker\TestCommand,
],
];
@freekmurze

This comment has been minimized.

Copy link

freekmurze commented May 10, 2017

Thanks for sharing this 👍

What does that \App\Tinker\TestCommand do?

@calebporzio

This comment has been minimized.

Copy link
Owner Author

calebporzio commented May 10, 2017

Ya, good call - that does nothing, I wanted to show that you can register custom commands from this config. I will comment it out so it doesn't break for others.

@unnikked

This comment has been minimized.

Copy link

unnikked commented May 10, 2017

Great share! Thank you 👍

@Ellrion

This comment has been minimized.

@freekmurze

This comment has been minimized.

Copy link

freekmurze commented May 20, 2017

Cleaned up version:

class ShortClassNames
{
    /** @var \Illuminate\Support\Collection */
    public $classes = [];

    public static function register()
    {
        (new static)->registerAutoloader();
    }

    public function __construct()
    {
        $classFiles = include(base_path('vendor/composer/autoload_classmap.php'));

        $this->classes = collect($classFiles)
            ->map(function (string $path, string $fqcn) {
                $name = last(explode('\\', $fqcn));

                return compact('fqcn', 'name');
            })
            ->filter()
            ->values();
    }

    public function registerAutoloader()
    {
        spl_autoload_register([$this, 'aliasClass']);
    }

    public function aliasClass($findClass)
    {
        $class = $this->classes->first(function ($class) use ($findClass) {
            return $class['name'] === $findClass;
        });

        if (! $class) {
            return;
        }

        class_alias($class['fqcn'], $class['name']);
    }
}

Usage: put this in config.tinker.php.

ShortClassNames::register();

Might add some options and package it soon.

@tristanbailey

This comment has been minimized.

Copy link

tristanbailey commented Sep 8, 2017

Just read your post and this is a nice addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.