MicroFrameWork/kernel/services/ModuleService.php

664 lines
22 KiB
PHP

<?php
namespace kernel\services;
use DirectoryIterator;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option;
use MongoDB\Driver\Session;
use ZipArchive;
class ModuleService
{
protected array $errors = [];
protected null|bool $serverAvailable = null;
/**
* @param string $module
* @return false|array|string
*/
public function getModuleInfo(string $module): false|array|string
{
$info = [];
$info['path'] = $module;
if (file_exists($module . "/manifest.json")) {
$manifest = file_get_contents($module . "/manifest.json");
$manifest = Manifest::getWithVars($manifest);
$manifest = getConst($manifest);
$info = array_merge($info, $manifest);
}
return $info;
}
/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* @param $msg
* @return void
*/
public function addError($msg): void
{
$this->errors[] = $msg;
}
/**
* @param string $slug
* @return false|array|string
*/
public function getModuleInfoBySlug(string $slug): false|array|string
{
return $this->getModuleInfo($this->getModuleDir($slug));
}
/**
* @param string $slug
* @return bool
*/
public function isActive(string $slug): bool
{
$active_modules = Option::where("key", "active_modules")->first();
if ($active_modules) {
$modules = json_decode($active_modules->value);
foreach ($modules->modules as $mod) {
if ($mod === $slug) {
return true;
}
}
}
return false;
}
/**
* @param string $module
* @return void
*/
public function toggleModule(string $module): void
{
$active_modules_info = Option::where("key", "active_modules")->first();
$active_modules = json_decode($active_modules_info->value);
if (in_array($module, $active_modules->modules)) {
$this->deactivateModule($module);
} else {
$this->setActiveModule($module);
}
}
/**
* @param string $module
* @return bool
*/
public function setActiveModule(string $module): bool
{
$active_modules_info = Option::where("key", "active_modules")->first();
$active_modules = json_decode($active_modules_info->value);
if (in_array($module, $active_modules->modules)) {
return true;
}
$module_info = $this->getModuleInfoBySlug($module);
if (isset($module_info['dependence'])) {
$dependence_array = explode(',', $module_info['dependence']);
foreach ($dependence_array as $depend) {
if (!in_array(trim($depend), $active_modules->modules)) {
$this->addError("first activate the $depend module");
return false;
}
}
}
$active_modules->modules[] = $module;
$this->runInitScript($this->getModuleInfoBySlug($module));
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
return true;
}
/**
* @param string $module
* @return bool
*/
public function deactivateModule(string $module): bool
{
$active_modules_info = Option::where("key", "active_modules")->first();
EntityRelation::removeEntityRelation($module);
EntityRelation::removePropertyRelation($module);
$active_modules = json_decode($active_modules_info->value);
if (!in_array($module, $active_modules->modules)) {
return true;
}
$dependence_array = $this->getDependencies();
$str_for_exception = '';
foreach ($dependence_array as $mod => $depend) {
if (in_array($module, $depend)) {
if ($str_for_exception !== '') {
$str_for_exception .= ', ';
}
$str_for_exception .= $mod;
}
}
if ($str_for_exception !== '') {
$this->addError("You can not deactivate $module module. First deactivate modules: $str_for_exception");
return false;
}
unset($active_modules->modules[array_search($module, $active_modules->modules)]);
$active_modules->modules = array_values($active_modules->modules);
$this->runDeactivateScript($this->getModuleInfoBySlug($module));
$active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
$active_modules_info->save();
return true;
}
public function getModuleDir(string $slug)
{
$module_paths = Option::where("key", "module_paths")->first();
$dirs = [];
if ($module_paths) {
$path = json_decode($module_paths->value);
foreach ($path->paths as $p) {
$dirs[] = getConst($p);
}
}
foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
if (basename($fileInfo->getPathname()) === $slug) {
return $fileInfo->getPathname();
}
}
}
return false;
}
/**
* @param $mod_info
* @return void
*/
public function runInitScript($mod_info): void
{
if (isset($mod_info['module_class'])) {
if (isset($mod_info['module_class_file'])) {
require_once $mod_info['module_class_file'];
}
$moduleClass = new $mod_info['module_class']();
$moduleClass->init();
}
}
/**
* @param $mod_info
* @return void
*/
public function runDeactivateScript($mod_info): void
{
if (isset($mod_info['module_class'])) {
if (isset($mod_info['module_class_file'])) {
require_once $mod_info['module_class_file'];
}
$moduleClass = new $mod_info['module_class']();
$moduleClass->deactivate();
}
}
/**
* @return array
*/
public function getActiveModules(): array
{
$modules = [];
$module_paths = Option::where("key", "module_paths")->first();
$active_modules = Option::where("key", "active_modules")->first();
$dirs = [];
if ($module_paths) {
$path = json_decode($module_paths->value);
foreach ($path->paths as $p) {
$dirs[] = getConst($p);
}
}
$active_modules = json_decode($active_modules->value, true);
foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isDot() or !in_array($fileInfo->getFilename(), $active_modules['modules'])) continue;
$modules[] = $this->getModuleInfo($fileInfo->getPathname());
}
}
return $modules;
}
/**
* @return array
*/
public function getModulesRouts(): array
{
$routs = [];
$modules = $this->getActiveModules();
foreach ($modules as $module) {
if (isset($module['routs'])) {
$routs[] = $module['path'] . "/" . $module['routs'];
}
}
return $routs;
}
/**
* @return array
*/
public function getModulesMigrationsPaths(): array
{
$migrationsPaths = [];
$modules = $this->getActiveModules();
foreach ($modules as $module) {
if (isset($module['migration_path'])) {
$migrationsPaths[] = $module['path'] . "/" . $module['migration_path'];
}
}
return $migrationsPaths;
}
/**
* @param string $path
* @return bool
*/
public function installModule(string $path): bool
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
} else {
$this->addError('unable to open zip archive');
return false;
}
if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
$fileHelper->copy_folder($tmpModuleDirFull . 'app', $manifest['app_module_path']);
if (isset($manifest['kernel_module_path'])) {
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
} else {
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
}
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
return true;
}
/**
* @param string $path
* @return void
*/
public function uninstallModule(string $path): void
{
$moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));
if ($this->isActive($moduleInfo['slug'])) {
$this->deactivateModule($moduleInfo['slug']);
}
$fileHelper = new Files();
if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(APP_DIR . '/modules/' . $moduleInfo['slug']);
}
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug'])) {
$fileHelper->recursiveRemoveDir(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug']);
}
}
/**
* @param string $path
* @return void
*/
public function packModule(string $path): void
{
$moduleName = basename($path);
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $moduleName . "/";
$fileHelper = new Files();
$fileHelper->copy_folder(ROOT_DIR . $path, $tmpModuleDirFull . 'app/');
if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleName)) {
$fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
} else {
mkdir($tmpModuleDirFull . 'kernel/');
}
if (!is_dir(RESOURCES_DIR . '/tmp/modules')) {
mkdir(RESOURCES_DIR . '/tmp/modules', 0777, true);
}
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.igm');
}
/**
* @param string $path
* @return bool
*/
public function updateModule(string $path): bool
{
$zip = new ZipArchive;
$tmpModuleDir = md5(time());
$res = $zip->open(ROOT_DIR . $path);
if ($res === TRUE) {
$tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
$zip->extractTo($tmpModuleDirFull);
$zip->close();
} else {
$this->addError('unable to open zip archive');
return false;
}
if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
$this->addError('manifest.json not found');
return false;
}
$manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
$manifest = Manifest::getWithVars($manifestJson);
$fileHelper = new Files();
$fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', $manifest['app_module_path'] . '/manifest.json');
if (isset($manifest['kernel_module_path'])) {
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
} else {
$fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
}
$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
return true;
}
/**
* @return array
*/
public function getDependencies(): array
{
$modules_info = $this->getActiveModules();
$dependence_array = [];
foreach ($modules_info as $mod) {
if (isset($mod['dependence'])) {
$dependence_array[$mod['slug']] = explode(',', $mod['dependence']);
}
}
return $dependence_array;
}
public function isInstall(string $slug): bool
{
$module_paths = Option::where("key", "module_paths")->first();
$dirs = [];
if ($module_paths) {
$path = json_decode($module_paths->value);
foreach ($path->paths as $p) {
$dirs[] = getConst($p);
}
}
foreach ($dirs as $dir) {
foreach (new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isDot()) continue;
if ($this->getModuleInfo($fileInfo->getPathname())['slug'] === $slug) {
return true;
};
}
}
return false;
}
public function isLastVersion(string $slug): bool
{
if ($this->isServerAvailable()){
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
return true;
}
}
}
return false;
}
public function isKernelModule(string $slug): bool
{
$modules_info = $this->getKernelModules();
foreach ($modules_info as $mod) {
if ($mod['slug'] === $slug) {
return true;
}
}
return false;
}
public function isShopModule(string $slug): bool
{
if ($this->isServerAvailable()){
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
if (!$this->issetModuleShopToken()){
return false;
}
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
$mod_info = $this->getModuleInfoBySlug($slug);
foreach ($modules_info as $mod) {
if ($mod['slug'] === $mod_info['slug']) {
return true;
}
}
}
return false;
}
public function getKernelModules(): array
{
$modules_info = [];
foreach (new DirectoryIterator(KERNEL_MODULES_DIR) as $fileInfo) {
if ($fileInfo->isDot()) continue;
$modules_info[] = $this->getModuleInfo($fileInfo->getPathname());
}
return $modules_info;
}
public function isServerAvailable(): bool
{
if (null !== $this->serverAvailable){
return $this->serverAvailable;
}
try {
RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$this->serverAvailable = true;
return true;
} catch (\Exception $e) {
$this->serverAvailable = false;
return false;
}
}
public function issetModuleShopToken(): bool
{
if (!empty($_ENV['MODULE_SHOP_TOKEN'])){
return true;
}
return false;
}
public function createDirs(string $slug): void
{
mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/service");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");
mkdir(APP_DIR . "/modules/$slug");
mkdir(APP_DIR . "/modules/$slug/controllers");
mkdir(APP_DIR . "/modules/$slug/routs");
}
public function createManifest(array $params): void
{
$name = $params['name'] ?? '';
$author = $params['author'] ?? '';
$slug = $params['slug'] ?? '';
$data = "{\n";
$data .= " \"name\": \"$name\",\n";
$data .= " \"version\": \"0.1\",\n";
$data .= " \"author\": \"$author\",\n";
$data .= " \"slug\": \"$slug\",\n";
$data .= " \"description\": \"$name module\",\n";
$data .= " \"module_class\": \"app\\\\modules\\\\$slug\\\\" . ucfirst($slug) . "Module\",\n";
$data .= " \"module_class_file\": \"{APP}/modules/$slug/" . ucfirst($slug) . "Module.php\",\n";
$data .= " \"routs\": \"routs/$slug.php\",\n";
$data .= " \"migration_path\": \"migrations\"\n";
$data .= "}";
file_put_contents(APP_DIR . "/modules/$slug/manifest.json", $data);
}
public function createControllers(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/controllers/kernel_controller_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data);
$data = file_get_contents(KERNEL_DIR . '/templates/controllers/app_controller_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(APP_DIR . '/modules/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data );
}
public function createRoutes(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/routs/kernel_routs_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $data);
$data = file_get_contents(KERNEL_DIR . '/templates/routs/app_routs_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $data );
}
public function createModuleClassFiles(string $slug, string $label): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/module_files/kernel_module_file_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
$data = str_replace('{label}', $label, $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . ucfirst($slug) . 'Module.php', $data);
$data = file_get_contents(KERNEL_DIR . '/templates/module_files/app_module_file_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(APP_DIR . '/modules/' . $slug . '/' . ucfirst($slug) . 'Module.php', $data);
}
public function createModel(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/models/model_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . ucfirst($slug) . '.php', $data);
}
public function createFormModel(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/models/forms/create_form_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . ucfirst($slug) . 'Form.php', $data);
}
public function createService(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/services/service_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/service/' . ucfirst($slug) . 'Service.php', $data);
}
public function createViews(string $slug): void
{
$this->createIndex($slug);
$this->createView($slug);
$this->createForm($slug);
}
public function createIndex(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/views/index_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $data);
}
public function createView(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/views/view_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $data);
}
public function createForm(string $slug): void
{
$data = file_get_contents(KERNEL_DIR . '/templates/views/form_template');
$data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $data);
}
}