Removed most of the Environment class and renamed it to just Index as a library meta class.

This commit is contained in:
flash 2024-07-31 19:47:05 +00:00
parent 13d2edb7c3
commit 1fa778b338
6 changed files with 59 additions and 396 deletions

View file

@ -1 +1 @@
0.2407.311920
0.2407.311946

View file

@ -1,340 +0,0 @@
<?php
// Environment.php
// Created: 2021-04-30
// Updated: 2022-02-27
namespace Index;
use RuntimeException;
/**
* Provides information about the current runtime environment.
*/
final class Environment {
/**
* Contains the end-of-line sequence for the current platform.
*
* @var string
*/
public const NEWLINE = PHP_EOL;
/**
* Contains the amount of bytes in an integer for this system.
*
* @var int
*/
public const INT_SIZE = PHP_INT_SIZE;
private static ?Version $indexVersion = null;
private static ?Version $phpVersion = null;
private static array $phpVersions = [];
/**
* Checks whether the current PHP environment is running in debug mode.
*
* Essentially checks if any sort of error reporting is enabled. It shouldn't be in prod.
*
* @return bool true if the environment is in debug mode, false is not.
*/
public static function isDebug(): bool {
return error_reporting() !== 0;
}
/**
* Toggles debug mode.
*
* Essentially turns all error reporting on or off.
*
* @param bool $debug true if debug mode should be enabled, false if it should be disabled.
*/
public static function setDebug(bool $debug = false): void {
if($debug) {
ini_set('display_errors', 'on');
error_reporting(-1);
} else {
ini_set('display_errors', 'off');
error_reporting(0);
}
}
/**
* Reads an environment variable.
*
* @param string $name Name of the environment variable.
* @param bool $localOnly true if only variables local to the application should be read,
* false if greater level operating system variables should be included.
* @return ?string null if the variable doesn't exist, otherwise a string of the value.
*/
public static function getVariable(string $name, bool $localOnly = true): ?string {
return ($value = getenv($name, $localOnly)) === false ? null : $value;
}
/**
* Writes an application-local environment variable.
*
* @param string $name Name of the environment variable.
* @param mixed $value Value that should be assigned to the environment variable. Will be cast to a string.
*/
public static function setVariable(string $name, mixed $value): void {
putenv($name . '=' . ((string)$value));
}
/**
* Removes an application-local environment variable.
*
* @param string $name Name of the environment variable.
*/
public static function removeVariable(string $name): void {
putenv($name);
}
/**
* Gets the current version of the Index library.
*
* Parses the VERSION in the root of the Index directory.
*
* @return Version A Version instance representing the version of the Index Library.
*/
public static function getIndexVersion(): Version {
if(self::$indexVersion === null)
self::$indexVersion = Version::parse(trim(file_get_contents(NDX_ROOT . DIRECTORY_SEPARATOR . 'VERSION')));
return self::$indexVersion;
}
/**
* Gets the version of the PHP installation Index is running on.
*
* @return Version A Version instance representing the version of PHP.
*/
public static function getPHPVersion(): Version {
if(self::$phpVersion === null)
self::$phpVersion = new Version(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
return self::$phpVersion;
}
/**
* Gets the version of a PHP extension.
*
* @param string $extension Name of the extension.
* @return ?Version null if the extension is not installed, otherwise an instance of Version representing the version of the extension.
*/
public static function getPHPExtensionVersion(string $extension): ?Version {
if(!isset(self::$phpVersions[$extension])) {
$rawVersion = phpversion($extension);
self::$phpVersions[$extension] = empty($rawVersion) ? Version::empty() : Version::parse($rawVersion);
}
return self::$phpVersions[$extension];
}
/**
* Gets the name of the Operating System PHP is running on.
*
* @return string Name of the Operating System.
*/
public static function getOSName(): string {
static $value = null;
$value ??= php_uname('s');
return $value;
}
/**
* Checks if we're running on Windows.
*
* @return bool true if we're running on Windows, otherwise false.
*/
public static function isWindows(): bool {
return self::isWindowsNT()
|| self::getOSName() === 'Windows';
}
/**
* Checks if we're running on Windows NT.
*
* @return bool true if we're running on Windows NT, otherwise false.
*/
public static function isWindowsNT(): bool {
return ($os = self::getOSName()) === 'Windows NT'
|| $os === 'Windows_NT'
|| $os === 'WindowsNT';
}
/**
* Checks if we're running on a Unix-like Operating System.
*
* @return bool true if we're running on a Unix-like Operating System, otherwise false.
*/
public static function isUnixLike(): bool {
return self::isLinux()
|| self::isBSD()
|| self::isMacOS()
|| self::isSolaris();
}
/**
* Checks if we're running on a BSD derivative.
*
* @return bool true if we're running on a BSD derivative, otherwise false.
*/
public static function isBSD(): bool {
return self::isOpenBSD()
|| self::isFreeBSD()
|| self::isNetBSD()
|| self::isDragonFlyBSD()
|| self::getOSName() === 'BSD';
}
/**
* Checks if we're running on OpenBSD.
*
* @return bool true if we're running on OpenBSD, otherwise false.
*/
public static function isOpenBSD(): bool {
return self::getOSName() === 'OpenBSD';
}
/**
* Checks if we're running on FreeBSD.
*
* @return bool true if we're running on FreeBSD, otherwise false.
*/
public static function isFreeBSD(): bool {
return self::getOSName() === 'FreeBSD';
}
/**
* Checks if we're running on NetBSD.
*
* @return bool true if we're running on NetBSD, otherwise false.
*/
public static function isNetBSD(): bool {
return self::getOSName() === 'NetBSD';
}
/**
* Checks if we're running on DragonFly BSD.
*
* @return bool true if we're running on DragonFly BSD, otherwise false.
*/
public static function isDragonFlyBSD(): bool {
return self::getOSName() === 'DragonFly';
}
/**
* Checks if we're running on macOS.
*
* @return bool true if we're running on macOS, otherwise false.
*/
public static function isMacOS(): bool {
return self::getOSName() === 'Darwin';
}
/**
* Checks if we're running on Solaris.
*
* @return bool true if we're running on Solaris, otherwise false.
*/
public static function isSolaris(): bool {
return self::getOSName() === 'SunOS';
}
/**
* Checks if we're running on Linux.
*
* @return bool true if we're running on Linux, otherwise false.
*/
public static function isLinux(): bool {
return self::getOSName() === 'Linux';
}
/**
* Returns the name of the SAPI PHP is currently running through.
*
* @return string Name of the SAPI.
*/
public static function getSAPIName(): string {
static $sapi = null;
$sapi ??= php_sapi_name();
return $sapi;
}
/**
* Checks if we're running through Apache2Handler.
*
* @return bool true if we're running through Apache2Handler, otherwise false.
*/
public static function isApache2(): bool {
return ($sapi = self::getSAPIName()) === 'apache' || $sapi === 'apache2handler';
}
/**
* Checks if we're running through CGI.
*
* @return bool true if we're running through CGI, otherwise false.
*/
public static function isCGI(): bool {
return self::getSAPIName() === 'cgi';
}
/**
* Checks if we're running through FastCGI (and/or php-fpm).
*
* @return bool true if we're running through FastCGI, otherwise false.
*/
public static function isFastCGI(): bool {
return ($sapi = self::getSAPIName()) === 'cgi-fcgi' || $sapi === 'fpm-fcgi';
}
/**
* Checks if we're running through console.
*
* @return bool true if we're running through console, otherwise false.
*/
public static function isConsole(): bool {
return self::getSAPIName() === 'cli';
}
/**
* Checks if we're running through the built-in development server.
*
* @return bool true if we're running through the built-in development server, otherwise false.
*/
public static function isDebugServer(): bool {
return self::getSAPIName() === 'cli-server';
}
/**
* Checks if we're running through LiteSpeed.
*
* @return bool true if we're running through LiteSpeed, otherwise false.
*/
public static function isLiteSpeed(): bool {
return self::getSAPIName() === 'litespeed';
}
/**
* Checks if we're running through PHPDBG.
*
* @return bool true if we're running through PHPDBG, otherwise false.
*/
public static function isPHPDebugger(): bool {
return self::getSAPIName() === 'phpdbg';
}
/**
* Checks if we're running on a 32-bit system.
*
* @return bool true if we're on 32-bit, false if not.
*/
public static function is32Bit(): bool {
return PHP_INT_SIZE === 4;
}
/**
* Checks if we're running on a 64-bit system.
*
* @return bool true if we're on 64-bit, false if not.
*/
public static function is64Bit(): bool {
return PHP_INT_SIZE === 8;
}
}

View file

@ -1,12 +1,11 @@
<?php
// Stream.php
// Created: 2021-04-30
// Updated: 2022-02-27
// Updated: 2024-07-31
namespace Index\IO;
use Stringable;
use Index\Environment;
use Index\ICloseable;
abstract class Stream implements Stringable, ICloseable {
@ -50,7 +49,7 @@ abstract class Stream implements Stringable, ICloseable {
public function writeLine(string $line): void {
$this->write($line);
$this->write(Environment::NEWLINE);
$this->write(PHP_EOL);
}
abstract public function writeChar(string $char): void;

34
src/Index.php Normal file
View file

@ -0,0 +1,34 @@
<?php
// Index.php
// Created: 2021-04-30
// Updated: 2024-07-31
namespace Index;
/**
* Provides information about the Index library.
*/
final class Index {
public const PATH_SOURCE = __DIR__;
public const PATH_ROOT = self::PATH_SOURCE . DIRECTORY_SEPARATOR . '..';
public const PATH_VERSION = self::PATH_ROOT . DIRECTORY_SEPARATOR . 'VERSION';
/**
* Gets the current version of the Index library.
*
* Reads the VERSION file in the root of the Index directory.
* Returns 0.0.0 if reading the file failed for any reason.
*
* @return string Current version string.
*/
public static function version(): string {
if(!is_file(self::PATH_VERSION))
return '0.0.0';
$version = file_get_contents(self::PATH_VERSION);
if($version === false)
return '0.0.0';
return trim($version);
}
}

View file

@ -1,52 +0,0 @@
<?php
// EnvironmentTest.php
// Created: 2021-05-02
// Updated: 2024-07-31
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Index\Environment;
use Index\Version;
#[CoversClass(Environment::class)]
#[UsesClass(Version::class)]
final class EnvironmentTest extends TestCase {
public function testEnvVars(): void {
$varName = 'NDX_TEST_VAR_DO_NOT_DEFINE';
$this->assertEmpty(Environment::getVariable($varName));
Environment::setVariable($varName, 'the');
$varValue = Environment::getVariable($varName);
$this->assertEquals('the', $varValue);
Environment::removeVariable($varName);
$this->assertEmpty(Environment::getVariable($varName));
}
public function testIndexVersion(): void {
$strVersion = trim(file_get_contents(__DIR__ . '/../VERSION'));
$rawVersion = explode('.', $strVersion);
$version = Environment::getIndexVersion();
$this->assertEquals((int)$rawVersion[0], $version->getMajor());
$this->assertEquals((int)$rawVersion[1], $version->getMinor());
$this->assertEquals((int)$rawVersion[2], $version->getPatch());
$this->assertEquals($strVersion, (string)$version);
}
public function testPHPAndExtVersion(): void {
$version = Environment::getPHPVersion();
$extVersion = Environment::getPHPExtensionVersion('Core');
$this->assertEquals(PHP_MAJOR_VERSION, $version->getMajor());
$this->assertEquals(PHP_MINOR_VERSION, $version->getMinor());
$this->assertEquals(PHP_RELEASE_VERSION, $version->getPatch());
$this->assertTrue($version->equals($extVersion));
}
// not really sure how you would test the platform and sapi functions, so i won't
}

22
tests/IndexTest.php Normal file
View file

@ -0,0 +1,22 @@
<?php
// IndexTest.php
// Created: 2021-05-02
// Updated: 2024-07-31
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Index\Index;
#[CoversClass(Index::class)]
final class IndexTest extends TestCase {
public function testIndexVersion(): void {
$expectedVersion = file_get_contents(__DIR__ . '/../VERSION');
$this->assertIsString($expectedVersion);
$expectedVersion = trim($expectedVersion);
$this->assertEquals($expectedVersion, Index::version());
}
}