Skip to content

Commit

Permalink
added function kilobytes_to_human_readable
Browse files Browse the repository at this point in the history
added Conf functions
  • Loading branch information
[email protected] authored and [email protected] committed Dec 26, 2023
1 parent d3dabac commit a37ab4c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/PhPease/Conf/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

/*
* This file is part of the PhPease package.
*
* (c) Timo Reith <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PhPease\Conf;

/**
* get memory limit in kilobyte
* @return false|float|int|string
*/
function get_memory_limit() {
$memory_limit = ini_get('memory_limit');
if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
if ($matches[2] == 'M') {
$memory_limit = $matches[1] * 1024; // nnnM -> nnn MB
} else if ($matches[2] == 'K') {
$memory_limit = $matches[1]; // nnnK -> nnn KB
} else if ($matches[2] == 'G') {
$memory_limit = $matches[1] * 1024 * 1024; // nnnG -> nnn GB
}
}
return $memory_limit;
}
12 changes: 11 additions & 1 deletion src/PhPease/Convert/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ function bytes_to_human_readable($bytes, int $decimals = 2): string
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
}
}

/**
* @param $kilobytes
* @param int $decimals
* @return string
*/
function kilobytes_to_human_readable($kilobytes, int $decimals = 2): string
{
return bytes_to_human_readable($kilobytes*1024, $decimals);
}
1 change: 1 addition & 0 deletions src/PhPease/functions_include.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

// for manual functions include
require_once __DIR__ . '/Conf/functions.php';
require_once __DIR__ . '/Convert/functions.php';
require_once __DIR__ . '/Request/functions.php';
require_once __DIR__ . '/String/functions.php';
Expand Down
17 changes: 17 additions & 0 deletions tests/ConfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use function PhPease\Conf\get_memory_limit;
use function PhPease\Convert\kilobytes_to_human_readable;

final class ConfTest extends TestCase
{
public function test_get_memory_limit()
{
ini_set('memory_limit', '128M');

$memLimit = get_memory_limit();

$this->assertEquals(131072, $memLimit);
$this->assertEquals('128.00 MB', kilobytes_to_human_readable($memLimit));
}
}

0 comments on commit a37ab4c

Please sign in to comment.