39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Marketplace\Services;
|
|
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
final class PackageManifest
|
|
{
|
|
public function parse(array|string $manifest): array
|
|
{
|
|
if (is_string($manifest)) {
|
|
$decoded = json_decode($manifest, true);
|
|
if (! is_array($decoded)) {
|
|
throw ValidationException::withMessages(['manifest' => 'The package manifest is not valid JSON.']);
|
|
}
|
|
$manifest = $decoded;
|
|
}
|
|
foreach (['name', 'type', 'version'] as $field) {
|
|
if (! filled(Arr::get($manifest, $field))) {
|
|
throw ValidationException::withMessages(['manifest' => "Manifest field [$field] is required."]);
|
|
}
|
|
}
|
|
if (! in_array($manifest['type'], config('marketplace.supported_types', []), true)) {
|
|
throw ValidationException::withMessages(['manifest' => 'Unsupported marketplace package type.']);
|
|
}
|
|
$manifest['requires'] = $manifest['requires'] ?? [];
|
|
$manifest['content_manager'] = $manifest['content_manager'] ?? '*';
|
|
$manifest['laravel'] = $manifest['laravel'] ?? '*';
|
|
|
|
return $manifest;
|
|
}
|
|
|
|
public function fromFile(string $path): array
|
|
{
|
|
return $this->parse((string) file_get_contents($path));
|
|
}
|
|
}
|