lock->run($identifier, fn () => $this->removePackage($identifier)); } private function removePackage(string $identifier): void { $manifest = $this->extensions->discovered()->get($identifier); throw_if($manifest === null, InvalidExtensionException::class, "Extension \"{$identifier}\" is not installed."); $this->compatibility->assertCanDisable($identifier, $this->extensions->enabled()); Event::dispatch(new ExtensionRemoving($manifest)); $this->files->run([ $this->extensions->directory().DIRECTORY_SEPARATOR.$identifier => null, $this->assets->publishedPath($identifier) => null, ], function () use ($identifier): void { DB::transaction(function () use ($identifier): void { Extension::query()->where('identifier', $identifier)->delete(); $this->purgeState($identifier); }); }); $this->extensions->flushDiscovery(); DB::afterCommit(function () use ($identifier): void { rescue(fn () => $this->settingFiles->purge($identifier)); rescue(fn () => $this->runtime->refresh()); rescue(fn () => Event::dispatch(new ExtensionRemoved($identifier))); }); } /** * Forget what the extension stored outside its own tables: settings (secrets included) in * every scope, and the `ext..*` permissions subusers were granted. Otherwise the next * package installed with this id would inherit them. */ private function purgeState(string $identifier): void { ExtensionSetting::query()->where('extension', $identifier)->delete(); $prefix = ExtensionPermissionRegistry::group($identifier).'.'; Subuser::query()->where('permissions', 'like', '%"'.$prefix.'%')->lazyById()->each(function (Subuser $subuser) use ($prefix): void { $permissions = array_values(array_filter($subuser->permissions, fn (string $permission): bool => ! str_starts_with($permission, $prefix))); if ($permissions !== $subuser->permissions) { $subuser->update(['permissions' => $permissions]); } }); } }