Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

get morphables from directory #4

Closed
cord opened this issue Oct 3, 2019 · 1 comment
Closed

get morphables from directory #4

cord opened this issue Oct 3, 2019 · 1 comment

Comments

@cord
Copy link
Contributor

cord commented Oct 3, 2019

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'))
@milewski
Copy link
Member

milewski commented Oct 3, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants