Skip to content

Commit

Permalink
fix: uniformisation
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Dec 24, 2024
1 parent 6d96de5 commit 9a4cb3a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

A modern PHP library to get system information with Enums and Value Objects.

The goal is to have reliable and consistent system information across different operating systems.

This library use local commands to get the system information and parse the output to provide a consistent API.

It also provides a command line utility: `sysinfo`.

Compatible with MacOS, Linux, and Windows.
Expand Down
3 changes: 2 additions & 1 deletion cli/sysinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@

echo '> OS : '.$os->name().PHP_EOL;
echo '> Version : '.$os->version().PHP_EOL;
echo '> Release : '.$os->release().PHP_EOL;
echo '> Kernel : '.$os->kernel().PHP_EOL;
echo '> Build : '.$os->buildVersion().PHP_EOL;
4 changes: 3 additions & 1 deletion src/Contracts/OperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public function name(): string;

public function version(): string;

public function release(): string;
public function kernel(): string;

public function buildVersion(): string;
}
7 changes: 6 additions & 1 deletion src/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public function version(): string
return php_uname('v');
}

public function release(): string
public function kernel(): string
{
return php_uname('r');
}

public function buildVersion(): string
{
return php_uname('r');
}
Expand Down
12 changes: 7 additions & 5 deletions src/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ final class Linux implements OperatingSystem

private ?string $cached_version = null;

private ?string $cached_release = null;

public function __construct()
{
$this->retrieveFromEtcOsRelease();
Expand All @@ -34,7 +32,6 @@ private function retrieveFromEtcOsRelease(): void
/** @var string[] $ini */
$this->cached_name = $ini['NAME'] ?? '';
$this->cached_version = preg_replace('/^((\d.?)*)\s.*/m', '$1', $ini['VERSION'] ?? '');
$this->cached_release = $ini['VERSION_ID'] ?? '';
}

public function name(): string
Expand All @@ -47,8 +44,13 @@ public function version(): string
return $this->cached_version ?? php_uname('v');
}

public function release(): string
public function kernel(): string
{
return php_uname('r');
}

public function buildVersion(): string
{
return $this->cached_release ?? php_uname('r');
return php_uname('r');
}
}
2 changes: 1 addition & 1 deletion src/OperatingSystems/MacOS.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function version(): string
return $this->cached_version ?? Shell::execOrFail('sw_vers --productVersion');
}

public function release(): string
public function kernel(): string
{
return php_uname('r');
}
Expand Down
85 changes: 77 additions & 8 deletions src/OperatingSystems/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace KnotsPHP\System\OperatingSystems;

use KnotsPHP\System\Contracts\OperatingSystem;
use KnotsPHP\System\Exceptions\InvalidArgumentException;
use KnotsPHP\System\Helpers\Shell;

final class Windows implements OperatingSystem
Expand All @@ -11,10 +12,10 @@ final class Windows implements OperatingSystem

private ?string $cached_version = null;

private ?string $cached_release = null;

private ?string $cached_build_version = null;

private ?string $cached_kernel = null;

private ?string $cached_edition = null;

public function __construct()
Expand All @@ -25,6 +26,9 @@ public function __construct()
private function retrieveFromSwVers(): void
{
$result = Shell::exec('wmic os get Caption, Version, BuildNumber /format:list');
// BuildNumber=22631
// Caption=Microsoft Windows 11 Famille
// Version=10.0.22631

$lines = explode(PHP_EOL, $result);

Expand All @@ -35,12 +39,43 @@ private function retrieveFromSwVers(): void
$infos[trim($parts[0])] = trim($parts[1] ?? '');
}

$this->cached_edition = preg_replace('/.+\s\d+\s(.*)/m', '$1', $infos['Caption'] ?? '');
if (str_contains($infos['Caption'], 'Server')) {
// Server 2022
$this->cached_edition = implode(' ', array_slice(explode(' ', $infos['Caption']), 2));
$this->cached_version = $this->getWindowsVersion($infos['Version'] ?? '');
} else {
// Pro / Family / Home
$this->cached_edition = preg_replace('/.+\s\d+\s(.*)/m', '$1', $infos['Caption'] ?? '');
$this->cached_version = preg_replace('/.+\s(\d+)\s.*/m', '$1', $infos['Version'] ?? '');
}

$this->cached_name = preg_replace('/(.+\s)\d+\s.*/m', '$1', $infos['Caption'] ?? '');
$this->cached_version = preg_replace('/.+\s(\d+)\s.*/m', '$1', $infos['Caption'] ?? '');
$this->cached_release = $infos['Version'] ?? null;
$this->cached_build_version = $infos['BuildNumber'] ?? null;

$this->cached_build_version = $this->getDisplayVersion();

$this->cached_kernel = $this->getFullVersionNumber() ?: ($infos['Version'] ?? null);
}

private function getFullVersionNumber(): ?string
{
return preg_replace('/^.*\[version\s((\d+.?)*)\]/m', '$1', Shell::exec('ver'));
}

/**
* Get the 23H2 version format
*/
private function getDisplayVersion(): ?string
{
// Execute the registry query
// or (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").DisplayVersion
$output = Shell::exec('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v DisplayVersion 2>&1');

// Use regular expression to extract the version
if (preg_match('/DisplayVersion\s+REG_SZ\s+(\S+)/', $output, $matches)) {
return $matches[1];
}

return null;
}

public function name(): string
Expand All @@ -53,9 +88,38 @@ public function version(): string
return $this->cached_version ?? '';
}

public function release(): string
public function getWindowsVersion(string $versionString): ?string
{
return $this->cached_release ?? '';
// 10.0.22631 / 10.0.20348
$versionParts = explode('.', $versionString);

// Check if the version string is valid
if (count($versionParts) < 2) {
throw new InvalidArgumentException('Invalid Windows version string: '.$versionString);
}

// Extract major and minor version numbers
$majorVersion = (int) $versionParts[0];
$minorVersion = (int) $versionParts[1];

// Determine the Windows version based on the major version
if ($majorVersion === 6) {
if ($minorVersion === 1) {
return '7';
} elseif ($minorVersion === 2) {
return '8';
} elseif ($minorVersion === 3) {
return '8.1';
}
} elseif ($majorVersion === 10) {
if ($minorVersion >= 0 && $minorVersion < 22000) {
return '10';
} elseif ($minorVersion >= 22000) {
return '11';
}
}

return null;
}

public function buildVersion(): string
Expand All @@ -72,4 +136,9 @@ public function isServer(): bool
{
return str_contains($this->name(), 'Server');
}

public function kernel(): string
{
return $this->cached_kernel ?? '';
}
}

0 comments on commit 9a4cb3a

Please sign in to comment.