Skip to content

Commit

Permalink
Merge pull request #6 from TorstenDittmann/main
Browse files Browse the repository at this point in the history
feat(system): get enum of arch
  • Loading branch information
eldadfux authored Jan 24, 2021
2 parents 1bff135 + 88dcb87 commit 63a4f2e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/System/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class System
public const PPC = 'ppc';
public const ARM = 'arm';

private const RegExX86 = '/(x86*|i386|i686)/';
private const RegExPPC = '/(aarch*|arm*)/';
private const RegExARM = '/(ppc*)/';

/**
* Returns the system's OS.
* @return string
Expand All @@ -29,6 +33,33 @@ static public function getArch(): string
return php_uname('m');
}

/**
* Returns the architecture's Enum of the system's processor.
*
* @return string
*
* @throws Exeption
*/
static public function getArchEnum(): string
{
$arch = self::getArch();
switch (1) {
case preg_match(self::RegExX86, $arch):
return System::X86;
break;
case preg_match(self::RegExPPC, $arch):
return System::PPC;
break;
case preg_match(self::RegExARM, $arch):
return System::ARM;
break;

default:
throw new Exception("'{$arch}' enum not found.");
break;
}
}

/**
* Returns the system's hostname.
*
Expand All @@ -46,7 +77,7 @@ static public function getHostname(): string
*/
static public function isArm(): bool
{
return !!preg_match('/(aarch*|arm*)/', self::getArch());
return !!preg_match(self::RegExARM, self::getArch());
}

/**
Expand All @@ -56,7 +87,7 @@ static public function isArm(): bool
*/
static public function isX86(): bool
{
return !!preg_match('/(x86*|i386|i686)/', self::getArch());
return !!preg_match(self::RegExX86, self::getArch());
}

/**
Expand All @@ -66,7 +97,7 @@ static public function isX86(): bool
*/
static public function isPPC(): bool
{
return !!preg_match('/(ppc*)/', self::getArch());
return !!preg_match(self::RegExPPC, self::getArch());
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/System/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public function testOs()
{
$this->assertIsString(System::getOS());
$this->assertIsString(System::getArch());
$this->assertIsString(System::getArchEnum());
$this->assertIsString(System::getHostname());
$this->assertIsBool(System::isArm());
$this->assertIsBool(System::isPPC());
$this->assertIsBool(System::isX86());
$this->assertIsBool(System::isArch(System::ARM));
$this->assertIsBool(System::isArch(System::X86));
$this->assertIsBool(System::isArch(System::PPC));
$this->expectException("Exception");
System::isArch("throw");
}
}
4 changes: 4 additions & 0 deletions tests/System/SystemTestARM.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Utopia PHP Framework
*
Expand Down Expand Up @@ -31,8 +32,11 @@ public function testOs()
$this->assertTrue(System::isArm());
$this->assertFalse(System::isPPC());
$this->assertFalse(System::isX86());

$this->assertTrue(System::isArch(System::ARM));
$this->assertFalse(System::isArch(System::PPC));
$this->assertFalse(System::isArch(System::X86));

$this->assertEquals(System::ARM, System::getArchEnum());
}
}
4 changes: 4 additions & 0 deletions tests/System/SystemTestPPC.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Utopia PHP Framework
*
Expand Down Expand Up @@ -31,8 +32,11 @@ public function testOs()
$this->assertFalse(System::isArm());
$this->assertTrue(System::isPPC());
$this->assertFalse(System::isX86());

$this->assertFalse(System::isArch(System::ARM));
$this->assertTrue(System::isArch(System::PPC));
$this->assertFalse(System::isArch(System::X86));

$this->assertEquals(System::PPC, System::getArchEnum());
}
}
4 changes: 4 additions & 0 deletions tests/System/SystemTestX86.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Utopia PHP Framework
*
Expand Down Expand Up @@ -31,8 +32,11 @@ public function testOs()
$this->assertFalse(System::isArm());
$this->assertFalse(System::isPPC());
$this->assertTrue(System::isX86());

$this->assertFalse(System::isArch(System::ARM));
$this->assertFalse(System::isArch(System::PPC));
$this->assertTrue(System::isArch(System::X86));

$this->assertEquals(System::X86, System::getArchEnum());
}
}

0 comments on commit 63a4f2e

Please sign in to comment.