Reflect.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import php.Boot;
  23. import php.Syntax;
  24. import php.Closure;
  25. import php.Const;
  26. import php.NativeAssocArray;
  27. import haxe.Constraints;
  28. using php.Global;
  29. @:coreApi class Reflect {
  30. public static function hasField(o:Dynamic, field:String):Bool {
  31. if (!o.is_object())
  32. return false;
  33. if (o.property_exists(field))
  34. return true;
  35. if (Boot.isClass(o)) {
  36. var phpClassName = Boot.castClass(o).phpClassName;
  37. return Global.property_exists(phpClassName, field)
  38. || Global.method_exists(phpClassName, field)
  39. || Global.defined('$phpClassName::$field');
  40. }
  41. return false;
  42. }
  43. public static function field(o:Dynamic, field:String):Dynamic {
  44. if (o.is_string()) {
  45. return Syntax.field(Boot.dynamicString(o), field);
  46. }
  47. if (!o.is_object())
  48. return null;
  49. if (field == '' && Const.PHP_VERSION_ID < 70100) {
  50. return Syntax.coalesce(Syntax.array(o)[field], null);
  51. }
  52. if (o.property_exists(field)) {
  53. return Syntax.field(o, field);
  54. }
  55. if (o.method_exists(field)) {
  56. return Boot.getInstanceClosure(o, field);
  57. }
  58. if (Boot.isClass(o)) {
  59. var phpClassName = Boot.castClass(o).phpClassName;
  60. if (Global.defined('$phpClassName::$field')) {
  61. return Global.constant('$phpClassName::$field');
  62. }
  63. if (Global.property_exists(phpClassName, field)) {
  64. return Syntax.field(o, field);
  65. }
  66. if (Global.method_exists(phpClassName, field)) {
  67. return Boot.getStaticClosure(phpClassName, field);
  68. }
  69. }
  70. return null;
  71. }
  72. public static function setField(o:Dynamic, field:String, value:Dynamic):Void {
  73. Syntax.setField(o, field, value);
  74. }
  75. public static function getProperty(o:Dynamic, field:String):Dynamic {
  76. if (o.is_object()) {
  77. if (Boot.isClass(o)) {
  78. var phpClassName = Boot.castClass(o).phpClassName;
  79. if (Boot.hasGetter(phpClassName, field)) {
  80. return Syntax.staticCall(phpClassName, 'get_$field');
  81. }
  82. } else if (Boot.hasGetter(Global.get_class(o), field)) {
  83. return Syntax.call(o, 'get_$field');
  84. }
  85. }
  86. return Reflect.field(o, field);
  87. }
  88. public static function setProperty(o:Dynamic, field:String, value:Dynamic):Void {
  89. if (o.is_object()) {
  90. if (Boot.hasSetter(Global.get_class(o), field)) {
  91. Syntax.call(o, 'set_$field', value);
  92. } else {
  93. Syntax.setField(o, field, value);
  94. }
  95. }
  96. }
  97. public static function callMethod(o:Dynamic, func:Function, args:Array<Dynamic>):Dynamic {
  98. return Global.call_user_func_array(func, @:privateAccess args.arr);
  99. }
  100. public static function fields(o:Dynamic):Array<String> {
  101. if (Global.is_object(o)) {
  102. return @:privateAccess Array.wrap(Global.get_object_vars(o).array_keys());
  103. }
  104. return [];
  105. }
  106. public static inline function isFunction(f:Dynamic):Bool {
  107. return Boot.isFunction(f);
  108. }
  109. public static function compare<T>(a:T, b:T):Int {
  110. if (a == b)
  111. return 0;
  112. if (Global.is_string(a)) {
  113. return Global.strcmp(cast a, cast b);
  114. } else {
  115. return ((cast a) > (cast b) ? 1 : -1);
  116. }
  117. }
  118. public static function compareMethods(f1:Dynamic, f2:Dynamic):Bool {
  119. if (Boot.isHxClosure(f1) && Boot.isHxClosure(f2)) {
  120. return f1.equals(f2);
  121. } else {
  122. return f1 == f2;
  123. }
  124. }
  125. public static function isObject(v:Dynamic):Bool {
  126. if (Boot.isEnumValue(v)) {
  127. return false;
  128. } else {
  129. return v.is_object() || v.is_string();
  130. }
  131. }
  132. public static inline function isEnumValue(v:Dynamic):Bool {
  133. return Boot.isEnumValue(v);
  134. }
  135. public static function deleteField(o:Dynamic, field:String):Bool {
  136. if (hasField(o, field)) {
  137. Global.unset(Syntax.field(o, field));
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. public static function copy<T>(o:Null<T>):Null<T> {
  144. if (Boot.isAnon(o)) {
  145. return Syntax.clone(o);
  146. } else {
  147. return null;
  148. }
  149. }
  150. @:overload(function(f:Array<Dynamic>->Void):Dynamic {})
  151. public static function makeVarArgs(f:Array<Dynamic>->Dynamic):Dynamic {
  152. return function() {
  153. return Global.call_user_func(f, @:privateAccess Array.wrap(Global.func_get_args()));
  154. }
  155. }
  156. }