diff --git a/composer.json b/composer.json index 7379d12..194c5d1 100644 --- a/composer.json +++ b/composer.json @@ -32,10 +32,5 @@ "phpunit/phpunit": "9.6.*", "laravel/pint": "1.13.*", "phpstan/phpstan": "1.10.*" - }, - "config": { - "platform": { - "php": "8.0" - } } } diff --git a/composer.lock b/composer.lock index c8a8c55..214bae0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "345bd4d4441c382f36951e3260c466da", + "content-hash": "5c33c217017df015ec976058ed455a1c", "packages": [], "packages-dev": [ { @@ -145,16 +145,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.11.1", + "version": "1.12.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c" + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", "shasum": "" }, "require": { @@ -162,11 +162,12 @@ }, "conflict": { "doctrine/collections": "<1.6.8", - "doctrine/common": "<2.13.3 || >=3,<3.2.2" + "doctrine/common": "<2.13.3 || >=3 <3.2.2" }, "require-dev": { "doctrine/collections": "^1.6.8", "doctrine/common": "^2.13.3 || ^3.2.2", + "phpspec/prophecy": "^1.10", "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" }, "type": "library", @@ -192,7 +193,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.11.1" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" }, "funding": [ { @@ -200,20 +201,20 @@ "type": "tidelift" } ], - "time": "2023-03-08T13:26:56+00:00" + "time": "2024-06-12T14:39:25+00:00" }, { "name": "nikic/php-parser", - "version": "v5.0.2", + "version": "v5.3.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13" + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/139676794dc1e9231bf7bcd123cfc0c99182cb13", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/8eea230464783aa9671db8eea6f8c6ac5285794b", + "reference": "8eea230464783aa9671db8eea6f8c6ac5285794b", "shasum": "" }, "require": { @@ -224,7 +225,7 @@ }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^9.0" }, "bin": [ "bin/php-parse" @@ -256,9 +257,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.2" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.3.1" }, - "time": "2024-03-05T20:51:40+00:00" + "time": "2024-10-08T18:51:32+00:00" }, { "name": "phar-io/manifest", @@ -380,16 +381,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.66", + "version": "1.10.67", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "94779c987e4ebd620025d9e5fdd23323903950bd" + "reference": "16ddbe776f10da6a95ebd25de7c1dbed397dc493" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/94779c987e4ebd620025d9e5fdd23323903950bd", - "reference": "94779c987e4ebd620025d9e5fdd23323903950bd", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/16ddbe776f10da6a95ebd25de7c1dbed397dc493", + "reference": "16ddbe776f10da6a95ebd25de7c1dbed397dc493", "shasum": "" }, "require": { @@ -432,45 +433,41 @@ { "url": "https://github.com/phpstan", "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", - "type": "tidelift" } ], - "time": "2024-03-28T16:17:31+00:00" + "time": "2024-04-16T07:22:02+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.31", + "version": "9.2.32", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965" + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/48c34b5d8d983006bd2adc2d0de92963b9155965", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5", + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.18 || ^5.0", + "nikic/php-parser": "^4.19.1 || ^5.1.0", "php": ">=7.3", - "phpunit/php-file-iterator": "^3.0.3", - "phpunit/php-text-template": "^2.0.2", - "sebastian/code-unit-reverse-lookup": "^2.0.2", - "sebastian/complexity": "^2.0", - "sebastian/environment": "^5.1.2", - "sebastian/lines-of-code": "^1.0.3", - "sebastian/version": "^3.0.1", - "theseer/tokenizer": "^1.2.0" + "phpunit/php-file-iterator": "^3.0.6", + "phpunit/php-text-template": "^2.0.4", + "sebastian/code-unit-reverse-lookup": "^2.0.3", + "sebastian/complexity": "^2.0.3", + "sebastian/environment": "^5.1.5", + "sebastian/lines-of-code": "^1.0.4", + "sebastian/version": "^3.0.2", + "theseer/tokenizer": "^1.2.3" }, "require-dev": { - "phpunit/phpunit": "^9.3" + "phpunit/phpunit": "^9.6" }, "suggest": { "ext-pcov": "PHP extension that provides line coverage", @@ -479,7 +476,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.2-dev" + "dev-main": "9.2.x-dev" } }, "autoload": { @@ -508,7 +505,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.31" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.32" }, "funding": [ { @@ -516,7 +513,7 @@ "type": "github" } ], - "time": "2024-03-02T06:37:42+00:00" + "time": "2024-08-22T04:23:01+00:00" }, { "name": "phpunit/php-file-iterator", @@ -761,45 +758,45 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.18", + "version": "9.6.21", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04" + "reference": "de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04", - "reference": "32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa", + "reference": "de6abf3b6f8dd955fac3caad3af7a9504e8c2ffa", "shasum": "" }, "require": { - "doctrine/instantiator": "^1.3.1 || ^2", + "doctrine/instantiator": "^1.5.0 || ^2", "ext-dom": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-xml": "*", "ext-xmlwriter": "*", - "myclabs/deep-copy": "^1.10.1", - "phar-io/manifest": "^2.0.3", - "phar-io/version": "^3.0.2", + "myclabs/deep-copy": "^1.12.0", + "phar-io/manifest": "^2.0.4", + "phar-io/version": "^3.2.1", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.28", - "phpunit/php-file-iterator": "^3.0.5", + "phpunit/php-code-coverage": "^9.2.32", + "phpunit/php-file-iterator": "^3.0.6", "phpunit/php-invoker": "^3.1.1", - "phpunit/php-text-template": "^2.0.3", - "phpunit/php-timer": "^5.0.2", - "sebastian/cli-parser": "^1.0.1", - "sebastian/code-unit": "^1.0.6", + "phpunit/php-text-template": "^2.0.4", + "phpunit/php-timer": "^5.0.3", + "sebastian/cli-parser": "^1.0.2", + "sebastian/code-unit": "^1.0.8", "sebastian/comparator": "^4.0.8", - "sebastian/diff": "^4.0.3", - "sebastian/environment": "^5.1.3", - "sebastian/exporter": "^4.0.5", - "sebastian/global-state": "^5.0.1", - "sebastian/object-enumerator": "^4.0.3", - "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^3.2", + "sebastian/diff": "^4.0.6", + "sebastian/environment": "^5.1.5", + "sebastian/exporter": "^4.0.6", + "sebastian/global-state": "^5.0.7", + "sebastian/object-enumerator": "^4.0.4", + "sebastian/resource-operations": "^3.0.4", + "sebastian/type": "^3.2.1", "sebastian/version": "^3.0.2" }, "suggest": { @@ -844,7 +841,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.18" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.21" }, "funding": [ { @@ -860,7 +857,7 @@ "type": "tidelift" } ], - "time": "2024-03-21T12:07:32+00:00" + "time": "2024-09-19T10:50:18+00:00" }, { "name": "sebastian/cli-parser", @@ -1878,15 +1875,12 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, "platform": { "php": ">=8.0.0" }, - "platform-dev": [], - "platform-overrides": { - "php": "8.0" - }, + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/src/System/System.php b/src/System/System.php index c40034c..fb5e5cc 100644 --- a/src/System/System.php +++ b/src/System/System.php @@ -262,6 +262,21 @@ public static function getCPUUsage(int $duration = 1): float } } + private static function getProcMemoryInfo(string $field): int + { + $memInfo = file_get_contents('/proc/meminfo'); + if (!$memInfo) { + throw new Exception('Unable to read /proc/meminfo'); + } + + preg_match(sprintf('/%s:\s+(\d+)/', $field), $memInfo, $matches); + if (isset($matches[1])) { + return intval(intval($matches[1]) / 1024); + } else { + throw new Exception("Unable to find {$field} in /proc/meminfo."); + } + } + /** * Returns the total amount of RAM available on the system as Megabytes. * @@ -273,19 +288,7 @@ public static function getMemoryTotal(): int { switch (self::getOS()) { case 'Linux': - $memInfo = file_get_contents('/proc/meminfo'); - - if (!$memInfo) { - throw new Exception('Unable to read /proc/meminfo'); - } - preg_match('/MemTotal:\s+(\d+)/', $memInfo, $matches); - - if (isset($matches[1])) { - return intval(intval($matches[1]) / 1024); - } else { - throw new Exception('Unable to find memtotal in /proc/meminfo.'); - } - // no break + return self::getProcMemoryInfo("MemTotal"); case 'Darwin': return intval((intval(shell_exec('sysctl -n hw.memsize'))) / 1024 / 1024); default: @@ -304,19 +307,7 @@ public static function getMemoryFree(): int { switch (self::getOS()) { case 'Linux': - $meminfo = file_get_contents('/proc/meminfo'); - - if (!$meminfo) { - throw new Exception('Unable to read /proc/meminfo'); - } - - preg_match('/MemFree:\s+(\d+)/', $meminfo, $matches); - if (isset($matches[1])) { - return intval(intval($matches[1]) / 1024); - } else { - throw new Exception('Could not find MemFree in /proc/meminfo.'); - } - // no break + return self::getProcMemoryInfo("MemFree"); case 'Darwin': return intval(intval(shell_exec('sysctl -n vm.page_free_count')) / 1024 / 1024); default: @@ -324,6 +315,25 @@ public static function getMemoryFree(): int } } + /** + * Returns the total amount of Available RAM on the system as Megabytes. + * + * @return int + * + * @throws Exception + */ + public static function getMemoryAvailable(): int + { + switch (self::getOS()) { + case 'Linux': + return self::getProcMemoryInfo("MemAvailable"); + case 'Darwin': + throw new Exception(self::getOS().' not supported.'); + default: + throw new Exception(self::getOS().' not supported.'); + } + } + /** * Returns the total amount of Disk space on the system as Megabytes. * @@ -331,9 +341,9 @@ public static function getMemoryFree(): int * * @throws Exception */ - public static function getDiskTotal(): int + public static function getDiskTotal(string $directory = __DIR__): int { - $totalSpace = disk_total_space(__DIR__); + $totalSpace = disk_total_space($directory); if ($totalSpace === false) { throw new Exception('Unable to get disk space'); @@ -349,9 +359,9 @@ public static function getDiskTotal(): int * * @throws Exception */ - public static function getDiskFree(): int + public static function getDiskFree(string $directory = __DIR__): int { - $totalSpace = disk_free_space(__DIR__); + $totalSpace = disk_free_space($directory); if ($totalSpace === false) { throw new Exception('Unable to get free disk space'); diff --git a/tests/System/SystemTest.php b/tests/System/SystemTest.php index 0ede284..b254546 100644 --- a/tests/System/SystemTest.php +++ b/tests/System/SystemTest.php @@ -93,6 +93,16 @@ public function testGetMemoryFree(): void } } + public function testGetMemoryAvailable(): void + { + if (System::getOS() === 'Linux') { + $this->assertIsInt(System::getMemoryAvailable()); + } else { + $this->expectException('Exception'); + System::getMemoryFree(); + } + } + public function testGetIOUsage(): void { if (System::getOS() === 'Linux') {