ソースを参照

php : added support for Reflect.getProperty/setProperty

Franco Ponticelli 13 年 前
コミット
3db8a8c767
2 ファイル変更37 行追加5 行削除
  1. 22 0
      genphp.ml
  2. 15 5
      std/php/_std/Reflect.hx

+ 22 - 0
genphp.ml

@@ -1989,7 +1989,29 @@ let generate_class ctx c =
 	);
 
 	List.iter (generate_field ctx true) c.cl_ordered_statics;
+	
+	let gen_props props = 
+		String.concat "," (List.map (fun (p,v) -> "\"" ^ p ^ "\" => \"" ^ v ^ "\"") props)
+	in
 
+	let rec fields c =
+		let list = Codegen.get_properties (c.cl_ordered_statics @ c.cl_ordered_fields) in
+		match c.cl_super with
+		| Some (csup, _) ->
+			list @ fields csup
+		| None ->
+			list
+	in
+	
+	(match fields c with
+	| [] ->
+		()
+	| props ->
+		newline ctx;
+		print ctx "static $__properties__ = array(%s)" (gen_props props);
+	);
+		
+		
 	cl();
 	newline ctx;
 

+ 15 - 5
std/php/_std/Reflect.hx

@@ -37,14 +37,24 @@
 		untyped __setfield__(o, field, value);
 	}
 
-	public static inline function getProperty( o : Dynamic, field : String ) : Dynamic {
-		return Reflect.field(o,field);
+	public static function getProperty( o : Dynamic, field : String ) : Dynamic {
+		if (null == o) return null;
+		var cls : String = Std.is(o, Class) ? untyped __php__("$o->__tname__") : untyped __call__("get_class", o);
+		if (untyped __php__("$cls::$__properties__ && isset($cls::$__properties__['get_'.$field]) && ($field = $cls::$__properties__['get_'.$field])"))
+			return untyped __php__("$o->$field()");
+		else
+			return untyped __php__("$o->$field");
 	}
 
-	public static inline function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void {
-		setField(o,field,value);
+	public static function setProperty( o : Dynamic, field : String, value : Dynamic ) : Void untyped {
+		if (null == o) return null;
+		var cls : String = Std.is(o, Class) ? untyped __php__("$o->__tname__") : untyped __call__("get_class", o);
+		if (untyped __php__("$cls::$__properties__ && isset($cls::$__properties__['set_'.$field]) && ($field = $cls::$__properties__['set_'.$field])"))
+			return untyped __php__("$o->$field($value)");
+		else
+			return untyped __php__("$o->$field = $value");
 	}
-	
+
 	public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic untyped {
 		if (__call__("is_string", o) && !__call__("is_array", func)) {
 			return __call__("call_user_func_array", field(o, func), __field__(args, "»a"));