Trap exception/check for module existing on disk #1211 (#1271)

* Trap exception/check for module existing on disk #1211

* Style fixes
This commit is contained in:
Nabeel S 2021-08-06 13:30:24 -04:00 committed by GitHub
parent 4a04d73d08
commit 95e1df619e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 7 deletions

View File

@ -9,6 +9,7 @@ use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Routing\Redirector; use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View; use Illuminate\View\View;
class ModulesController extends Controller class ModulesController extends Controller
@ -29,6 +30,7 @@ class ModulesController extends Controller
{ {
$modules = $this->moduleSvc->getAllModules(); $modules = $this->moduleSvc->getAllModules();
$new_modules = $this->moduleSvc->scan(); $new_modules = $this->moduleSvc->scan();
return view('admin.modules.index', [ return view('admin.modules.index', [
'modules' => $modules, 'modules' => $modules,
'new_modules' => $new_modules, 'new_modules' => $new_modules,
@ -97,7 +99,14 @@ class ModulesController extends Controller
*/ */
public function enable(Request $request) 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')); return redirect(route('admin.modules.index'));
} }

View File

@ -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 * @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 * @return object
*/ */
public function getModule($id): object public function getModule($id): Module
{ {
return Module::find($id); return Module::find($id);
} }
@ -164,6 +206,7 @@ class ModuleService extends Service
0777, 0777,
true true
); );
$temp_ext_folder = storage_path('app/tmp/modules/'.$new_dir); $temp_ext_folder = storage_path('app/tmp/modules/'.$new_dir);
$temp = 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::moveDirectory($temp, $toCopy);
File::deleteDirectory($temp_ext_folder); File::deleteDirectory($temp_ext_folder);
try { try {

View File

@ -1 +0,0 @@
'stored' => '保存',