当我们写
webman 应用插件时想要带上一些依赖,该怎么办呢?带上依赖能不触发webman-admin的接口呢?我们可以创建webman 基础插件把webman 应用插件给带上即可。
├── src
│ ├── demo_app (应用目录)
│ │ ├── ....
│ │ └── api (接口目录,和应用插件创建的一样)
│ │ │ └── Install.php (应用安装文件)
│ ├── ....
│ └── Install.php webman插件文件
...
└── composer.json
应用插件
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();
}
笔记雏形,欢迎大家一起纠正,或者提出更好的方案