Files
nexusphp/nexus/Plugin/BasePlugin.php

83 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2022-06-03 03:42:53 +08:00
<?php
namespace Nexus\Plugin;
2022-06-08 14:15:59 +08:00
use App\Repositories\BaseRepository;
2022-06-03 03:42:53 +08:00
use Illuminate\Support\Facades\Artisan;
2022-06-08 14:15:59 +08:00
abstract class BasePlugin extends BaseRepository
2022-06-03 03:42:53 +08:00
{
abstract function install();
abstract function boot();
public function runMigrations($dir, $rollback = false)
{
$command = "migrate";
if ($rollback) {
$command .= ":rollback";
}
$command .= " --realpath --force";
foreach (glob("$dir/*.php") as $file) {
$file = str_replace('\\', '/', $file);
$toExecute = "$command --path=$file";
do_log("command: $toExecute");
Artisan::call($toExecute);
}
}
2022-08-20 19:11:28 +08:00
public static function checkMainApplicationVersion($silent = true): bool
2022-08-20 19:11:28 +08:00
{
$constantNameArr = [
"static::COMPATIBLE_NP_VERSION",
"static::COMPATIBLE_VERSION", //before use
];
foreach ($constantNameArr as $constantName) {
if (defined($constantName) && version_compare(VERSION_NUMBER, constant($constantName), '<')) {
if ($silent) {
return false;
}
throw new \RuntimeException(sprintf(
"NexusPHP version: %s is too low, this plugin require: %s",
VERSION_NUMBER, constant($constantName)
));
}
2022-08-20 19:11:28 +08:00
}
return true;
2022-08-20 19:11:28 +08:00
}
2023-07-04 01:00:02 +08:00
public function getNexusView($name): string
{
$reflection = new \ReflectionClass(get_called_class());
$pluginRoot = dirname($reflection->getFileName(), 2);
return $pluginRoot . "/resources/views/" . trim($name, "/");
}
2024-10-08 00:22:52 +08:00
public function trans($name): string
{
2024-10-20 23:47:40 +08:00
return nexus_trans($this->getTransKey($name));
}
public function getTransKey($name): string
{
return sprintf("%s::%s", static::ID, $name);
2024-10-08 00:22:52 +08:00
}
public static function getInstance(): static
{
return Plugin::getById(static::ID);
}
2025-05-05 18:24:17 +07:00
public function getVersion(): string
{
$constantName = "static::VERSION";
return defined($constantName) ? constant($constantName) : '';
}
public function getId(): string
{
$className = str_replace("Repository", "", get_called_class());
$plugin = call_user_func([$className, "make"]);
return $plugin->getId();
}
2022-06-03 03:42:53 +08:00
}