Skip to content

Commit 2fe1a8a

Browse files
committed
WIP commit
1 parent 40646f8 commit 2fe1a8a

6 files changed

Lines changed: 95 additions & 38 deletions

File tree

src/Core/JavaArchive.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
class JavaArchive
77
{
8+
const MANIFEST_FILE_NAME = 'META-INF/MANIFEST.MF';
9+
810
private $manifestData = [];
911
private $jarFile;
1012
private $expandedHArchive;
1113
private $files = [];
14+
private $classes = [];
1215

1316
public function __construct(string $jarFile)
1417
{
@@ -21,21 +24,38 @@ public function __construct(string $jarFile)
2124
if ($name[strlen($name) - 1] === '/') {
2225
continue;
2326
}
24-
$this->files[$archive->getNameIndex($i)] = $archive->getFromIndex($i);
27+
$this->files[preg_replace('/\.class$/', '', $name)] = $archive->getFromIndex($i);
2528
}
2629

27-
if (!isset($this->files['META-INF/MANIFEST.MF'])) {
30+
if (!isset($this->files[static::MANIFEST_FILE_NAME])) {
2831
throw new FileNotFoundException('Failed to load Manifest.mf');
2932
}
3033

31-
foreach (explode("\n", $this->files['META-INF/MANIFEST.MF']) as $attribute) {
34+
foreach (explode("\n", $this->files[static::MANIFEST_FILE_NAME]) as $attribute) {
3235
$attribute = str_replace(["\r", "\n"], '', $attribute);
3336
if (empty($attribute)) {
3437
continue;
3538
}
3639
[$name, $value] = explode(':', $attribute);
3740
$this->manifestData[strtolower($name)] = trim($value);
3841
}
42+
43+
$this->files = array_filter(
44+
$this->files,
45+
function ($fileName) {
46+
return $fileName !== static::MANIFEST_FILE_NAME;
47+
},
48+
ARRAY_FILTER_USE_KEY
49+
);
50+
51+
foreach ($this->files as $className => $code) {
52+
$this->classes[$className] = new JavaClass(new JavaClassInlineFileReader(
53+
$className,
54+
$code
55+
));
56+
}
57+
58+
var_dump($this->classes);
3959
}
4060

4161
public function __debugInfo()

src/Core/JavaClassFileReader.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace PHPJava\Core;
3+
4+
use PHPJava\Utilities\ClassResolver;
5+
6+
class JavaClassFileReader implements JavaClassReader
7+
{
8+
private $handle;
9+
private $binaryReader;
10+
11+
public function __construct(string $file)
12+
{
13+
if (!preg_match('/\.class$/', $file, $matches)) {
14+
// Add extension
15+
$file = $file . '.class';
16+
}
17+
$this->handle = fopen($file, 'r');
18+
$this->binaryReader = new JVM\Stream\BinaryReader($this->handle);
19+
20+
// Add resolving path
21+
ClassResolver::add(
22+
[
23+
[ClassResolver::RESOURCE_TYPE_FILE, dirname($file)],
24+
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],
25+
]
26+
);
27+
}
28+
29+
public function getBinaryReader(): JVM\Stream\BinaryReader
30+
{
31+
return $this->binaryReader;
32+
}
33+
34+
public function __toString(): string
35+
{
36+
return stream_get_meta_data($this->handle)['uri'];
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace PHPJava\Core;
3+
4+
use PHPJava\Utilities\ClassResolver;
5+
6+
class JavaClassInlineFileReader implements JavaClassReader
7+
{
8+
private $fileName;
9+
private $handle;
10+
private $binaryReader;
11+
12+
public function __construct(string $fileName, string $code)
13+
{
14+
$this->fileName = $fileName;
15+
$this->handle = fopen('php://memory', 'rw');
16+
fwrite($this->handle, $code);
17+
rewind($this->handle);
18+
$this->binaryReader = new JVM\Stream\BinaryReader($this->handle);
19+
}
20+
21+
public function getBinaryReader(): JVM\Stream\BinaryReader
22+
{
23+
return $this->binaryReader;
24+
}
25+
26+
public function __toString(): string
27+
{
28+
return $this->fileName;
29+
}
30+
}

src/Core/JavaClassReader.php

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,7 @@
11
<?php
22
namespace PHPJava\Core;
33

4-
use PHPJava\Utilities\ClassResolver;
5-
6-
class JavaClassReader
4+
interface JavaClassReader
75
{
8-
private $handle;
9-
private $binaryReader;
10-
11-
public function __construct(string $file)
12-
{
13-
if (!preg_match('/\.class$/', $file, $matches)) {
14-
// Add extension
15-
$file = $file . '.class';
16-
}
17-
$this->handle = fopen($file, 'r');
18-
$this->binaryReader = new JVM\Stream\BinaryReader($this->handle);
19-
20-
// Add resolving path
21-
ClassResolver::add(
22-
[
23-
[ClassResolver::RESOURCE_TYPE_FILE, dirname($file)],
24-
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],
25-
]
26-
);
27-
}
28-
29-
public function getBinaryReader(): JVM\Stream\BinaryReader
30-
{
31-
return $this->binaryReader;
32-
}
336

34-
public function __toString(): string
35-
{
36-
return stream_get_meta_data($this->handle)['uri'];
37-
}
387
}

src/Utilities/ClassResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace PHPJava\Utilities;
33

44
use PHPJava\Core\JavaClass;
5-
use PHPJava\Core\JavaClassReader;
5+
use PHPJava\Core\JavaClassFileReader;
66

77
class ClassResolver
88
{
@@ -39,7 +39,7 @@ public static function resolve(string $javaPath, JavaClass $class = null): array
3939
return static::$resolvedPaths[$key];
4040
}
4141
if (is_file($path)) {
42-
$initiatedClass = new JavaClass(new JavaClassReader($path));
42+
$initiatedClass = new JavaClass(new JavaClassFileReader($path));
4343
if (strpos($relativePath, '$') !== false && $class !== null) {
4444
$initiatedClass->setParentClass($class);
4545
}

tests/Base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function setUp(): void
1818
foreach ($this->fixtures as $fixture) {
1919
exec('javac -classpath ' . $pathRoot . ':' . $pathRoot . 'caches -encoding UTF8 ' . $pathRoot . str_replace(['../', './'], '', $fixture) . '.java -d ' . __DIR__ . '/caches');
2020
$this->initiatedJavaClasses[$fixture] = new \PHPJava\Core\JavaClass(
21-
new \PHPJava\Core\JavaClassReader(
21+
new \PHPJava\Core\JavaClassFileReader(
2222
$this->getClassName($fixture)
2323
)
2424
);

0 commit comments

Comments
 (0)