Lib.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 printFile(file : String) {
  39. return untyped __call__("fpassthru", __call__("fopen", file, "r"));
  40. }
  41. public static function toPhpArray(a : Array<Dynamic>) : NativeArray {
  42. return untyped __field__(a, '»a');
  43. }
  44. public static inline function toHaxeArray(a : NativeArray) : Array<Dynamic> {
  45. return untyped __call__("new _hx_array", a);
  46. }
  47. public static function hashOfAssociativeArray<T>(arr : NativeArray) : Hash<T> {
  48. var h = new Hash<T>();
  49. untyped __php__("reset($arr); while(list($k, $v) = each($arr)) $h->set($k, $v)");
  50. return h;
  51. }
  52. public static function associativeArrayOfHash(hash : Hash<Dynamic>) : NativeArray {
  53. return untyped hash.h;
  54. }
  55. /**
  56. For neko compatibility only.
  57. **/
  58. public static function rethrow( e : Dynamic ) {
  59. if(Std.is(e, Exception)) {
  60. var __rtex__ = e;
  61. untyped __php__("throw $__rtex__");
  62. }
  63. else 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. /**
  86. * Loads types defined in the specified directory.
  87. */
  88. public static function loadLib(pathToLib : String) : Void
  89. {
  90. var prefix = untyped __prefix__();
  91. untyped __php__("$_hx_types_array = array();
  92. $_hx_cache_content = '';
  93. //Calling this function will put all types present in the specified types in the $_hx_types_array
  94. _hx_build_paths($pathToLib, $_hx_types_array, array(), $prefix);
  95. for($i=0;$i<count($_hx_types_array);$i++) {
  96. //For every type that has been found, create its description
  97. $t = null;
  98. if($_hx_types_array[$i]['type'] == 0) {
  99. $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  100. } else if($_hx_types_array[$i]['type'] == 1) {
  101. $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  102. } else if($_hx_types_array[$i]['type'] == 2) {
  103. $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  104. } else if($_hx_types_array[$i]['type'] == 3) {
  105. $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  106. }
  107. //Register the type
  108. if(!array_key_exists($t->__qname__, php_Boot::$qtypes)) {
  109. _hx_register_type($t);
  110. }
  111. }
  112. ");
  113. }
  114. }