1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
|
7
|
|
|
class Json |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The file path. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $path; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The laravel filesystem instance. |
18
|
|
|
* |
19
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
20
|
|
|
*/ |
21
|
|
|
protected $filesystem; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The attributes collection. |
25
|
|
|
* |
26
|
|
|
* @var \Illuminate\Support\Collection |
27
|
|
|
*/ |
28
|
|
|
protected $attributes; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The constructor. |
32
|
|
|
* |
33
|
|
|
* @param mixed $path |
34
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
35
|
|
|
*/ |
36
|
39 |
|
public function __construct($path, Filesystem $filesystem = null) |
37
|
|
|
{ |
38
|
39 |
|
$this->path = (string) $path; |
39
|
39 |
|
$this->filesystem = $filesystem ?: new Filesystem(); |
40
|
39 |
|
$this->attributes = Collection::make($this->getAttributes()); |
41
|
39 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get filesystem. |
45
|
|
|
* |
46
|
|
|
* @return Filesystem |
47
|
|
|
*/ |
48
|
|
|
public function getFilesystem() |
49
|
|
|
{ |
50
|
|
|
return $this->filesystem; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set filesystem. |
55
|
|
|
* |
56
|
|
|
* @param Filesystem $filesystem |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setFilesystem(Filesystem $filesystem) |
61
|
|
|
{ |
62
|
|
|
$this->filesystem = $filesystem; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get path. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
39 |
|
public function getPath() |
73
|
|
|
{ |
74
|
39 |
|
return $this->path; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set path. |
79
|
|
|
* |
80
|
|
|
* @param mixed $path |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
1 |
|
public function setPath($path) |
85
|
|
|
{ |
86
|
1 |
|
$this->path = (string) $path; |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Make new instance. |
93
|
|
|
* |
94
|
|
|
* @param string $path |
95
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
96
|
|
|
* |
97
|
|
|
* @return static |
98
|
|
|
*/ |
99
|
25 |
|
public static function make($path, Filesystem $filesystem = null) |
100
|
|
|
{ |
101
|
25 |
|
return new static($path, $filesystem); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get file content. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
39 |
|
public function getContents() |
110
|
|
|
{ |
111
|
39 |
|
return $this->filesystem->get($this->getPath()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get file contents as array. |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
39 |
|
public function getAttributes() |
120
|
|
|
{ |
121
|
39 |
|
if (config('modules.cache.enabled') === false) { |
122
|
39 |
|
return json_decode($this->getContents(), 1); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return app('cache')->remember($this->getPath(), config('modules.cache.lifetime'), function () { |
126
|
|
|
return json_decode($this->getContents(), 1); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Convert the given array data to pretty json. |
132
|
|
|
* |
133
|
|
|
* @param array $data |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
1 |
|
public function toJsonPretty(array $data = null) |
138
|
|
|
{ |
139
|
1 |
|
return json_encode($data ?: $this->attributes, JSON_PRETTY_PRINT); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Update json contents from array data. |
144
|
|
|
* |
145
|
|
|
* @param array $data |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function update(array $data) |
150
|
|
|
{ |
151
|
|
|
$this->attributes = new Collection(array_merge($this->attributes->toArray(), $data)); |
152
|
|
|
|
153
|
|
|
return $this->save(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set a specific key & value. |
158
|
|
|
* |
159
|
|
|
* @param string $key |
160
|
|
|
* @param mixed $value |
161
|
|
|
* |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
1 |
|
public function set($key, $value) |
165
|
|
|
{ |
166
|
1 |
|
$this->attributes->offsetSet($key, $value); |
167
|
|
|
|
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Save the current attributes array to the file storage. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function save() |
177
|
|
|
{ |
178
|
|
|
return $this->filesystem->put($this->getPath(), $this->toJsonPretty()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Handle magic method __get. |
183
|
|
|
* |
184
|
|
|
* @param string $key |
185
|
|
|
* |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
1 |
|
public function __get($key) |
189
|
|
|
{ |
190
|
1 |
|
return $this->get($key); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the specified attribute from json file. |
195
|
|
|
* |
196
|
|
|
* @param $key |
197
|
|
|
* @param null $default |
198
|
|
|
* |
199
|
|
|
* @return mixed |
200
|
|
|
*/ |
201
|
34 |
|
public function get($key, $default = null) |
202
|
|
|
{ |
203
|
34 |
|
return $this->attributes->get($key, $default); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Handle call to __call method. |
208
|
|
|
* |
209
|
|
|
* @param string $method |
210
|
|
|
* @param array $arguments |
211
|
|
|
* |
212
|
|
|
* @return mixed |
213
|
|
|
*/ |
214
|
|
|
public function __call($method, $arguments = []) |
215
|
|
|
{ |
216
|
|
|
if (method_exists($this, $method)) { |
217
|
|
|
return call_user_func_array([$this, $method], $arguments); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return call_user_func_array([$this->attributes, $method], $arguments); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Handle call to __toString method. |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
1 |
|
public function __toString() |
229
|
|
|
{ |
230
|
1 |
|
return $this->getContents(); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|