Lib.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C)2005-2017 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 inline function objectOfAssociativeArray(arr : NativeArray) : Dynamic {
  97. return Boot.createAnon(arr);
  98. }
  99. public static inline function associativeArrayOfObject(ob : Dynamic) : NativeArray {
  100. return Syntax.array(ob);
  101. }
  102. /**
  103. * See the documentation for the equivalent PHP function for details on usage:
  104. * <http://php.net/manual/en/function.mail.php>
  105. * @param to
  106. * @param subject
  107. * @param message
  108. * @param ?additionalHeaders
  109. * @param ?additionalParameters
  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. For neko compatibility only.
  116. **/
  117. public static inline function rethrow( e : Dynamic ) {
  118. throw e;
  119. }
  120. /**
  121. Tries to load all compiled php files and returns list of tpes.
  122. **/
  123. public static function getClasses():Dynamic {
  124. if(!loaded) {
  125. loaded = true;
  126. var reflection = new ReflectionClass(Boot.getPhpName('php.Boot'));
  127. loadLib(Global.dirname(reflection.getFileName(), 2));
  128. }
  129. var result:Dynamic = {};
  130. Syntax.foreach(Boot.getRegisteredAliases(), function(phpName:String, haxeName:String) {
  131. var parts = haxeName.split('.');
  132. var obj = result;
  133. while(parts.length > 1) {
  134. var pack = parts.shift();
  135. if(Syntax.getField(obj, pack) == null) {
  136. Syntax.setField(obj, pack, {});
  137. }
  138. obj = Syntax.getField(obj, pack);
  139. }
  140. Syntax.setField(obj, parts[0], Boot.getClass(phpName));
  141. });
  142. return result;
  143. }
  144. static var loaded:Bool = false;
  145. /**
  146. Loads types defined in the specified directory.
  147. **/
  148. public static function loadLib(pathToLib : String) : Void {
  149. var absolutePath = Global.realpath(pathToLib);
  150. if(absolutePath == false) throw 'Failed to read path: $pathToLib';
  151. Syntax.foreach(Global.glob('$absolutePath/*.php'), function(_, fileName) {
  152. if(!Global.is_dir(fileName)) {
  153. Global.require_once(fileName);
  154. }
  155. });
  156. Syntax.foreach(Global.glob('$absolutePath/*', Const.GLOB_ONLYDIR), function(_, dirName) {
  157. loadLib(dirName);
  158. });
  159. }
  160. }