diff --git a/app/modules/module_shop/controllers/ModuleShopRestController.php b/app/modules/module_shop/controllers/ModuleShopRestController.php index 8d59687..fba8140 100644 --- a/app/modules/module_shop/controllers/ModuleShopRestController.php +++ b/app/modules/module_shop/controllers/ModuleShopRestController.php @@ -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)); + } + } \ No newline at end of file diff --git a/app/modules/module_shop/routs/ms.php b/app/modules/module_shop/routs/ms.php index cf9ef9f..ee32d28 100644 --- a/app/modules/module_shop/routs/ms.php +++ b/app/modules/module_shop/routs/ms.php @@ -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']); }); diff --git a/app/modules/module_shop/services/ModuleShopService.php b/app/modules/module_shop/services/ModuleShopService.php index 65efd90..cd2bedb 100644 --- a/app/modules/module_shop/services/ModuleShopService.php +++ b/app/modules/module_shop/services/ModuleShopService.php @@ -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]); + } + } \ No newline at end of file