<?php

declare(strict_types=1);


namespace App\Blog\Providers;

use App\Blog\Support\PluginManager;
use Illuminate\Support\ServiceProvider;

class PluginManagerServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(PluginManager::class);
    }

    public function boot(): void
    {
        $manager = $this->app->make(PluginManager::class);

        if (! is_dir(config('plugins.path'))) {
            return;
        }

        foreach ($manager->all() as $key => $plugin) {
            if (! $plugin['enabled']) {
                continue;
            }

            $provider = $manager->resolveProvider($key);

            if ($provider && class_exists($provider)) {
                $provider::bootPlugin($manager);
            }
        }
    }
}
