Lib.hx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. /**
  24. Platform-specific PHP Library. Provides some platform-specific functions
  25. for the PHP target, such as conversion from Haxe types to native types
  26. and vice-versa.
  27. **/
  28. class Lib {
  29. /**
  30. Print the specified value on the default output.
  31. **/
  32. public static function print( v : Dynamic ) : Void {
  33. untyped __call__("echo", Std.string(v));
  34. }
  35. /**
  36. Print the specified value on the default output followed by
  37. a newline character.
  38. **/
  39. public static function println( v : Dynamic ) : Void {
  40. print(v);
  41. print("\n");
  42. }
  43. /**
  44. Displays structured information about one or more expressions
  45. that includes its type and value. Arrays and objects are
  46. explored recursively with values indented to show structure.
  47. */
  48. public static function dump(v : Dynamic) : Void {
  49. untyped __call__("var_dump", v);
  50. }
  51. /**
  52. Serialize using native PHP serialization. This will return a binary
  53. `String` that can be stored for long term usage.
  54. **/
  55. public static function serialize( v : Dynamic ) : String {
  56. return untyped __call__("serialize", v);
  57. }
  58. /**
  59. Unserialize a `String` using native PHP serialization. See `php.Lib.serialize()`.
  60. **/
  61. public static function unserialize( s : String ) : Dynamic {
  62. return untyped __call__("unserialize", s);
  63. }
  64. /**
  65. Find out whether an extension is loaded.
  66. */
  67. public static function extensionLoaded(name : String) {
  68. return untyped __call__("extension_loaded", name);
  69. }
  70. public static function isCli() : Bool {
  71. return untyped __php__("(0 == strncasecmp(PHP_SAPI, 'cli', 3))");
  72. }
  73. /**
  74. Output file content from the given file name.
  75. */
  76. public static function printFile(file : String) {
  77. return untyped __call__("fpassthru", __call__("fopen", file, "r"));
  78. }
  79. public static function toPhpArray(a : Array<Dynamic>) : NativeArray {
  80. return untyped __field__(a, 'a');
  81. }
  82. public static inline function toHaxeArray(a : NativeArray) : Array<Dynamic> {
  83. return untyped __call__("new _hx_array", a);
  84. }
  85. public static function hashOfAssociativeArray<T>(arr : NativeArray) : Map<String,T> {
  86. var h = new haxe.ds.StringMap<T>();
  87. untyped h.h = arr;
  88. return h;
  89. }
  90. public static function associativeArrayOfHash(hash : haxe.ds.StringMap<Dynamic>) : NativeArray {
  91. return untyped hash.h;
  92. }
  93. public static function objectOfAssociativeArray(arr : NativeArray) : Dynamic {
  94. untyped __php__("foreach($arr as $key => $value){
  95. if(is_array($value)) $arr[$key] = php_Lib::objectOfAssociativeArray($value);
  96. }");
  97. return untyped __call__("_hx_anonymous", arr);
  98. }
  99. public static function associativeArrayOfObject(ob : Dynamic) : NativeArray {
  100. return untyped __php__("(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 function mail(to : String, subject : String, message : String, ?additionalHeaders : String, ?additionalParameters : String) : Bool
  112. {
  113. if(null != additionalParameters)
  114. return untyped __call__("mail", to, subject, message, additionalHeaders, additionalParameters);
  115. else if(null != additionalHeaders)
  116. return untyped __call__("mail", to, subject, message, additionalHeaders);
  117. else
  118. return untyped __call__("mail", to, subject, message);
  119. }
  120. /**
  121. For neko compatibility only.
  122. **/
  123. public static function rethrow( e : Dynamic ) {
  124. if(Std.is(e, Exception)) {
  125. var __rtex__ = e;
  126. untyped __php__("throw $__rtex__");
  127. }
  128. else throw e;
  129. }
  130. static function appendType(o : Dynamic, path : Array<String>, t : Dynamic) {
  131. var name = path.shift();
  132. if(path.length == 0)
  133. untyped __php__("$o->$name = $t");
  134. else {
  135. var so = untyped __call__("isset", __php__("$o->$name")) ? __php__("$o->$name") : {};
  136. appendType(so, path, t);
  137. untyped __php__("$o->$name = $so");
  138. }
  139. }
  140. public static function getClasses() {
  141. var path : String = null;
  142. var o = {};
  143. untyped __call__('reset', php.Boot.qtypes);
  144. while((path = untyped __call__('key', php.Boot.qtypes)) != null) {
  145. appendType(o, path.split('.'), untyped php.Boot.qtypes[path]);
  146. untyped __call__('next',php.Boot.qtypes);
  147. }
  148. return o;
  149. }
  150. /**
  151. * Loads types defined in the specified directory.
  152. */
  153. public static function loadLib(pathToLib : String) : Void
  154. {
  155. var prefix = untyped __prefix__();
  156. untyped __php__("$_hx_types_array = array();
  157. $_hx_cache_content = '';
  158. //Calling this function will put all types present in the specified types in the $_hx_types_array
  159. _hx_build_paths($pathToLib, $_hx_types_array, array(), $prefix);
  160. foreach($_hx_types_array as $val) {
  161. //For every type that has been found, create its description
  162. $t = null;
  163. if($val['type'] == 0) {
  164. $t = new _hx_class($val['phpname'], $val['qname'], $val['path']);
  165. } else if($val['type'] == 1) {
  166. $t = new _hx_enum($val['phpname'], $val['qname'], $val['path']);
  167. } else if($val['type'] == 2) {
  168. $t = new _hx_interface($val['phpname'], $val['qname'], $val['path']);
  169. } else if($val['type'] == 3) {
  170. $t = new _hx_class($val['name'], $val['qname'], $val['path']);
  171. }
  172. //Register the type
  173. if(!array_key_exists($t->__qname__, php_Boot::$qtypes)) {
  174. _hx_register_type($t);
  175. }
  176. }
  177. ");
  178. }
  179. }