11<?php
22namespace PHPJava \Kernel \Resolvers ;
33
4- use PHPJava \Core \JavaArchive ;
54use PHPJava \Core \JavaClass ;
6- use PHPJava \Core \JavaClassInterface ;
7- use PHPJava \Core \JavaCompiledClass ;
8- use PHPJava \Core \JavaGenericClassInterface ;
95use PHPJava \Core \JVM \Parameters \Runtime ;
10- use PHPJava \Core \Stream \Reader \FileReader ;
11- use PHPJava \Packages \java \lang \ClassNotFoundException ;
126
137class ClassResolver
148{
@@ -20,10 +14,6 @@ class ClassResolver
2014 const RESOURCE_TYPE_CLASS = 'RESOURCE_TYPE_CLASS ' ;
2115 const RESOURCE_TYPE_INNER_CLASS = 'RESOURCE_TYPE_INNER_CLASS ' ;
2216
23- // resolved types
24- const RESOLVED_TYPE_CLASS = 'RESOLVED_TYPE_CLASS ' ;
25- const RESOLVED_TYPE_PACKAGES = 'RESOLVED_TYPE_PACKAGES ' ;
26-
2717 /**
2818 * @var array
2919 */
@@ -39,110 +29,6 @@ class ClassResolver
3929 */
4030 private $ options = [];
4131
42- public function __construct (array $ options )
43- {
44- $ this ->options = $ options ;
45- }
46-
47- public function resolve (string $ javaPath , JavaClassInterface $ class = null , bool $ instantiation = true ): array
48- {
49- $ javaPath = str_replace ('/ ' , '. ' , $ javaPath );
50- $ namespaces = explode ('. ' , $ javaPath );
51- $ buildClassPath = [];
52- foreach ($ namespaces as $ namespace ) {
53- $ buildClassPath [] = static ::MAPS [$ namespace ] ?? $ namespace ;
54- }
55-
56- // resolve something approaching
57- $ relativePath = implode ('/ ' , $ namespaces );
58-
59- foreach (static ::$ resolves as [$ resourceType , $ value ]) {
60- switch ($ resourceType ) {
61- case static ::RESOURCE_TYPE_INNER_CLASS :
62- // TODO: Implement here
63- break ;
64- case static ::RESOURCE_TYPE_JAR :
65- /**
66- * @var JavaArchive $value
67- */
68- try {
69- return $ this ->resolvedPaths [] = [
70- static ::RESOLVED_TYPE_CLASS ,
71- $ value ->getClassByName ($ relativePath ),
72- ];
73- } catch (ClassNotFoundException $ e ) {
74- }
75- break ;
76- case static ::RESOURCE_TYPE_FILE :
77- $ path = realpath ($ value . '/ ' . $ relativePath . '.class ' );
78- if ($ path === false ) {
79- break ;
80- }
81- if (!$ instantiation ) {
82- foreach ($ this ->resolvedPaths as [$ resolvedPath , $ class ]) {
83- if ($ relativePath === $ class ->getClassName ()) {
84- return [$ resolvedPath , $ class ];
85- }
86- }
87- }
88- /**
89- * @var JavaClass $initiatedClass
90- */
91- if (is_file ($ path )) {
92- $ initiatedClass = new JavaClass (
93- new JavaCompiledClass (
94- new FileReader ($ path ),
95- $ this ->options
96- )
97- );
98-
99- return $ this ->resolvedPaths [] = [
100- static ::RESOLVED_TYPE_CLASS ,
101- $ initiatedClass ,
102- ];
103- }
104- break ;
105- case static ::RESOURCE_TYPE_CLASS :
106- if (!$ instantiation ) {
107- foreach ($ this ->resolvedPaths as [$ resolvedPath , $ class ]) {
108- if ($ value ->getClassName () === $ class ->getClassName ()) {
109- return [$ resolvedPath , $ class ];
110- }
111- }
112- }
113-
114- /**
115- * @var JavaGenericClassInterface $value
116- */
117- try {
118- return $ this ->resolvedPaths [] = [
119- static ::RESOLVED_TYPE_CLASS ,
120- new JavaClass ($ value ),
121- ];
122- } catch (ClassNotFoundException $ e ) {
123- }
124- break ;
125- }
126- }
127-
128- $ className = Runtime::PHP_PACKAGES_DIRECTORY . '\\' . implode ('\\' , $ buildClassPath );
129-
130- if (!class_exists ($ className )) {
131- throw new ClassNotFoundException (
132- str_replace (
133- [Runtime::PHP_PACKAGES_DIRECTORY . '\\' , '\\' ],
134- ['' , '. ' ],
135- $ className
136- ) . ' class does not exist. '
137- );
138- }
139-
140- return [
141- static ::RESOLVED_TYPE_PACKAGES ,
142- $ className ,
143- ];
144- }
145-
14632 public static function getClassPaths (): array
14733 {
14834 return static ::$ resolves ;
@@ -161,24 +47,4 @@ public static function add($valuesOrResourceType = self::RESOURCE_TYPE_FILE, $va
16147 }
16248 static ::$ resolves [] = [$ valuesOrResourceType , $ value ];
16349 }
164-
165- public static function resolveNameByPath ($ path ): string
166- {
167- $ names = explode (
168- '. ' ,
169- str_replace (
170- [Runtime::PHP_PACKAGES_DIRECTORY . '\\' , '\\' ],
171- ['' , '. ' ],
172- '\\' . ltrim (get_class ($ path ))
173- )
174- );
175-
176- $ string = [];
177-
178- foreach ($ names as $ name ) {
179- $ string [] = array_flip (static ::MAPS )[$ name ] ?? $ name ;
180- }
181-
182- return implode ('. ' , $ string );
183- }
18450}
0 commit comments