Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
400 stars 53 forks source link

Registering individual composers manually #180

Closed oryoca closed 4 months ago

oryoca commented 9 months ago

Hello!

I have a use case where I provide Blocks as a part of a composer package. It has a ServiceProvider that does along the lines of

class FooProvider extends ServiceProvider
{
    public function boot(): void
    {
        App::call(function(AcfComposer $acf) {
            $acf->registerPath(__DIR__, __NAMESPACE__ . '\\');
        });
    }

}

and that seems to work just fine.

However, I would like to register Composers conditionally, not a directory at a time, so I can skip Composers that exist in the App's namespace with the same name.

This would be similar to how Composers can be registered in Laravel (https://laravel.com/docs/9.x/views#view-composers).

Would it be feasible to add a method for that to AcfComposer?

For example

public function register(string $composer): void
{
    if (
        ! is_subclass_of($composer, Composer::class) ||
        is_subclass_of($composer, Partial::class) ||
        (new ReflectionClass($composer))->isAbstract()
    ) {
        return;
    }

    $namespace = Str::beforeLast($composer, '\\');
    $this->composers[$namespace][] = (new $composer($this->app))->compose();
}