Lib.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. package php;
  23. import haxe.ds.StringMap;
  24. import php.*;
  25. import php.reflection.ReflectionClass;
  26. /**
  27. Platform-specific PHP Library. Provides some platform-specific functions
  28. for the PHP target, such as conversion from Haxe types to native types
  29. and vice-versa.
  30. **/
  31. class Lib {
  32. /**
  33. Print the specified value on the default output.
  34. **/
  35. public static inline function print(v:Dynamic):Void {
  36. Global.echo(Std.string(v));
  37. }
  38. /**
  39. Print the specified value on the default output followed by
  40. a newline character.
  41. **/
  42. public static function println(v:Dynamic):Void {
  43. print(v);
  44. print("\n");
  45. }
  46. /**
  47. Displays structured information about one or more expressions
  48. that includes its type and value. Arrays and objects are
  49. explored recursively with values indented to show structure.
  50. **/
  51. public static inline function dump(v:Dynamic):Void {
  52. Global.var_dump(v);
  53. }
  54. /**
  55. Serialize using native PHP serialization. This will return a binary
  56. `String` that can be stored for long term usage.
  57. **/
  58. public static inline function serialize(v:Dynamic):String {
  59. return Global.serialize(v);
  60. }
  61. /**
  62. Unserialize a `String` using native PHP serialization. See `php.Lib.serialize()`.
  63. **/
  64. public static inline function unserialize(s:String):Dynamic {
  65. return Global.unserialize(s);
  66. }
  67. /**
  68. Find out whether an extension is loaded.
  69. **/
  70. public static inline function extensionLoaded(name:String) {
  71. return Global.extension_loaded(name);
  72. }
  73. public static inline function isCli():Bool {
  74. return 0 == Global.strncasecmp(Const.PHP_SAPI, 'cli', 3);
  75. }
  76. /**
  77. Output file content from the given file name.
  78. **/
  79. public static inline function printFile(file:String) {
  80. return Global.fpassthru(Global.fopen(file, "r"));
  81. }
  82. public static inline function toPhpArray(a:Array<Dynamic>):NativeArray {
  83. return @:privateAccess a.arr;
  84. }
  85. public static inline function toHaxeArray(a:NativeArray):Array<Dynamic> {
  86. return @:privateAccess Array.wrap(a);
  87. }
  88. public static function hashOfAssociativeArray<T>(arr:NativeAssocArray<T>):Map<String, T> {
  89. var result = new StringMap();
  90. @:privateAccess result.data = arr;
  91. return result;
  92. }
  93. public static inline function associativeArrayOfHash(hash:haxe.ds.StringMap<Dynamic>):NativeArray {
  94. return @:privateAccess hash.data;
  95. }
  96. public static function objectOfAssociativeArray(arr:NativeArray):Dynamic {
  97. Syntax.foreach(arr, function(key:Scalar, value:Dynamic) {
  98. if (Global.is_array(value)) {
  99. arr[key] = objectOfAssociativeArray(value);
  100. }
  101. });
  102. return Boot.createAnon(arr);
  103. }
  104. public static inline function associativeArrayOfObject(ob:Dynamic):NativeArray {
  105. return Syntax.array(ob);
  106. }
  107. /**
  108. See the documentation for the equivalent PHP function for details on usage:
  109. <http://php.net/manual/en/function.mail.php>
  110. **/
  111. public static inline function mail(to:String, subject:String, message:String, ?additionalHeaders:String, ?additionalParameters:String):Bool {
  112. return Global.mail(to, subject, message, additionalHeaders, additionalParameters);
  113. }
  114. /**
  115. Rethrows an exception.
  116. If `e` is not a value caught in `try...catch` or if called outside of `catch` block, then `e` is thrown as
  117. a new exception.
  118. **/
  119. extern public static inline function rethrow(e:Dynamic) {
  120. if (Syntax.code("isset($__hx__caught_e, $__hx__real_e)") && e == Syntax.code("$__hx__real_e")) {
  121. Syntax.code("throw $__hx__caught_e");
  122. }
  123. throw e;
  124. }
  125. /**
  126. Tries to load all compiled php files and returns list of types.
  127. **/
  128. public static function getClasses():Dynamic {
  129. if (!loaded) {
  130. loaded = true;
  131. var reflection = new ReflectionClass(Boot.getPhpName('php.Boot'));
  132. loadLib(Global.dirname(reflection.getFileName(), 2));
  133. }
  134. var result:Dynamic = {};
  135. Syntax.foreach(Boot.getRegisteredAliases(), function(phpName:String, haxeName:String) {
  136. var parts = haxeName.split('.');
  137. var obj = result;
  138. while (parts.length > 1) {
  139. var pack = parts.shift();
  140. if (Syntax.field(obj, pack) == null) {
  141. Syntax.setField(obj, pack, {});
  142. }
  143. obj = Syntax.field(obj, pack);
  144. }
  145. Syntax.setField(obj, parts[0], Boot.getClass(phpName));
  146. });
  147. return result;
  148. }
  149. static var loaded:Bool = false;
  150. /**
  151. Loads types defined in the specified directory.
  152. **/
  153. public static function loadLib(pathToLib:String):Void {
  154. var absolutePath = Global.realpath(pathToLib);
  155. if (absolutePath == false)
  156. throw 'Failed to read path: $pathToLib';
  157. Syntax.foreach(Global.glob('$absolutePath/*.php'), function(_, fileName) {
  158. if (!Global.is_dir(fileName)) {
  159. Global.require_once(fileName);
  160. }
  161. });
  162. Syntax.foreach(Global.glob('$absolutePath/*', Const.GLOB_ONLYDIR), function(_, dirName) {
  163. loadLib(dirName);
  164. });
  165. }
  166. }