Lib.hx 6.1 KB

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