OperatingSystem.php (2873B)
1 <?php 2 3 namespace PartKeepr\CoreBundle\System; 4 5 class OperatingSystem 6 { 7 /** 8 * Returns the platform name the system is running on. 9 * 10 * Typical return values are: "Linux", "FreeBSD", "Darwin" (Mac OSX), 11 * "Windows". 12 */ 13 public function getPlatform() 14 { 15 if (function_exists('posix_uname')) { 16 $data = posix_uname(); 17 18 if (array_key_exists('sysname', $data)) { 19 return $data['sysname']; 20 } 21 } 22 23 if (\PHP_OS == 'WINNT') { 24 return 'Windows'; 25 } 26 27 return 'unknown'; 28 } 29 30 /** 31 * Returns the distribution. 32 * 33 * @return string string 34 */ 35 public function getRelease() 36 { 37 switch (strtolower($this->getPlatform())) { 38 case 'freebsd': 39 /* 40 * Unfortunately, there's no text file on FreeBSD which tells us the release 41 * number. Thus, we hope that "release" within posix_uname() is defined. 42 */ 43 if (function_exists('posix_uname')) { 44 $data = posix_uname(); 45 46 if (array_key_exists('release', $data)) { 47 return $data['release']; 48 } 49 } 50 break; 51 case 'darwin': 52 /* 53 * Mac stores its version number in a public readable plist file, which 54 * is in XML format. 55 */ 56 $document = new \DomDocument(); 57 $document->load('/System/Library/CoreServices/SystemVersion.plist'); 58 $xpath = new \DOMXPath($document); 59 $entries = $xpath->query('/plist/dict/*'); 60 61 $previous = ''; 62 foreach ($entries as $entry) { 63 if (strpos($previous, 'ProductVersion') !== false) { 64 return $entry->textContent; 65 } 66 $previous = $entry->textContent; 67 } 68 break; 69 case 'linux': 70 return $this->getLinuxDistribution(); 71 break; 72 default: 73 break; 74 } 75 76 return 'unknown'; 77 } 78 79 /** 80 * Tries to detect the distribution. 81 * 82 * Currently, we only execute lsb_release to find out the version number. 83 * As I don't have any other distributions at hand to test with, I rely 84 * on user feedback which distributions don't have lsb_release. 85 * 86 * @return string 87 */ 88 public function getLinuxDistribution() 89 { 90 /* Try executing lsb_release */ 91 $release = @exec('lsb_release -d -s', $void, $retval); 92 93 if ($retval === 0 && $release !== '') { 94 return $release; 95 } 96 97 //@todo we need better handling here 98 return 'unknown'; 99 } 100 }