This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -103,7 +103,7 @@ class F
],
];
public static $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
public static $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
/**
* Appends new content to an existing file
@@ -357,19 +357,24 @@ class F
}
/**
* Loads a file and returns the result
* Loads a file and returns the result or `false` if the
* file to load does not exist
*
* @param string $file
* @param mixed $fallback
* @param array $data Optional array of variables to extract in the variable scope
* @return mixed
*/
public static function load(string $file, $fallback = null)
public static function load(string $file, $fallback = null, array $data = [])
{
if (file_exists($file) === false) {
if (is_file($file) === false) {
return $fallback;
}
$result = include $file;
// we use the loadIsolated() method here to prevent the included
// file from overwriting our $fallback in this variable scope; see
// https://www.php.net/manual/en/function.include.php#example-124
$result = static::loadIsolated($file, $data);
if ($fallback !== null && gettype($result) !== gettype($fallback)) {
return $fallback;
@@ -378,6 +383,39 @@ class F
return $result;
}
/**
* Loads a file with as little as possible in the variable scope
*
* @param string $file
* @param array $data Optional array of variables to extract in the variable scope
* @return mixed
*/
protected static function loadIsolated(string $file, array $data = [])
{
// extract the $data variables in this scope to be accessed by the included file;
// protect $file against being overwritten by a $data variable
$___file___ = $file;
extract($data);
return include $___file___;
}
/**
* Loads a file using `include_once()` and returns whether loading was successful
*
* @param string $file
* @return bool
*/
public static function loadOnce(string $file): bool
{
if (is_file($file) === false) {
return false;
}
include_once $file;
return true;
}
/**
* Returns the mime type of a file
*
@@ -498,7 +536,7 @@ class F
// avoid errors for invalid sizes
if ($size <= 0) {
return '0 kB';
return '0 KB';
}
// the math magic