Skip to content

Commit 14e61c1

Browse files
author
Andrey Helldar
authored
Added loading json files
1 parent 188eb9b commit 14e61c1

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/Services/File.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Helldar\PrettyArray\Exceptions\FileDoesntExistsException;
66
use Helldar\Support\Facades\File as FileSupport;
7+
use Helldar\Support\Facades\Str;
78
use Helldar\Support\Tools\Stub;
89

910
class File
@@ -21,19 +22,26 @@ public static function make(string $content = null)
2122
}
2223

2324
/**
24-
* @param string $filename
25+
* @param string $filename
2526
*
2627
* @throws \Helldar\PrettyArray\Exceptions\FileDoesntExistsException
2728
*
28-
* @return array
29+
* @return mixed
2930
*/
30-
public function load(string $filename): array
31+
public function load(string $filename)
3132
{
3233
if (! file_exists($filename)) {
3334
throw new FileDoesntExistsException($filename);
3435
}
3536

36-
return require $filename;
37+
return $this->isJson($filename)
38+
? $this->loadJson($filename)
39+
: require $filename;
40+
}
41+
42+
public function loadJson(string $filename)
43+
{
44+
return json_decode(file_get_contents($filename), true);
3745
}
3846

3947
public function store(string $path)
@@ -44,4 +52,11 @@ public function store(string $path)
4452

4553
FileSupport::store($path, $content);
4654
}
55+
56+
public function isJson(string $filename): bool
57+
{
58+
$extension = pathinfo($filename, PATHINFO_EXTENSION);
59+
60+
return Str::lower($extension) === 'json';
61+
}
4762
}

0 commit comments

Comments
 (0)