<?php

declare(strict_types=1);


namespace App\Blog\Support;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

abstract class PluginServiceProvider
{
    /**
     * 插件入口：由 PluginManagerServiceProvider 在应用启动时调用。
     */
    public static function bootPlugin(PluginManager $manager): void
    {
        (new static)->boot($manager);
    }

    abstract protected function boot(PluginManager $manager): void;

    protected function addAction(PluginManager $manager, string $hook, callable $callback, int $priority = 10): void
    {
        $manager->addAction($hook, $callback, $priority);
    }

    protected function addFilter(PluginManager $manager, string $hook, callable $callback, int $priority = 10): void
    {
        $manager->addFilter($hook, $callback, $priority);
    }

    /**
     * 加载插件路由文件。
     */
    protected function loadRoutes(string $file): void
    {
        if (file_exists($file)) {
            Route::middleware('web')->group($file);
        }
    }

    /**
     * 注册插件视图命名空间：plugins/{vendor}.{name}/views -> plugin.{vendor}.{name}
     */
    protected function loadViews(string $viewsPath, string $namespace): void
    {
        if (is_dir($viewsPath)) {
            \Illuminate\Support\Facades\View::addNamespace($namespace, $viewsPath);
        }
    }

    protected function slug(string $string): string
    {
        return Str::slug($string);
    }
}
