File size: 4,490 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | <?php
namespace App\Services\Extension\Traits;
use App\Models\Extension;
use Exception;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
trait UninstallExtension
{
public function uninstall(string $extensionSlug): bool|array
{
try {
$this->extensionSlug = $extensionSlug;
$this->zipExtractPath = resource_path('extensions' . DIRECTORY_SEPARATOR . $extensionSlug);
$this->getIndexJson();
if (empty($this->indexJsonArray)) {
return [
'status' => false,
'message' => trans('index.json not found'),
];
}
$this->deleteOldVersionFiles();
$this->uninstallFilesDelete();
$this->deleteRoute();
$this->runUninstallQuery();
$this->deleteControllers();
$this->deleteResource();
Artisan::call('optimize:clear');
Extension::query()->where('slug', $extensionSlug)
->update([
'installed' => 0,
]);
Artisan::call('cache:clear');
if ($extensionSlug == 'maintenance') {
cache()->forget('maintenance');
}
return [
'success' => true,
'status' => true,
'message' => trans('Extension uninstalled successfully'),
];
} catch (Exception $e) {
return [
'status' => false,
'message' => $e->getMessage(),
];
}
}
public function uninstallFilesDelete(): void
{
$files = data_get($this->indexJsonArray, 'stubs', []);
if (empty($files) && ! is_array($files)) {
return;
}
foreach ($files as $file) {
$destinationPath = base_path($file);
if (File::exists($destinationPath)) {
File::delete($destinationPath);
}
}
}
public function deleteRoute(): void
{
$route = data_get($this->indexJsonArray, 'route', null);
if (empty($route)) {
return;
}
$routePath = base_path('routes/extroutes/' . basename($route));
if (File::exists($routePath)) {
File::delete($routePath);
}
}
public function deleteControllers(): void
{
$controllers = data_get($this->indexJsonArray, 'controllers', []);
if (empty($controllers) && ! is_array($controllers)) {
return;
}
foreach ($controllers as $controller) {
$path = base_path($controller);
if (File::exists($path)) {
File::delete($path);
}
}
}
public function runUninstallQuery()
{
$data = data_get($this->indexJsonArray, 'migrations.uninstall');
if (empty($data)) {
return;
}
foreach ($data as $value) {
$table = data_get($value, 'condition.table');
$column = data_get($value, 'condition.column', null);
$sqlPath = $this->zipExtractPath . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR . data_get($value, 'path');
if (
Schema::hasTable($table)
&& File::exists($sqlPath)
&& is_null($column)
) {
$query = $this->installQuery(
$sqlPath
);
DB::unprepared($query);
} elseif (
Schema::hasTable($table)
&& File::exists($sqlPath)
&& $column
) {
$query = $this->installQuery(
$sqlPath
);
$column = data_get($value, 'condition.column');
if (Schema::hasColumn($table, $column)) {
DB::unprepared($query);
}
}
}
}
public function deleteResource(): void
{
File::deleteDirectory(resource_path("extensions/$this->extensionSlug"));
}
public function uninstallQuery(?string $zipExtractPath = null): bool|string
{
$zipExtractPath = $zipExtractPath ?? $this->zipExtractPath;
return file_get_contents($zipExtractPath . DIRECTORY_SEPARATOR . 'uninstall.sql');
}
}
|