Browse Source

[php7] implemented php.Lib.getClasses() (closes #6384)

Alexander Kuzmenko 8 years ago
parent
commit
5636f845c4
2 changed files with 46 additions and 6 deletions
  1. 18 0
      std/php7/Boot.hx
  2. 28 6
      std/php7/Lib.hx

+ 18 - 0
std/php7/Boot.hx

@@ -142,6 +142,24 @@ class Boot {
 		aliases[phpClassName] = haxeClassName;
 	}
 
+	/**
+		Returns a list of currently loaded haxe-generated classes.
+	**/
+	public static function getRegisteredClasses():Array<Class<Dynamic>> {
+		var result = [];
+		Syntax.foreach(aliases, function(phpName, haxeName) {
+			result.push(cast getClass(phpName));
+		});
+		return result;
+	}
+
+	/**
+		Returns a list of phpName=>haxeName for currently loaded haxe-generated classes.
+	**/
+	public static function getRegisteredAliases():NativeAssocArray<String> {
+		return aliases;
+	}
+
 	/**
 		Get Class<T> instance for PHP fully qualified class name (E.g. '\some\pack\MyClass')
 		It's always the same instance for the same `phpClassName`

+ 28 - 6
std/php7/Lib.hx

@@ -22,10 +22,8 @@
 package php;
 
 import haxe.ds.StringMap;
-import php.Global;
-import php.Throwable;
-import php.Syntax;
-import php.Const;
+import php.*;
+import php.reflection.ReflectionClass;
 
 /**
 	Platform-specific PHP Library. Provides some platform-specific functions
@@ -137,9 +135,33 @@ class Lib {
 		throw e;
 	}
 
-	public static function getClasses() {
-		throw "Not implemented";
+	/**
+		Tries to load all compiled php files and returns list of tpes.
+	**/
+	public static function getClasses():Dynamic {
+		if(!loaded) {
+			loaded = true;
+			var reflection = new ReflectionClass(Boot.getPhpName('php.Boot'));
+			loadLib(Global.dirname(reflection.getFileName(), 2));
+		}
+
+		var result:Dynamic = {};
+		Syntax.foreach(Boot.getRegisteredAliases(), function(phpName:String, haxeName:String) {
+			var parts = haxeName.split('.');
+			var obj = result;
+			while(parts.length > 1) {
+				var pack = parts.shift();
+				if(Syntax.getField(obj, pack) == null) {
+					Syntax.setField(obj, pack, {});
+				}
+				obj = Syntax.getField(obj, pack);
+			}
+			Syntax.setField(obj, parts[0], Boot.getClass(phpName));
+		});
+
+		return result;
 	}
+	static var loaded:Bool = false;
 
 	/**
 		Loads types defined in the specified directory.