feat: Функция чтения json из файла
This commit is contained in:
parent
29048a7203
commit
14b10eea22
2 changed files with 25 additions and 3 deletions
|
|
@ -479,8 +479,10 @@ class Component
|
||||||
* @param array $shim
|
* @param array $shim
|
||||||
*/
|
*/
|
||||||
function addRequireJsPath($name, $path, $shim = null): void {
|
function addRequireJsPath($name, $path, $shim = null): void {
|
||||||
|
if ($this->site) {
|
||||||
$this->site->addRequireJsPath($name, $path, $shim);
|
$this->site->addRequireJsPath($name, $path, $shim);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ComponentRequest $request
|
* @param ComponentRequest $request
|
||||||
|
|
|
||||||
24
src/File.php
24
src/File.php
|
|
@ -6,14 +6,34 @@ use Exception;
|
||||||
class File {
|
class File {
|
||||||
/**
|
/**
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @return string
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
static function getContents($filename) {
|
static function getContents(string $filename): string {
|
||||||
$buffer = @file_get_contents($filename);
|
$buffer = @file_get_contents($filename);
|
||||||
if ($buffer !== false) {
|
if ($buffer !== false) {
|
||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
throw new Exception("Unable to read file: " . $filename);
|
throw new Exception("Unable to read file: " . $filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $filename
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
static function getJson(string $filename, bool|null $associative = null): mixed {
|
||||||
|
if (!file_exists($filename)) {
|
||||||
|
return throw new \Exception("File not exist: " . $filename);
|
||||||
|
}
|
||||||
|
$json = @file_get_contents($filename);
|
||||||
|
if ($json === false) {
|
||||||
|
throw new \Exception("Unable to read file: " . $filename);
|
||||||
|
}
|
||||||
|
$file = json_decode($json, $associative);
|
||||||
|
|
||||||
|
if (is_null($file)) {
|
||||||
|
throw new \Exception("Unable decoding json file: " . $filename);
|
||||||
|
}
|
||||||
|
return $file;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue