From 95e1df619eba3dfacfbed58f2eb37027eecb7d6c Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Fri, 6 Aug 2021 13:30:24 -0400 Subject: [PATCH] Trap exception/check for module existing on disk #1211 (#1271) * Trap exception/check for module existing on disk #1211 * Style fixes --- .../Controllers/Admin/ModulesController.php | 11 +++- app/Services/ModuleService.php | 52 +++++++++++++++++-- resources/lang/zh/aircraft.php | 1 - 3 files changed, 57 insertions(+), 7 deletions(-) delete mode 100644 resources/lang/zh/aircraft.php diff --git a/app/Http/Controllers/Admin/ModulesController.php b/app/Http/Controllers/Admin/ModulesController.php index 318355a5..3b0bd555 100644 --- a/app/Http/Controllers/Admin/ModulesController.php +++ b/app/Http/Controllers/Admin/ModulesController.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\View\Factory; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Routing\Redirector; +use Illuminate\Support\Facades\Log; use Illuminate\View\View; class ModulesController extends Controller @@ -29,6 +30,7 @@ class ModulesController extends Controller { $modules = $this->moduleSvc->getAllModules(); $new_modules = $this->moduleSvc->scan(); + return view('admin.modules.index', [ 'modules' => $modules, 'new_modules' => $new_modules, @@ -97,7 +99,14 @@ class ModulesController extends Controller */ public function enable(Request $request) { - $this->moduleSvc->addModule($request->input('name')); + $moduleName = $request->input('name'); + + try { + $this->moduleSvc->addModule($moduleName); + } catch (\Exception $e) { + Log::error('Error activating module '.$moduleName); + } + return redirect(route('admin.modules.index')); } diff --git a/app/Services/ModuleService.php b/app/Services/ModuleService.php index ca485477..e21d3087 100644 --- a/app/Services/ModuleService.php +++ b/app/Services/ModuleService.php @@ -88,13 +88,55 @@ class ModuleService extends Service } /** - * Get All modules from Database + * Get all of the modules from database but make sure they also exist on disk * * @return object */ - public function getAllModules(): object + public function getAllModules(): array { - return Module::all(); + $modules = []; + $moduleList = Module::all(); + + /** @var Module $module */ + foreach ($moduleList as $module) { + /** @var \Nwidart\Modules\Module $moduleInstance */ + $moduleInstance = \Nwidart\Modules\Facades\Module::find($module->name); + if (empty($moduleInstance)) { + continue; + } + + if (file_exists($moduleInstance->getPath())) { + $modules[] = $module; + } + } + + return $modules; + } + + /** + * Determine if a module is active - also checks that the module exists properly + * + * @param string $name + * + * @return bool + */ + public function moduleActive(string $name): bool + { + $module = Module::where('name', $name); + if (!$module->exists()) { + return false; + } + + $moduleInstance = \Nwidart\Modules\Facades\Module::find($module->name); + if (empty($moduleInstance)) { + return false; + } + + if (!file_exists($moduleInstance->getPath())) { + return false; + } + + return true; } /** @@ -104,7 +146,7 @@ class ModuleService extends Service * * @return object */ - public function getModule($id): object + public function getModule($id): Module { return Module::find($id); } @@ -164,6 +206,7 @@ class ModuleService extends Service 0777, true ); + $temp_ext_folder = storage_path('app/tmp/modules/'.$new_dir); $temp = storage_path('app/tmp/modules/'.$new_dir); @@ -219,7 +262,6 @@ class ModuleService extends Service } File::moveDirectory($temp, $toCopy); - File::deleteDirectory($temp_ext_folder); try { diff --git a/resources/lang/zh/aircraft.php b/resources/lang/zh/aircraft.php deleted file mode 100644 index 64070ebd..00000000 --- a/resources/lang/zh/aircraft.php +++ /dev/null @@ -1 +0,0 @@ - 'stored' => '保存',