Skip to content

Commit fc7332d

Browse files
committed
Support classpath
1 parent eeb5d06 commit fc7332d

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/Core/JavaArchive.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPJava\Imitation\java\io\FileNotFoundException;
66
use PHPJava\Imitation\java\lang\ClassNotFoundException;
77
use PHPJava\Utilities\ClassResolver;
8+
use PHPJava\Utilities\FileTypeResolver;
89

910
class JavaArchive
1011
{
@@ -76,6 +77,29 @@ function ($fileName) {
7677
$code
7778
));
7879
}
80+
81+
$currentDirectory = getcwd();
82+
foreach ($this->getClassPaths() as $classPath) {
83+
$resolvePath = $classPath[0] === '/' ? $classPath : ($currentDirectory . '/' . $classPath);
84+
$realpath = realpath($resolvePath);
85+
if ($realpath === false) {
86+
throw new FileNotFoundException($classPath . ' does not exist.');
87+
}
88+
89+
$value = $realpath;
90+
91+
switch ($fileType = FileTypeResolver::resolve($resolvePath)) {
92+
case ClassResolver::RESOLVED_TYPE_CLASS:
93+
$value = new JavaClassFileReader($value);
94+
break;
95+
case ClassResolver::RESOURCE_TYPE_JAR:
96+
$value = new JavaArchive($value);
97+
break;
98+
case ClassResolver::RESOURCE_TYPE_FILE:
99+
break;
100+
}
101+
ClassResolver::add($fileType, $value);
102+
}
79103
}
80104

81105
/**
@@ -129,7 +153,7 @@ public function getEntryPointName(): ?string
129153
public function getClassPaths(): array
130154
{
131155
$classPaths = [];
132-
foreach (explode(' ', $this->manifestData['class-path'] ?? []) as $path) {
156+
foreach (explode(' ', $this->manifestData['class-path'] ?? '') as $path) {
133157
if (empty($path)) {
134158
continue;
135159
}

src/Utilities/ClassResolver.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PHPJava\Core\JavaArchive;
55
use PHPJava\Core\JavaClass;
66
use PHPJava\Core\JavaClassFileReader;
7+
use PHPJava\Core\JavaClassReaderInterface;
78
use PHPJava\Imitation\java\lang\ClassNotFoundException;
89

910
class ClassResolver
@@ -15,7 +16,8 @@ class ClassResolver
1516

1617
// resource types
1718
const RESOURCE_TYPE_FILE = 'RESOURCE_TYPE_FILE';
18-
const RESOURCE_TYPE_JAR = 'RESOLVED_TYPE_JAR';
19+
const RESOURCE_TYPE_JAR = 'RESOURCE_TYPE_JAR';
20+
const RESOURCE_TYPE_CLASS = 'RESOURCE_TYPE_CLASS';
1921

2022
// resolved types
2123
const RESOLVED_TYPE_CLASS = 'RESOLVED_TYPE_CLASS';
@@ -64,6 +66,18 @@ public static function resolve(string $javaPath, JavaClass $class = null): array
6466
} catch (ClassNotFoundException $e) {
6567
}
6668
break;
69+
case static::RESOLVED_TYPE_CLASS:
70+
/**
71+
* @var JavaClassReaderInterface $value
72+
*/
73+
try {
74+
return $resolvedPaths[] = [
75+
static::RESOLVED_TYPE_CLASS,
76+
new JavaClass($value),
77+
];
78+
} catch (ClassNotFoundException $e) {
79+
}
80+
break;
6781
}
6882
}
6983

src/Utilities/FileTypeResolver.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace PHPJava\Utilities;
3+
4+
use PHPJava\Core\JavaClass;
5+
use PHPJava\Exceptions\TypeException;
6+
7+
class FileTypeResolver
8+
{
9+
public static function resolve($path): string
10+
{
11+
if (preg_match('/\.class$/', $path)) {
12+
return ClassResolver::RESOLVED_TYPE_CLASS;
13+
}
14+
if (preg_match('/\.jar$/', $path)) {
15+
return ClassResolver::RESOURCE_TYPE_JAR;
16+
}
17+
if (is_dir($path)) {
18+
return ClassResolver::RESOURCE_TYPE_FILE;
19+
}
20+
}
21+
22+
}

0 commit comments

Comments
 (0)