mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 06:15:20 +00:00
refactor(services): move ModuleInstaller to app/Support/Module
ModuleInstaller has the same shape as Updater (moved in 7cf72b9f): every method is public static, no constructor, no DI, no instance state. It orchestrates marketplace operations — fetch catalog, download zip, verify checksum, unzip, copy files, run module:migrate/module:enable, dispatch install/enable events — but the orchestration itself is stateless procedural plumbing.
Emitting events and writing to the Module eloquent model doesn't make it a service; plenty of static helpers touch models. The distinguishing factor is stateless-procedural vs DI-injected-workflow, and this is clearly the former.
4 consumers updated: ModulesController, ModuleInstallationController, InstallModuleCommand, and a doc comment in config/invoiceshelf.php. 350 tests still pass.
This leaves app/Services/ with no single-file driver-less subdirs except Mail/Module/Pdf/Storage which have multiple files each. The Module/ subdir in Services is now deleted entirely — the marketplace installer moved out and there were no other files in there.
This commit is contained in:
262
app/Support/Module/ModuleInstaller.php
Normal file
262
app/Support/Module/ModuleInstaller.php
Normal file
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Module;
|
||||
|
||||
use App\Events\ModuleEnabledEvent;
|
||||
use App\Events\ModuleInstalledEvent;
|
||||
use App\Models\Module as ModelsModule;
|
||||
use App\Models\Setting;
|
||||
use App\Traits\SiteApi;
|
||||
use Artisan;
|
||||
use File;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
use ZipArchive;
|
||||
|
||||
class ModuleInstaller
|
||||
{
|
||||
use SiteApi;
|
||||
|
||||
private static function marketplaceToken(): ?string
|
||||
{
|
||||
$token = Setting::getSetting('api_token');
|
||||
|
||||
if (! is_string($token) || trim($token) === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
private static function decodeMarketplaceJson($response): array
|
||||
{
|
||||
if ($response instanceof RequestException || ! $response) {
|
||||
return [
|
||||
'status' => 0,
|
||||
'body' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$body = $response->getBody()->getContents();
|
||||
|
||||
return [
|
||||
'status' => $response->getStatusCode(),
|
||||
'body' => $body !== '' ? json_decode($body) : null,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getModules(): array
|
||||
{
|
||||
$url = env('APP_ENV') === 'development'
|
||||
? 'api/marketplace/modules?is_dev=1'
|
||||
: 'api/marketplace/modules';
|
||||
|
||||
$token = static::marketplaceToken();
|
||||
$decoded = static::decodeMarketplaceJson(
|
||||
static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], $token)
|
||||
);
|
||||
|
||||
if ($decoded['status'] === 401 && $token !== null) {
|
||||
$decoded = static::decodeMarketplaceJson(
|
||||
static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], null)
|
||||
);
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
public static function getModule(string $module): array
|
||||
{
|
||||
$url = env('APP_ENV') === 'development'
|
||||
? 'api/marketplace/modules/'.$module.'?is_dev=1'
|
||||
: 'api/marketplace/modules/'.$module;
|
||||
|
||||
$token = static::marketplaceToken();
|
||||
$decoded = static::decodeMarketplaceJson(
|
||||
static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], $token)
|
||||
);
|
||||
|
||||
if ($decoded['status'] === 401 && $token !== null) {
|
||||
$decoded = static::decodeMarketplaceJson(
|
||||
static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], null)
|
||||
);
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
public static function upload($request): string
|
||||
{
|
||||
$tempDir = storage_path('app/temp-'.md5(mt_rand()));
|
||||
|
||||
if (! File::isDirectory($tempDir)) {
|
||||
File::makeDirectory($tempDir);
|
||||
}
|
||||
|
||||
return $request->file('avatar')->storeAs(
|
||||
'temp-'.md5(mt_rand()),
|
||||
$request->module.'.zip',
|
||||
'local'
|
||||
);
|
||||
}
|
||||
|
||||
public static function download(string $slug, string $version, ?string $checksumSha256 = null): array|bool
|
||||
{
|
||||
$data = null;
|
||||
$path = null;
|
||||
|
||||
$url = env('APP_ENV') === 'development'
|
||||
? "api/marketplace/modules/file/{$slug}?version={$version}&is_dev=1"
|
||||
: "api/marketplace/modules/file/{$slug}?version={$version}";
|
||||
|
||||
$token = static::marketplaceToken();
|
||||
$response = static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], $token);
|
||||
|
||||
if ($response instanceof RequestException) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Download Exception',
|
||||
'data' => [
|
||||
'path' => $path,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
if ($response && $response->getStatusCode() === 401 && $token !== null) {
|
||||
$response = static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], null);
|
||||
}
|
||||
|
||||
if ($response instanceof RequestException || ! $response) {
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Download Exception',
|
||||
];
|
||||
}
|
||||
|
||||
if ($response && $response->getStatusCode() !== 200) {
|
||||
$decoded = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $decoded['error'] ?? 'Module download failed',
|
||||
];
|
||||
}
|
||||
|
||||
if ($response && $response->getStatusCode() === 200) {
|
||||
$data = $response->getBody()->getContents();
|
||||
}
|
||||
|
||||
$tempDir = storage_path('app/temp-'.md5(mt_rand()));
|
||||
|
||||
if (! File::isDirectory($tempDir)) {
|
||||
File::makeDirectory($tempDir);
|
||||
}
|
||||
|
||||
$zipFilePath = $tempDir.'/upload.zip';
|
||||
$uploaded = is_int(file_put_contents($zipFilePath, $data));
|
||||
|
||||
if (! $uploaded) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($checksumSha256 && hash_file('sha256', $zipFilePath) !== $checksumSha256) {
|
||||
File::delete($zipFilePath);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => 'Checksum verification failed',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'path' => $zipFilePath,
|
||||
];
|
||||
}
|
||||
|
||||
public static function unzip($module, $zipFilePath): string
|
||||
{
|
||||
if (! file_exists($zipFilePath)) {
|
||||
throw new \Exception('Zip file not found');
|
||||
}
|
||||
|
||||
$tempExtractDir = storage_path('app/temp2-'.md5(mt_rand()));
|
||||
|
||||
if (! File::isDirectory($tempExtractDir)) {
|
||||
File::makeDirectory($tempExtractDir);
|
||||
}
|
||||
|
||||
$zip = new ZipArchive;
|
||||
|
||||
if ($zip->open($zipFilePath)) {
|
||||
$zip->extractTo($tempExtractDir);
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
File::delete($zipFilePath);
|
||||
|
||||
return $tempExtractDir;
|
||||
}
|
||||
|
||||
public static function copyFiles($module, $tempExtractDir): bool
|
||||
{
|
||||
if (! File::isDirectory(base_path('Modules'))) {
|
||||
File::makeDirectory(base_path('Modules'));
|
||||
}
|
||||
|
||||
if (File::isDirectory(base_path('Modules').'/'.$module)) {
|
||||
File::deleteDirectory(base_path('Modules').'/'.$module);
|
||||
}
|
||||
|
||||
if (! File::copyDirectory($tempExtractDir, base_path('Modules').'/')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File::deleteDirectory($tempExtractDir);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function deleteFiles($json): bool
|
||||
{
|
||||
$files = json_decode($json);
|
||||
|
||||
foreach ($files as $file) {
|
||||
File::delete(base_path($file));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function complete($module, $version): bool
|
||||
{
|
||||
Module::register();
|
||||
|
||||
Artisan::call("module:migrate $module --force");
|
||||
Artisan::call("module:seed $module --force");
|
||||
Artisan::call("module:enable $module");
|
||||
|
||||
$module = ModelsModule::updateOrCreate(
|
||||
['name' => $module],
|
||||
['version' => $version, 'installed' => true, 'enabled' => true]
|
||||
);
|
||||
|
||||
ModuleInstalledEvent::dispatch($module);
|
||||
ModuleEnabledEvent::dispatch($module);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function checkToken(string $token)
|
||||
{
|
||||
$url = 'api/marketplace/ping';
|
||||
$normalizedToken = trim($token) !== '' ? $token : null;
|
||||
$response = static::getRemote($url, ['timeout' => 100, 'track_redirects' => true], $normalizedToken);
|
||||
|
||||
if ($response && $response->getStatusCode() === 200) {
|
||||
return response()->json(json_decode($response->getBody()->getContents()));
|
||||
}
|
||||
|
||||
return response()->json(['error' => 'invalid_token']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user