Skip to content

Commit 40646f8

Browse files
committed
Add java archive testing
1 parent e10ffbd commit 40646f8

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/Core/JavaArchive.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

src/Core/JavaClassReader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class JavaClassReader
1010

1111
public function __construct(string $file)
1212
{
13+
if (!preg_match('/\.class$/', $file, $matches)) {
14+
// Add extension
15+
$file = $file . '.class';
16+
}
1317
$this->handle = fopen($file, 'r');
1418
$this->binaryReader = new JVM\Stream\BinaryReader($this->handle);
1519

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace PHPJava\Imitation\java\io;
3+
4+
use PHPJava\Imitation\java\lang\Exception;
5+
6+
class FileNotFoundException extends Exception
7+
{
8+
}

0 commit comments

Comments
 (0)