Skip to content

Commit

Permalink
Implement Platform::getSystemAlias()
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbrandao committed Nov 8, 2014
1 parent 2009706 commit ed0045b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
platform.php
============

Access to underlying platform's identifying data (Python port).

[![Total Downloads](https://poser.pugx.org/noiselabs/platform/downloads.png)](https://packagist.org/packages/noiselabs/platform)
[![Latest Stable Version](https://poser.pugx.org/noiselabs/platform/v/stable.png)](https://packagist.org/packages/noiselabs/platform)
[![Latest Unstable Version](https://poser.pugx.org/noiselabs/platform/v/unstable.png)](https://packagist.org/packages/noiselabs/platform)
[![Build Status](https://secure.travis-ci.org/noiselabs/platform.php.png)](http://travis-ci.org/noiselabs/platform.php)
[![License](https://poser.pugx.org/noiselabs/platform/license.png)](https://packagist.org/packages/noiselabs/platform)

Access to underlying platform's identifying data (Python port).
100 changes: 96 additions & 4 deletions src/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public function getLinuxDistribution($distname = '', $version = '', $id = '', $s
}

// Lookup /etc for *-release or *-version files
$file = null;
foreach (scandir(self::UNIXCONFDIR) as $file) {
preg_match('/(\w+)(-|_)(release|version)$/', $file, $matches);
if (isset($matches[1]) && in_array($matches[1], $supportedDists)) {
Expand All @@ -231,13 +232,90 @@ public function getLinuxDistribution($distname = '', $version = '', $id = '', $s
}
}

if (!$distname) {
return $this->distTryDarder($distname, $version, $id);
if (!$file || !$distname) {
return $this->_distTryHarder($distname, $version, $id);
}

// Read the first line
$handle = fopen(self::UNIXCONFDIR . '/'. $file, 'r');
$line = fgets($handle);
fclose($handle);

$fromReleaseFile = $this->_parseReleaseFile($line);

if (isset($fromReleaseFile['distname']) && $fromReleaseFile['distname'] && $fullDistributionName) {
$distname = $fromReleaseFile['distname'];
}
if (isset($fromReleaseFile['version']) && $fromReleaseFile['version']) {
$version = $fromReleaseFile['version'];
}
if (isset($fromReleaseFile) && $fromReleaseFile['id']) {
$id = $fromReleaseFile['id'];
}

return array('distname' => $distname, 'version' => $version, 'id' => $id);
}

/**
* System name aliasing.
*
* Returns (system,release,version) aliased to common marketing names used for some systems.
*
* It also does some reordering of the information in some cases where it would otherwise cause confusion.
*
* @param string $system
* @param string $release
* @param string $version
*
* @return array()
*/
public function getSystemAlias($system, $release, $version)
{
if ($system == 'Rhapsody') {
// Apple's BSD derivative
// XXX How can we determine the marketing release number?
return array('system' => 'MacOS X Server', 'release' => $system . $release, 'version' => $version);

} elseif ($system == 'SunOS') {
// Sun's OS
if ($release < '5') {
// These releases use the old name SunOS
return array('system' => $system, 'release' => $release, 'version' => $version);
}

// Modify release (marketing release = SunOS release - 3)
$l = explode('.', $release);
if ($l) {
$major = (int)$l[0] - 3;
$l[0] = (string)$major;
$release = implode('.', $l);
if ($release < '6') {
$system = 'Solaris';
} else {
// XXX Whatever the new SunOS marketing name is...
$system = 'Solaris';
}
}

} elseif ($system == 'IRIX64') {
// IRIX reports IRIX64 on platforms with 64-bit support; yet it is really a version and not a different
// platform, since 32-bit apps are also supported.
$system = 'IRIX';
if ($version) {
$version = $version . ' (64bit)';
} else {
$version = '64bit';
}
}

elseif (in_array($system, array('win32', 'win16'))) {
// In case one of the other tricks
$system = 'Windows';
}

return array('system' => $system, 'release' => $release, 'version' => $version);
}

/**
* Allows Python-style method naming. Platform::version() gets converted into Platform::getVersion().
*
Expand Down Expand Up @@ -325,8 +403,8 @@ protected function _distTryHarder($distname, $version, $id)
if ($verfiles) {
sort($verfiles);
$distname = 'slackware';
$last = end($varfiles);
$version = isset($last[14]) ? $last[14] : $version;
$last = end($varfiles);
$version = isset($last[14]) ? $last[14] : $version;

}

Expand All @@ -335,4 +413,18 @@ protected function _distTryHarder($distname, $version, $id)

return array('distname' => $distname, 'version' => $version, 'id' => $id);
}

/**
* Default to empty 'version' and 'id' strings. Both defaults are used when 'line' is empty. 'id' defaults to
* empty when an id can not be deduced.
*
* @param string $line
*/
protected function _parseReleaseFile($line)
{
$version = '';
$id = '';

// TODO
}
}
10 changes: 10 additions & 0 deletions tests/PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ public function testGetUname()
$this->assertArrayHasKey($key, $uname);
}
}

public function testGetSystemAlias()
{
$platform = new Platform();

$alias = $platform->getSystemAlias('Rhapsody', 'Mac OS X Server 1.0', '');
foreach (array('system', 'release', 'version') as $key) {
$this->assertArrayHasKey($key, $alias);
}
}
}

0 comments on commit ed0045b

Please sign in to comment.