|
| 1 | +<?php |
| 2 | +namespace PHPJava\Core; |
| 3 | + |
| 4 | +use PHPJava\Imitation\java\io\FileNotFoundException; |
| 5 | + |
| 6 | +class JavaArchive |
| 7 | +{ |
| 8 | + private $manifestData = []; |
| 9 | + private $jarFile; |
| 10 | + private $expandedHArchive; |
| 11 | + private $files = []; |
| 12 | + |
| 13 | + public function __construct(string $jarFile) |
| 14 | + { |
| 15 | + $this->jarFile = $jarFile; |
| 16 | + $archive = new \ZipArchive(); |
| 17 | + $archive->open($jarFile); |
| 18 | + $this->expandedHArchive = $archive; |
| 19 | + for ($i = 0; $i < $this->expandedHArchive->numFiles; $i++) { |
| 20 | + $name = $archive->getNameIndex($i); |
| 21 | + if ($name[strlen($name) - 1] === '/') { |
| 22 | + continue; |
| 23 | + } |
| 24 | + $this->files[$archive->getNameIndex($i)] = $archive->getFromIndex($i); |
| 25 | + } |
| 26 | + |
| 27 | + if (!isset($this->files['META-INF/MANIFEST.MF'])) { |
| 28 | + throw new FileNotFoundException('Failed to load Manifest.mf'); |
| 29 | + } |
| 30 | + |
| 31 | + foreach (explode("\n", $this->files['META-INF/MANIFEST.MF']) as $attribute) { |
| 32 | + $attribute = str_replace(["\r", "\n"], '', $attribute); |
| 33 | + if (empty($attribute)) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + [$name, $value] = explode(':', $attribute); |
| 37 | + $this->manifestData[strtolower($name)] = trim($value); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function __debugInfo() |
| 42 | + { |
| 43 | + return [ |
| 44 | + 'version' => $this->getVersion(), |
| 45 | + 'createdBy' => $this->getCreatedBy(), |
| 46 | + 'entrypoint' => $this->getEntryPoint(), |
| 47 | + 'file' => $this->jarFile, |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + public function getVersion() |
| 52 | + { |
| 53 | + return $this->manifestData['manifest-version'] ?? null; |
| 54 | + } |
| 55 | + |
| 56 | + public function getCreatedBy() |
| 57 | + { |
| 58 | + return $this->manifestData['created-by'] ?? null; |
| 59 | + } |
| 60 | + |
| 61 | + public function getEntryPoint() |
| 62 | + { |
| 63 | + return $this->manifestData['main-class'] ?? null; |
| 64 | + } |
| 65 | +} |
0 commit comments