get all dependencies

This commit is contained in:
Kavalar 2025-01-23 13:33:41 +03:00
parent 0cc1e0773d
commit 29e0ff327f
3 changed files with 43 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace app\modules\module_shop\controllers;
use app\modules\module_shop\models\ModuleShop;
use app\modules\module_shop\services\ModuleShopService;
use JetBrains\PhpStorm\NoReturn;
use kernel\helpers\Debug;
use kernel\Request;
@ -105,4 +106,9 @@ class ModuleShopRestController extends RestController
$this->renderApi($model->toArray());
}
public function actionGetDependenciesBySlug(string $slug)
{
$this->renderApi(ModuleShopService::getAllDependencies($slug));
}
}

View File

@ -39,6 +39,7 @@ App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router)
App::$collector->group(["prefix" => "module_shop"], function (CgRouteCollector $router) {
App::$collector->get('/gb_slug', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionIndexGroupBySlug']);
App::$collector->get('/install/{id}', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionInstall']);
App::$collector->get('/get_all_dependencies/{slug}', [\app\modules\module_shop\controllers\ModuleShopRestController::class, 'actionGetDependenciesBySlug']);
App::$collector->group(["prefix" => "kernel"], function (CgRouteCollector $router) {
App::$collector->get('/update/{id}', [\app\modules\module_shop\controllers\KernelShopRestController::class, 'actionUpdate']);
});

View File

@ -51,4 +51,40 @@ class ModuleShopService extends ModuleService
return false;
}
public static function getAllDependencies(string $slug, array $depArr = []): array
{
$query = ModuleShop::query();
$query->select('ms1.*')
->from('module_shop as ms1')
->leftJoin('module_shop as ms2', function ($join) {
$join->on('ms1.slug', '=', 'ms2.slug')
->on('ms1.id', '<', 'ms2.id');
})
->where('ms2.slug', '=', null);
$query->where('ms1.slug', $slug);
$module = $query->first();
if ($module){
if ($module->dependence !== null){
$moduleDependencies = explode(",", $module->dependence);
foreach ($moduleDependencies as $dependency){
if (!in_array($dependency, $depArr)){
$depArr[] = $dependency;
$depArrTmp = self::getAllDependencies($dependency, $depArr);
foreach ($depArrTmp as $item){
if (!in_array($item, $depArr)){
$depArr[] = $item;
}
}
}
}
}
}
return array_diff($depArr, [$slug]);
}
}