diff --git a/src/Utils/Json.php b/src/Utils/Json.php index 9c626b8..9967dde 100644 --- a/src/Utils/Json.php +++ b/src/Utils/Json.php @@ -2,7 +2,6 @@ namespace Kiwilan\Steward\Utils; -use Illuminate\Support\Facades\File; use Kiwilan\HttpPool\Utils\PrintConsole; class Json @@ -17,28 +16,20 @@ class Json public function __construct( readonly protected mixed $data, ) { - if (is_file($this->data)) { - $this->path = $this->data; - $this->contents = file_get_contents($this->data); - } elseif (is_string($this->data)) { - $this->contents = json_decode($this->data, true); - } else { + if (is_array($data)) { $this->contents = $this->data; + } elseif (is_string($data)) { + if (file_exists($data)) { + $this->path = $data; + $this->contents = file_get_contents($data); + } else { + $this->contents = json_decode($data, true); + } + } else { + $this->contents = $data; } } - public function load(string $path): mixed - { - $this->path = $path; - $this->contents = file_get_contents($path); - - if (! $this->contents) { - $this->contents = []; - } - - return $this->contents; - } - /** * @return string Pretty json string */ @@ -55,11 +46,14 @@ public function save(string $saveTo, bool $console = true): void { $pretty = $this->pretty(); - if (! is_dir($saveTo)) { - mkdir($saveTo, recursive: true); + if (file_exists($saveTo)) { + unlink($saveTo); + } + + if (! is_dir(dirname($saveTo))) { + mkdir(dirname($saveTo), recursive: true); } - unlink($saveTo); file_put_contents($saveTo, $pretty); if ($console) {