dcasia/nova-inline-morph-to

get morphables from directory

cord opened this issue · 1 comments

cord commented

maybe something you can incorporate into the package.

Following a working solution to automatically list morphables from a given directory Nova/Metricables where the Nova Resources for the morphables are defined:

Helper function:

//https://stackoverflow.com/questions/31837075/laravel-get-list-of-models
function getClassesList($dir)
    {
        $classes = \File::allFiles($dir);
        foreach ($classes as $class) {
            $class->classname = str_replace(
                [app_path(), '/', '.php'],
                ['App', '\\', ''],
                $class->getRealPath()
            );
        }
        return $classes;
    }

then in resource:

$classlist = getClassesList(app_path('Nova/Metricables'));
$metricables = [];
foreach ($classlist as $c) {
  $metricables[] = $c->classname;
}

and the field definition

 InlineMorphTo::make('Metricable')->types($metricables)

maybe you can shortcut it into

 InlineMorphTo::make('Metricable')->typesFromDir(app_path('Nova/Metricables'))

I think this type of things can be easily implemented on the userland with helpers as on your example, it doesn't add much value to the package, having types being automatically discovered and it has some downsides, especially when it comes to refactoring, there are also some other issues like, how many levels deep it would look for files, what order should it display it, what if I want one of the files to be ignored.

That is an approach that I wouldn't personally use, as I think being explicit is better