-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.php
More file actions
74 lines (67 loc) · 1.65 KB
/
utils.php
File metadata and controls
74 lines (67 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Core;
use \ReflectionClass;
/**
* Check if module implements Core\Entity
* Load the module if not loaded. (autoloader)
* @param string $name
* @return bool
*/
function isEntity(string $name): bool
{
try {
$class = new ReflectionClass($name);
$parent = $class->getParentClass();
return $parent && $parent->getName() == 'Core\\Entity';
} catch (\Exception $e) {
return false;
}
}
/**
* Calls custom init functions, if applicable
* @param string $name
* @return void
*/
function initClass(string $name): void
{
if (defined("DEBUG_AUTOLOAD_LOG")){
echo "[+] $name <br />\n";
}
if (isEntity($name)){
$name::init();
}
}
/**
* Scan for avaliable module names
* Do not check if they are valid.
* @return array
*/
function getModuleNames(): array
{
$names = [];
foreach (scandir("Modules") as $name){
if (!strpos($name, '.php')){
continue;
}
$names[] = 'Modules\\' . str_replace('.php', '', $name);
}
return $names;
}
/**
* Returns formatted memory usage string
* @param $real pass this to PHP's memory_get_usage
* @return string
*/
function getMemoryUsage(bool $real = false): string
{
$units = ['B', 'KiB', 'MiB', 'GiB'];
$memory_usage_raw = memory_get_usage($real);
$unit = floor(log($memory_usage_raw, 1024));
$memory_usage = round($memory_usage_raw / pow(1024, $unit));
return $memory_usage . ' ' . $units[$unit];
}