Lib.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.Global;
  25. import php.Throwable;
  26. import php.Syntax;
  27. import php.Const;
  28. /**
  29. Platform-specific PHP Library. Provides some platform-specific functions
  30. for the PHP target, such as conversion from Haxe types to native types
  31. and vice-versa.
  32. **/
  33. class Lib {
  34. /**
  35. Print the specified value on the default output.
  36. **/
  37. public static inline function print( v : Dynamic ) : Void {
  38. Global.echo(Std.string(v));
  39. }
  40. /**
  41. Print the specified value on the default output followed by
  42. a newline character.
  43. **/
  44. public static function println( v : Dynamic ) : Void {
  45. print(v);
  46. print("\n");
  47. }
  48. /**
  49. Displays structured information about one or more expressions
  50. that includes its type and value. Arrays and objects are
  51. explored recursively with values indented to show structure.
  52. */
  53. public static inline function dump(v : Dynamic) : Void {
  54. Global.var_dump(v);
  55. }
  56. /**
  57. Serialize using native PHP serialization. This will return a binary
  58. `String` that can be stored for long term usage.
  59. **/
  60. public static inline function serialize( v : Dynamic ) : String {
  61. return Global.serialize(v);
  62. }
  63. /**
  64. Unserialize a `String` using native PHP serialization. See `php.Lib.serialize()`.
  65. **/
  66. public static inline function unserialize( s : String ) : Dynamic {
  67. return Global.unserialize(s);
  68. }
  69. /**
  70. Find out whether an extension is loaded.
  71. */
  72. public static inline function extensionLoaded(name : String) {
  73. return Global.extension_loaded(name);
  74. }
  75. public static inline function isCli() : Bool {
  76. return 0 == Global.strncasecmp(Const.PHP_SAPI, 'cli', 3);
  77. }
  78. /**
  79. Output file content from the given file name.
  80. */
  81. public static inline function printFile(file : String) {
  82. return Global.fpassthru(Global.fopen(file, "r"));
  83. }
  84. public static inline function toPhpArray(a : Array<Dynamic>) : NativeArray {
  85. return @:privateAccess a.arr;
  86. }
  87. public static inline function toHaxeArray(a : NativeArray) : Array<Dynamic> {
  88. return @:privateAccess Array.wrap(a);
  89. }
  90. public static function hashOfAssociativeArray<T>(arr : NativeAssocArray<T>) : Map<String,T> {
  91. var result = new StringMap();
  92. @:privateAccess result.data = arr;
  93. return result;
  94. }
  95. public static inline function associativeArrayOfHash(hash : haxe.ds.StringMap<Dynamic>) : NativeArray {
  96. return @:privateAccess hash.data;
  97. }
  98. public static inline function objectOfAssociativeArray(arr : NativeArray) : Dynamic {
  99. return Boot.createAnon(arr);
  100. }
  101. public static inline function associativeArrayOfObject(ob : Dynamic) : NativeArray {
  102. return Syntax.array(ob);
  103. }
  104. /**
  105. * See the documentation for the equivalent PHP function for details on usage:
  106. * <http://php.net/manual/en/function.mail.php>
  107. * @param to
  108. * @param subject
  109. * @param message
  110. * @param ?additionalHeaders
  111. * @param ?additionalParameters
  112. */
  113. public static inline function mail(to : String, subject : String, message : String, ?additionalHeaders : String, ?additionalParameters : String) : Bool {
  114. return Global.mail(to, subject, message, additionalHeaders, additionalParameters);
  115. }
  116. /**
  117. For neko compatibility only.
  118. **/
  119. public static inline function rethrow( e : Dynamic ) {
  120. throw e;
  121. }
  122. public static function getClasses() {
  123. throw "Not implemented";
  124. }
  125. /**
  126. Loads types defined in the specified directory.
  127. **/
  128. public static function loadLib(pathToLib : String) : Void {
  129. var absolutePath = Global.realpath(pathToLib);
  130. if(absolutePath == false) throw 'Failed to read path: $pathToLib';
  131. Syntax.foreach(Global.glob('$absolutePath/*.php'), function(_, fileName) {
  132. if(!Global.is_dir(fileName)) {
  133. Global.require_once(fileName);
  134. }
  135. });
  136. Syntax.foreach(Global.glob('$absolutePath/*', Const.GLOB_ONLYDIR), function(_, dirName) {
  137. loadLib(dirName);
  138. });
  139. }
  140. }