1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TwoDojo\ModuleManager\Registries; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use TwoDojo\ModuleManager\Support\ModuleDescriptor; |
9
|
|
|
|
10
|
|
|
class FileRegistry extends BaseRegistry |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem |
14
|
|
|
*/ |
15
|
|
|
protected $storage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $directory = 'module_manager/registry'; |
21
|
|
|
|
22
|
|
|
protected $scopes = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* FileRegistry constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
$this->storage = Storage::disk(config('module_manager.file_registry_storage')); |
30
|
|
|
if (!$this->storage->exists($this->directory)) { |
31
|
|
|
$this->storage->makeDirectory($this->directory); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function save(array $attributes) |
39
|
|
|
{ |
40
|
|
|
$uniqueName = $attributes['uniqueName']; |
41
|
|
|
$lastId = $this->getFiles()->sortByDesc('id')->pluck('id')->first(); |
42
|
|
|
$lastId = $lastId ? $lastId : 1; |
43
|
|
|
|
44
|
|
|
$saved = $this->storage->put($this->getFullPath($uniqueName), json_encode(array_merge([ |
45
|
|
|
'id' => $lastId, |
46
|
|
|
'created_at' => Carbon::now()->timestamp, |
47
|
|
|
'updated_at' => Carbon::now()->timestamp, |
48
|
|
|
], $attributes))); |
49
|
|
|
|
50
|
|
|
return $saved ? $this->getFile($uniqueName) : (object)$attributes; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
|
|
public function update($id, array $attributes) |
57
|
|
|
{ |
58
|
|
|
$record = $this->find($id); |
59
|
|
|
if (is_null($record)) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$saved = $this->storage->put($this->getFullPath($record->uniqueName), json_encode(array_merge($record->getAttributes(), [ |
64
|
|
|
'updated_at' => Carbon::now()->timestamp |
65
|
|
|
], $attributes))); |
66
|
|
|
|
67
|
|
|
return $saved; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function find($id) |
74
|
|
|
{ |
75
|
|
|
return $this->applyScopes($this->getFiles()->where('id', $id))->first(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function findByUniqueName(string $uniqueName) |
82
|
|
|
{ |
83
|
|
|
$data = $this->applyScopes($this->getFile($uniqueName))->first(); |
84
|
|
|
$collection = collect([]); |
85
|
|
|
|
86
|
|
|
return $data !== null ? $collection->put($data->id, $data) : null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function findByField(string $field, $value) |
93
|
|
|
{ |
94
|
|
|
return $this->applyScopes($this->getFiles()->where($field, $value)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function all() |
101
|
|
|
{ |
102
|
|
|
return $this->applyScopes($this->getFiles()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Open the module's json file |
107
|
|
|
* |
108
|
|
|
* @param $file |
109
|
|
|
* @return null|ModuleDescriptor |
110
|
|
|
*/ |
111
|
|
|
protected function getFile($file) |
112
|
|
|
{ |
113
|
|
|
$path = $this->getFullPath($file); |
114
|
|
|
if (!$this->storage->exists($path)) { |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$data = json_decode($this->storage->get($path), true); |
119
|
|
|
return $this->getNewModuleDescriptor($data); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get all saved modules' json file |
124
|
|
|
* |
125
|
|
|
* @return Collection |
126
|
|
|
*/ |
127
|
|
|
protected function getFiles() |
128
|
|
|
{ |
129
|
|
|
$files = collect([]); |
130
|
|
|
foreach ($this->storage->allFiles($this->directory) as $file) { |
131
|
|
|
$data = json_decode($this->storage->get($file), true); |
132
|
|
|
$files->put($data['id'], $this->getNewModuleDescriptor($data)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $files; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a new ModuleDescriptor instance. |
140
|
|
|
* |
141
|
|
|
* @param array $attributes |
142
|
|
|
* @return ModuleDescriptor |
143
|
|
|
*/ |
144
|
|
|
protected function getNewModuleDescriptor(array $attributes) |
145
|
|
|
{ |
146
|
|
|
return new ModuleDescriptor($attributes); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the full path for a registry entry. |
151
|
|
|
* |
152
|
|
|
* @param $file |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
protected function getFullPath($file) |
156
|
|
|
{ |
157
|
|
|
return $this->directory.'/'.$file.'.json'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Apply the registered scopes |
162
|
|
|
* |
163
|
|
|
* @param $target |
164
|
|
|
* @return Collection |
165
|
|
|
*/ |
166
|
|
|
protected function applyScopes($target) |
167
|
|
|
{ |
168
|
|
|
$collection = $target; |
169
|
|
|
if (!($target instanceof Collection)) { |
170
|
|
|
$collection = collect([$target]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
foreach ($this->scopes as $data) { |
174
|
|
|
list($scope, $arguments) = $data; |
175
|
|
|
switch ($scope) { |
176
|
|
|
case 'enabled': |
177
|
|
|
$collection = $collection->where('is_enabled', true); |
178
|
|
|
break; |
179
|
|
|
default: |
180
|
|
|
throw new \InvalidArgumentException(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$this->scopes = []; |
185
|
|
|
|
186
|
|
|
return $collection; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @inheritdoc |
191
|
|
|
*/ |
192
|
|
|
protected function scope($scope, $arguments) |
193
|
|
|
{ |
194
|
|
|
$this->scopes[] = [$scope, $arguments]; |
195
|
|
|
|
196
|
|
|
return parent::scope($scope, $arguments); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|