Lib.hx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 function hashOfAssociativeArray<T>(arr : Dynamic) : Hash<T> {
  49. var h = new Hash<T>();
  50. untyped __php__("foreach($arr as $k => $v) $h->set($k, $v)");
  51. return h;
  52. }
  53. /**
  54. For neko compatibility only.
  55. **/
  56. public static function rethrow( e : Dynamic ) {
  57. throw e;
  58. }
  59. static function appendType(o : Dynamic, path : Array<String>, t : Dynamic) {
  60. var name = path.shift();
  61. if(path.length == 0)
  62. untyped __php__("$o->$name = $t");
  63. else {
  64. var so = untyped __call__("isset", __php__("$o->$name")) ? __php__("$o->$name") : {};
  65. appendType(so, path, t);
  66. untyped __php__("$o->$name = $so");
  67. }
  68. }
  69. public static function getClasses() {
  70. var path : String = null;
  71. var o = {};
  72. untyped __call__('reset', php.Boot.qtypes);
  73. while((path = untyped __call__('key', php.Boot.qtypes)) != null) {
  74. appendType(o, path.split('.'), untyped php.Boot.qtypes[path]);
  75. untyped __call__('next',php.Boot.qtypes);
  76. }
  77. return o;
  78. }
  79. }