61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
namespace App\ModuleManager;
|
|
|
|
use App\Models\Module;
|
|
use App\Models\Templates\TemplatePath;
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
class Uninstaller {
|
|
/**
|
|
* Delete module from database and uninstall its dependencies
|
|
*
|
|
* @param Module $module
|
|
*
|
|
* @throws \ErrorException
|
|
*/
|
|
public static function uninstall(Module $module)
|
|
{
|
|
// check if user does not want to install module in runtime
|
|
if (!App::runningInConsole()) {
|
|
throw new \ErrorException('Modules can be uninstalled only via Artisan!');
|
|
}
|
|
|
|
// delete everything in /resources/views/visitor
|
|
if ($module->is_template) {
|
|
self::deleteDirectory(resource_path('views/visitor'));
|
|
self::deleteDirectory(resource_path('views/modules'));
|
|
self::deleteDirectory(public_path('assets'));
|
|
|
|
// delete all template paths
|
|
TemplatePath::getQuery()
|
|
->delete();
|
|
}
|
|
|
|
if ($module->handler && is_callable([$module->handler, 'uninstall'])) {
|
|
call_user_func([$module->handler, 'uninstall'], $module);
|
|
}
|
|
|
|
$module->delete();
|
|
}
|
|
|
|
/**
|
|
* Delete directory when uninstalling module.
|
|
*
|
|
* @param $directory
|
|
*
|
|
* @throws \ErrorException
|
|
*/
|
|
private static function deleteDirectory($directory)
|
|
{
|
|
if (!is_dir($directory)) {
|
|
return;
|
|
}
|
|
|
|
$success = \File::deleteDirectory($directory, true);
|
|
|
|
if (!$success) {
|
|
throw new \ErrorException('Cannot delete ' . $directory);
|
|
}
|
|
}
|
|
}
|