system_detector.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace phpath;
  3. class system_detector {
  4. /**
  5. * That class could be used to detect, which standard of operating system
  6. * code is running on. POSIX like operating systems, that mean Linux, BSD
  7. * MacOS, Solaris, and other *nix use "/" as directory separator and uses
  8. * root directory, when Windows and DOS use "\" and drives letters.
  9. */
  10. public static function is_posix(): bool {
  11. /**
  12. * This function check that system uses POSIX like standards.
  13. * When system is POSIX like, that mean Linux, Unix, MacOS and much
  14. * more return true. When run on not POSIX system return false.
  15. *
  16. * @return bool True when system is POSIX like, false when not.
  17. */
  18. static $is_posix = null;
  19. if ($is_posix !== null) {
  20. return $is_posix;
  21. }
  22. $is_posix = false;
  23. $is_posix = $is_posix or self::in_os_name('nix');
  24. $is_posix = $is_posix or self::in_os_name('linux');
  25. $is_posix = $is_posix or self::in_os_name('mac');
  26. $is_posix = $is_posix or self::in_os_name('bsd');
  27. $is_posix = $is_posix or self::in_os_name('solaris');
  28. return $is_posix;
  29. }
  30. public static function is_windows(): bool {
  31. /**
  32. * It check that code run on windows.
  33. * When code run on windows like os, which use "\" as directory
  34. * separator, return true. When not run on dos or windows return
  35. * false.
  36. *
  37. * @return bool True when run on windows, or false when on POSIX.
  38. */
  39. static $is_windows = null;
  40. if ($is_windows !== null) {
  41. return $is_windows;
  42. }
  43. $is_windows = false;
  44. $is_windows = $is_windows or self::in_os_name('nt');
  45. $is_windows = $is_windows or self::in_os_name('win');
  46. $is_windows = $is_windows or self::in_os_name('dos');
  47. return $is_windows;
  48. }
  49. public static function get_os_name(): string {
  50. /**
  51. * It return simple name of family of system on which code run.
  52. *
  53. * @return string Name of system family.
  54. */
  55. return strtolower(php_uname('s'));
  56. }
  57. private static function in_os_name(string $part): bool {
  58. /**
  59. * This function check that given part of name is in the operating
  60. * system name or given part is equal to system name.
  61. *
  62. * @return bool True when it is equal or false when not.
  63. */
  64. static $uname = self::get_os_name();
  65. if ($uname === $part) {
  66. return true;
  67. }
  68. if (strpos($uname, $part) !== false) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. public static function get_separator(): string {
  74. /**
  75. * That return directory separator.
  76. *
  77. * @return string Directory separator.
  78. */
  79. return DIRECTORY_SEPARATOR;
  80. }
  81. }