Lib.hx 6.4 KB

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