Sys.hx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package php;
  2. class Sys {
  3. public static function args() : Array<String> {
  4. return untyped __call__('array_key_exists', 'argv', __var__('_SERVER')) ? __call__('array_slice', __var__('_SERVER', 'argv'), 1) : [];
  5. }
  6. public static function getEnv( s : String ) : String {
  7. return untyped __call__("getenv", s);
  8. }
  9. public static function putEnv( s : String, v : String ) : Void {
  10. return untyped __call__("putenv", s + "=" + v);
  11. }
  12. public static function sleep( seconds : Float ) {
  13. return untyped __call__("usleep", seconds*1000000);
  14. }
  15. public static function setTimeLocale( loc : String ) : Bool {
  16. return untyped __call__("setlocale", __php__("LC_TIME"), loc) != false;
  17. }
  18. public static function getCwd() : String {
  19. var cwd : String = untyped __call__("getcwd");
  20. var l = cwd.substr(-1);
  21. return cwd + (l == '/' || l == '\\' ? '' : '/');
  22. }
  23. public static function setCwd( s : String ) {
  24. return untyped __call__("chdir", s);
  25. }
  26. public static function systemName() : String {
  27. var s : String = untyped __call__("php_uname", "s");
  28. var p : Int;
  29. if((p = s.indexOf(" ")) >= 0)
  30. return s.substr(0, p);
  31. else
  32. return s;
  33. }
  34. public static function escapeArgument( arg : String ) : String {
  35. var ok = true;
  36. for( i in 0...arg.length )
  37. switch( arg.charCodeAt(i) ) {
  38. case 32, 34: // [space] "
  39. ok = false;
  40. case 0, 13, 10: // [eof] [cr] [lf]
  41. arg = arg.substr(0,i);
  42. }
  43. if( ok )
  44. return arg;
  45. return '"'+arg.split('"').join('\\"')+'"';
  46. }
  47. public static function command( cmd : String, ?args : Array<String> ) : Int {
  48. if( args != null ) {
  49. cmd = escapeArgument(cmd);
  50. for( a in args )
  51. cmd += " "+escapeArgument(a);
  52. }
  53. var result = 0;
  54. var output = "";
  55. // untyped __call__("exec", cmd, output, result);
  56. untyped __call__("system", cmd, result);
  57. return result;
  58. }
  59. public static function exit( code : Int ) {
  60. return untyped __call__("exit", code);
  61. }
  62. public static function time() : Float {
  63. return untyped __call__("microtime", true);
  64. }
  65. public static function cpuTime() : Float {
  66. return untyped __call__("microtime", true) - __php__("$_SERVER['REQUEST_TIME']");
  67. }
  68. public static function executablePath() : String {
  69. return untyped __php__("$_SERVER['SCRIPT_FILENAME']");
  70. }
  71. public static function environment() : Hash<String> {
  72. return Lib.hashOfAssociativeArray(untyped __php__("$_SERVER"));
  73. }
  74. }