| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Introspection; |
| 4: | |
| 5: | use JsonException; |
| 6: | use LogicException; |
| 7: | |
| 8: | final readonly class Ui5PackageMeta |
| 9: | { |
| 10: | public function __construct( |
| 11: | private string $name, |
| 12: | private string $version, |
| 13: | private string $builder, |
| 14: | ) |
| 15: | { |
| 16: | } |
| 17: | |
| 18: | |
| 19: | |
| 20: | public function getName(): string |
| 21: | { |
| 22: | return $this->name; |
| 23: | } |
| 24: | |
| 25: | public function getVersion(): string |
| 26: | { |
| 27: | return $this->version; |
| 28: | } |
| 29: | |
| 30: | public function getBuilder(): string |
| 31: | { |
| 32: | return $this->builder; |
| 33: | } |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public static function fromPackageJson(string $path): self |
| 41: | { |
| 42: | $srcPath = "{$path}/package.json"; |
| 43: | |
| 44: | if (!file_exists($srcPath)) { |
| 45: | throw new LogicException("package.json not found at {$srcPath}"); |
| 46: | } |
| 47: | |
| 48: | $data = json_decode( |
| 49: | file_get_contents($srcPath), |
| 50: | true, |
| 51: | 512, |
| 52: | JSON_THROW_ON_ERROR |
| 53: | ); |
| 54: | |
| 55: | return new self( |
| 56: | name: $data['name'] ?? 'unknown', |
| 57: | version: $data['version'] ?? '0.0.0', |
| 58: | builder: $data['scripts']['build'] ?? 'ui5 build --clean-dest' |
| 59: | ); |
| 60: | } |
| 61: | } |
| 62: | |