orchidsoftware / crud

Simplify the process of building CRUD (Create, Read, Update, Delete) functionality in Laravel using the features of Orchid.
https://orchid.software
MIT License
138 stars 34 forks source link

CRUD auto discovery for packages #33

Closed bilogic closed 3 years ago

bilogic commented 3 years ago

Hi,

I would like to add lines 78 to 92 into the BootCrudGenerator.php middleware for auto discovery. See https://github.com/bilogic/OrchidExamples/blob/265e5ea2f72d42ddedc45877cae8e549d6cc94c5/src/OrchidExamplesServiceProvider.php#L78

This will allow packages to define namespace and folders that BootCrudGenerator.php should also search/discover for

app()->packageOrchidResources = ['Bilogic\\OrchidExamples' => __DIR__];

This already works on my system but I will be happy to make changes if there is a better way to do this. Thank you.

tabuna commented 3 years ago

I'm not sure if we always need to register package resources. For example, when discovered is disabled or executed under certain conditions (For example, the package config). It seems to me that the best solution, for now, is that each package would register its dependencies on its own, as this will give more freedom.

public function boot(Arbitrator $arbitrator): void
{
    $arbitrator->resources([ ... ])
}

The only thing that would be nice to add is an event so that packages would register through it. Then their download was postponed until the moment when the user needs these resources.

bilogic commented 3 years ago

Oh, do you mean if I call $arbitrator->resources([ ... ]) in my package service provider, it will get registered? If yes, then certainly your method is better!

tabuna commented 3 years ago

Yes, it will be registered

bilogic commented 3 years ago

I can't add do public function boot(Arbitrator $arbitrator): void because my service provider extends from OrchidServiceProvider as I need the Dashboard to add routes.

So, I did this in boot(), but it does not get registered, i.e. the CRUD does not show up in the side menu.

$r = resolve(ResourceFinder::class)
    ->setNamespace('Bilogic\\OrchidExamples\\Orchid\\Resources')
    ->find(__DIR__ . '/Orchid/Resources');

resolve(Arbitrator::class)->resources($r);

Any idea why? Thank you.

tabuna commented 3 years ago

I use the same construction. Сheck that your finder actually found the files. And also that your user has the rights to the necessary resources

bilogic commented 3 years ago

I found the issue, I have to run it after it booted(). This is how it looks:

$this->app->booted(function () {
    $r = app(ResourceFinder::class)
        ->setNamespace('Bilogic\\OrchidExamples\\Orchid\\Resources')
        ->find(__DIR__ . '/Orchid/Resources');

    app(Arbitrator::class)->resources($r);
});

Basically there were multiple calls to return new Arbitrator(); in CrudServiceProvider.php, so each time Arbitrator() was "newed", the earlier registrations were lost.

bilogic commented 3 years ago

The only thing that would be nice to add is an event so that packages would register through it. Then their download was postponed until the moment when the user needs these resources.

What do you mean by this? If it is within my ability, I will write it.

Do you mean a method() to register resources? If yes, in the form of a trait?

tabuna commented 3 years ago

What do you mean by this? If it is within my ability, I will write it.

I looked at it more closely and it turned out that it is already there. We only do the actual work when we go through the middleware https://github.com/orchidsoftware/crud/blob/84851a080e99e370e6e08da0d0c0668e8d03aac3/src/Arbitrator.php#L49-L60

bilogic commented 3 years ago

Ok, thank you so much for the help! I will close this issue first.