Files
BiveEngine/engine/Main/trait.plugins.php
2025-12-24 19:19:01 +03:00

90 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// Работа с плагинами
defined('ROOT_DIR') || exit;
trait Plugins {
public array $plugins = array();
// Загрузка всех плагинов системы
public function plugins_load(): bool
{
$plugins_dir = $this->plugins_get_dir();
$plugins = scandir($plugins_dir, SCANDIR_SORT_DESCENDING);
if(!count($plugins)) return false;
foreach ($plugins as $key => $plugin_dir)
$this->plugin_load($plugin_dir);
return true;
}
// Загрузка плагина
public function plugin_load($plugin_dir){
global $b;
if($this->plugins[$plugin_dir]) return;
$plugins_dir = $this->plugins_get_dir();
$plugin_path = $plugins_dir . SLASH . $plugin_dir . SLASH;
$plugin_manifest_path = $plugin_path . "manifest.yaml";
// Проверка наличия manifest.yaml
if(!file_exists($plugin_manifest_path)) return;
$manifest = $this->yaml_read($plugin_manifest_path);
$plugin_script_path = $plugin_path . $manifest["plugin_script"];
// Подгрузка зависимостей
if(isset($manifest["plugin_dependencies"])) {
foreach ($manifest["plugin_dependencies"] as $key => $plugin_dependence) {
$this->plugin_load($plugin_dependence);
}
}
// Проверка наличия исполняемого скрипта
if(!file_exists($plugin_script_path)) return;
$plugin_class = $this->plugin_register($manifest, $plugin_dir);
$this->ls_set_key("plugin", $plugin_class);
require_once $plugin_script_path;
}
// Получить текущий плагин
public function plugin_get()
{
return $this->ls_get_key("plugin");
}
// Регистрация плагина
public function plugin_register($manifest, $plugin_dir)
{
$plugin_class = $this->plugin_create_class($manifest, $plugin_dir);
$this->plugins[$plugin_dir] = array(
"plugin_name" => $manifest["plugin_name"],
"plugin_description" => $manifest["plugin_description"],
"plugin_author" => $manifest["plugin_author"],
"plugin_version" => $manifest["plugin_version"],
"class" => $plugin_class
);
return $plugin_class;
}
// Функция создания класса плагина
public function plugin_create_class($manifest, $plugin_dir)
{
$plugin_class_name = $manifest["plugin_class"] ?? "Plugin";
return new $plugin_class_name($plugin_dir);
}
// Получить папку где хранятся плагины
public function plugins_get_dir(): string
{
return ROOT_DIR . SLASH . PLAYAREA_DIR_NAME . SLASH . "plugins";
}
// Прочитать конфигурацию YAML
public function yaml_read($filePath)
{
if (!extension_loaded('yaml'))
die('Расширение YAML не установлено.');
$ymlContent = file_get_contents($filePath);
return yaml_parse($ymlContent);
}
}