webman 应用插件带依赖 笔记

KingBes

当我们写 webman 应用插件 时想要带上一些依赖,该怎么办呢?带上依赖能不触发webman-admin的接口呢?我们可以创建 webman 基础插件webman 应用插件给带上即可。

目录结构

├── src
│    ├── demo_app (应用目录)
│    │    ├── ....
│    │    └── api (接口目录,和应用插件创建的一样)
│    │    │    └── Install.php (应用安装文件)
│    ├── ....
│    └── Install.php webman插件文件
...
└── composer.json 

操作

创建webman应用插件和基础插件

应用插件

php webman app-plugin:create {插件标识}

基础插件

php webman plugin:create --name=foo/admin (foo/admin 具体看文档)

将创建好的应用插件 plugin/{插件标识} 复制到 基础插件的src目录下

编辑文件

Install.php webman插件文件的Install::class

  • 添加----应用映射关系
public static $appRelation = array(
    "foo_blog" => "plugin/foo_blog", //比如 应用名=>plugin/应用名
);
  • 添加----静态方法 安装应用插件
public static function installByAppRelation()
{
    try {
        foreach (static::$appRelation as $source => $dest) {
            $context = null;
            $new_version = static::newVersion($source);
            $old_version = static::oldVersion($source);
            $install_class = "\\plugin\\$source\\api\\Install";
            if ($pos = strrpos($dest, '/')) {
                $parent_dir = base_path() . '/' . substr($dest, 0, $pos);
                if (!is_dir($parent_dir)) {
                    mkdir($parent_dir, 0777, true);
                }
            }
            copy_dir(__DIR__ . "/$source", base_path() . "/$dest");
            echo "Create app $source\n";
            if ($old_version) {
                if (class_exists($install_class) && method_exists($install_class, 'beforeUpdate')) {
                    $context = call_user_func([$install_class, 'beforeUpdate'], $old_version, $new_version);
                }
                if (class_exists($install_class) && method_exists($install_class, 'update')) {
                    call_user_func([$install_class, 'update'], $old_version, $new_version, $context);
                }
            } else {
                if (class_exists($install_class) && method_exists($install_class, 'install')) {
                    call_user_func([$install_class, 'install'], $new_version);
                }
            }
            echo "Install app $source $new_version\n";
        }
    } catch (\Exception $e) {
        throw new \Exception($e->getMessage());
    }
}
  • 添加----静态方法 卸载应用插件
public static function uninstallByAppRelation()
{
    foreach (static::$appRelation as $source => $dest) {
        $path = base_path() . "/$dest";
        $install_class = "\\plugin\\$source\\api\\Install";
        // 卸载应用
        if (class_exists($install_class) && method_exists($install_class, 'uninstall')) {
            call_user_func([$install_class, 'uninstall'], static::newVersion($source));
        }
        if (!is_dir($path) && !is_file($path)) {
            continue;
        }
        echo "Remove $dest\n";
        if (is_file($path) || is_link($path)) {
            unlink($path);
            continue;
        }
        remove_dir($path);
    }
}
  • 添加----静态方法 获取新应用版本号
public static function newVersion($app): string|null
{
    if (!is_file($file = __DIR__ . "/$app/config/app.php")) {
        return null;
    }
    $app_config = include $file;
    return $app_config['version'] ?? null;
}
  • 添加----静态方法 获取旧应用版本号
public static function oldVersion($app): string|null
{
    if (!is_file($file = base_path() . "/plugin/$app/config/app.php")) {
        return null;
    }
    $app_config = include $file;
    return $app_config['version'] ?? null;
}
  • 修改----静态方法 install()
public static function install()
{
    static::installByRelation();
    // 这里
    static::installByAppRelation();
}
  • 修改----静态方法 uninstall()
public static function uninstall()
{
    self::uninstallByRelation();
    // 这里
    self::uninstallByAppRelation();
}

笔记雏形,欢迎大家一起纠正,或者提出更好的方案

54 0 0
0个评论

KingBes

1930
积分
0
获赞数
0
粉丝数
2023-06-12 加入
🔝