1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Rawilk\LaravelModules\Exceptions\InvalidJson; |
7
|
|
|
|
8
|
|
|
class Json |
9
|
|
|
{ |
10
|
|
|
/** @var \Rawilk\LaravelModules\Collection */ |
11
|
|
|
protected $attributes; |
12
|
|
|
|
13
|
|
|
/** @var \Illuminate\Filesystem\Filesystem */ |
14
|
|
|
protected $filesystem; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $path; |
18
|
|
|
|
19
|
|
|
public function __construct($path, Filesystem $filesystem = null) |
20
|
|
|
{ |
21
|
|
|
$this->path = (string) $path; |
22
|
|
|
$this->filesystem = $filesystem ?: new Filesystem; |
23
|
|
|
$this->attributes = Collection::make($this->getAttributes()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function make($path, Filesystem $filesystem = null): self |
27
|
|
|
{ |
28
|
|
|
return new static($path, $filesystem); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function get(string $key, $default = null) |
32
|
|
|
{ |
33
|
|
|
return $this->attributes->get($key, $default); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getAttributes(): array |
37
|
|
|
{ |
38
|
|
|
$attributes = json_decode($this->getContents(), true); |
39
|
|
|
|
40
|
|
|
if (json_last_error() > 0) { |
41
|
|
|
throw InvalidJson::invalidString($this->getPath()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (! config('modules.cache.enabled')) { |
45
|
|
|
return $attributes; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return app('cache')->remember($this->getPath(), config('modules.cache.lifetime'), function () use ($attributes) { |
49
|
|
|
return $attributes; |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getContents(): string |
54
|
|
|
{ |
55
|
|
|
return $this->filesystem->get($this->getPath()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getPath(): string |
59
|
|
|
{ |
60
|
|
|
return $this->path; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getFilesystem(): Filesystem |
64
|
|
|
{ |
65
|
|
|
return $this->filesystem; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function save(): bool |
69
|
|
|
{ |
70
|
|
|
return $this->filesystem->put($this->getPath(), $this->toJsonPretty()); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function set(string $key, $value): self |
74
|
|
|
{ |
75
|
|
|
$this->attributes->offsetSet($key, $value); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setPath($path): self |
81
|
|
|
{ |
82
|
|
|
$this->path = (string) $path; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function setFilesystem(Filesystem $filesystem): self |
88
|
|
|
{ |
89
|
|
|
$this->filesystem = $filesystem; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function toJsonPretty(array $data = null): string |
95
|
|
|
{ |
96
|
|
|
return json_encode($data ?: $this->attributes, JSON_PRETTY_PRINT); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function update(array $data): bool |
100
|
|
|
{ |
101
|
|
|
$this->attributes = new Collection(array_merge($this->attributes->toArray(), $data)); |
102
|
|
|
|
103
|
|
|
return $this->save(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function __call($method, $arguments = []) |
107
|
|
|
{ |
108
|
|
|
if (method_exists($this, $method)) { |
109
|
|
|
return call_user_func_array([$this, $method], $arguments); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return call_user_func_array([$this->attributes, $method], $arguments); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function __get($key) |
116
|
|
|
{ |
117
|
|
|
return $this->get($key); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function __toString() |
121
|
|
|
{ |
122
|
|
|
return $this->getContents(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|