Initial commit
This commit is contained in:
90
engine/Main/trait.plugins.php
Normal file
90
engine/Main/trait.plugins.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user