-
-
Save calebporzio/cdf70bd390688646fda65490006eb0a6 to your computer and use it in GitHub Desktop.
| <?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, | |
| ], | |
| ]; |
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.
Just read your post and this is a nice addition.
Thanks man, great article and great gist.
My pleasure!
what about defining custom functions for use in tinker?
Should be as simple as defining a function in the config, but I don't remember off hand. Shouldn't be too difficult though with a little research into tinker config files. (looking particularly at the underlying package: PsySh)
I couldn't find a way to write simpler functions to include as new commands, unless you write at least an anonymous class - anything under commands must be an instance of Symfony\Component\Console\Command\Command:
'commands' => [
'« CMD_NAME »' => new class extends \Psy\Command\Command {
protected function configure() {
$this->setName('« CMD_NAME »')->setDescription('« CMD DESCRIPTION »');
}
protected function execute(InputInterface $input, OutputInterface $output) {
//do your thing here
}
},
]If this becomes a pattern on your config file, I guess a simple wrapper function could be created to streamline closures into command instances.
Great share! Thank you 👍