Lib.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 h.h = arr;
  50. return h;
  51. }
  52. public static function associativeArrayOfHash(hash : Hash<Dynamic>) : NativeArray {
  53. return untyped hash.h;
  54. }
  55. public static function objectOfAssociativeArray(arr : NativeArray) : Dynamic {
  56. untyped __php__("foreach($arr as $key => $value){
  57. if(is_array($value)) $arr[$key] = php_Lib::objectOfAssociativeArray($value);
  58. }");
  59. return untyped __call__("_hx_anonymous", arr);
  60. }
  61. public static function associativeArrayOfObject(ob : Dynamic) : NativeArray {
  62. return untyped __php__("(array) $ob");
  63. }
  64. /**
  65. * See the documentation for the equivalent PHP function for details on usage:
  66. * http://php.net/manual/en/function.mail.php
  67. * @param to
  68. * @param subject
  69. * @param message
  70. * @param ?additionalHeaders
  71. * @param ?additionalParameters
  72. */
  73. public static function mail(to : String, subject : String, message : String, ?additionalHeaders : String, ?additionalParameters : String) : Bool
  74. {
  75. if(null != additionalParameters)
  76. return untyped __call__("mail", to, subject, message, additionalHeaders, additionalParameters);
  77. else if(null != additionalHeaders)
  78. return untyped __call__("mail", to, subject, message, additionalHeaders);
  79. else
  80. return untyped __call__("mail", to, subject, message);
  81. }
  82. /**
  83. For neko compatibility only.
  84. **/
  85. public static function rethrow( e : Dynamic ) {
  86. if(Std.is(e, Exception)) {
  87. var __rtex__ = e;
  88. untyped __php__("throw $__rtex__");
  89. }
  90. else throw e;
  91. }
  92. static function appendType(o : Dynamic, path : Array<String>, t : Dynamic) {
  93. var name = path.shift();
  94. if(path.length == 0)
  95. untyped __php__("$o->$name = $t");
  96. else {
  97. var so = untyped __call__("isset", __php__("$o->$name")) ? __php__("$o->$name") : {};
  98. appendType(so, path, t);
  99. untyped __php__("$o->$name = $so");
  100. }
  101. }
  102. public static function getClasses() {
  103. var path : String = null;
  104. var o = {};
  105. untyped __call__('reset', php.Boot.qtypes);
  106. while((path = untyped __call__('key', php.Boot.qtypes)) != null) {
  107. appendType(o, path.split('.'), untyped php.Boot.qtypes[path]);
  108. untyped __call__('next',php.Boot.qtypes);
  109. }
  110. return o;
  111. }
  112. /**
  113. * Loads types defined in the specified directory.
  114. */
  115. public static function loadLib(pathToLib : String) : Void
  116. {
  117. var prefix = untyped __prefix__();
  118. untyped __php__("$_hx_types_array = array();
  119. $_hx_cache_content = '';
  120. //Calling this function will put all types present in the specified types in the $_hx_types_array
  121. _hx_build_paths($pathToLib, $_hx_types_array, array(), $prefix);
  122. for($i=0;$i<count($_hx_types_array);$i++) {
  123. //For every type that has been found, create its description
  124. $t = null;
  125. if($_hx_types_array[$i]['type'] == 0) {
  126. $t = new _hx_class($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  127. } else if($_hx_types_array[$i]['type'] == 1) {
  128. $t = new _hx_enum($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  129. } else if($_hx_types_array[$i]['type'] == 2) {
  130. $t = new _hx_interface($_hx_types_array[$i]['phpname'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  131. } else if($_hx_types_array[$i]['type'] == 3) {
  132. $t = new _hx_class($_hx_types_array[$i]['name'], $_hx_types_array[$i]['qname'], $_hx_types_array[$i]['path']);
  133. }
  134. //Register the type
  135. if(!array_key_exists($t->__qname__, php_Boot::$qtypes)) {
  136. _hx_register_type($t);
  137. }
  138. }
  139. ");
  140. }
  141. }