2
0

Lib.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package php;
  2. class Lib {
  3. /**
  4. Print the specified value on the default output.
  5. **/
  6. public static function print( v : Dynamic ) : Void {
  7. untyped __call__("echo", Std.string(v));
  8. }
  9. /**
  10. Print the specified value on the default output followed by a newline character.
  11. **/
  12. public static function println( v : Dynamic ) : Void {
  13. print(v);
  14. print("\n");
  15. }
  16. public static function dump(v : Dynamic) : Void {
  17. untyped __call__("var_dump", v);
  18. }
  19. /**
  20. Serialize using native PHP serialization. This will return a Binary string that can be
  21. stored for long term usage.
  22. **/
  23. public static function serialize( v : Dynamic ) : String {
  24. return untyped __call__("serialize", v);
  25. }
  26. /**
  27. Unserialize a string using native PHP serialization. See [serialize].
  28. **/
  29. public static function unserialize( s : String ) : Dynamic {
  30. return untyped __call__("unserialize", s);
  31. }
  32. public static function extensionLoaded(name : String) {
  33. return untyped __call__("extension_loaded", name);
  34. }
  35. public static function isCli() : Bool {
  36. return untyped __php__("0 == strncasecmp(PHP_SAPI, 'cli', 3)");
  37. }
  38. public static function exit(?msg : String) {
  39. return untyped __call__("exit", msg);
  40. }
  41. public static function exitCode(code : Int) {
  42. return untyped __call__("exit", code);
  43. }
  44. public static function printFile(file : String) {
  45. var h = untyped __call__("fopen", file, "r");
  46. return untyped __call__("fpassthru", h);
  47. }
  48. public static inline function haxeToNativeArray(a : Array<Dynamic>) : NativeArray {
  49. return untyped a.a;
  50. }
  51. public static inline function nativeArrayToHaxe(a : NativeArray) : Array<Dynamic> {
  52. return untyped __call__("new _hx_array", a);
  53. }
  54. public static function hashOfAssociativeArray<T>(arr : NativeArray) : Hash<T> {
  55. var h = new Hash<T>();
  56. untyped __php__("foreach($arr as $k => $v) $h->set($k, $v)");
  57. return h;
  58. }
  59. /**
  60. For neko compatibility only.
  61. **/
  62. public static function rethrow( e : Dynamic ) {
  63. throw e;
  64. }
  65. static function appendType(o : Dynamic, path : Array<String>, t : Dynamic) {
  66. var name = path.shift();
  67. if(path.length == 0)
  68. untyped __php__("$o->$name = $t");
  69. else {
  70. var so = untyped __call__("isset", __php__("$o->$name")) ? __php__("$o->$name") : {};
  71. appendType(so, path, t);
  72. untyped __php__("$o->$name = $so");
  73. }
  74. }
  75. public static function getClasses() {
  76. var path : String = null;
  77. var o = {};
  78. untyped __call__('reset', php.Boot.qtypes);
  79. while((path = untyped __call__('key', php.Boot.qtypes)) != null) {
  80. appendType(o, path.split('.'), untyped php.Boot.qtypes[path]);
  81. untyped __call__('next',php.Boot.qtypes);
  82. }
  83. return o;
  84. }
  85. }