1
|
|
|
<?php |
2
|
|
|
namespace Caffeinated\Modules; |
3
|
|
|
|
4
|
|
|
use Caffeinated\Modules\Repositories\Interfaces\ModuleRepositoryInterface; |
5
|
|
|
use Illuminate\Foundation\Application; |
6
|
|
|
|
7
|
|
|
class Modules implements ModuleRepositoryInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \Illuminate\Foundation\Application |
11
|
|
|
*/ |
12
|
|
|
protected $app; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \Caffeinated\Modules\Repositories\Interfaces\ModuleRepositoryInterface |
16
|
|
|
*/ |
17
|
|
|
protected $repository; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor method. |
21
|
|
|
* |
22
|
|
|
* @param \Caffeinated\Modules\Repositories\Interfaces\ModuleRepositoryInterface $repository |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct(Application $app, ModuleRepositoryInterface $repository) |
25
|
|
|
{ |
26
|
1 |
|
$this->app = $app; |
27
|
1 |
|
$this->repository = $repository; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Register the module service provider file from all modules. |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function register() |
36
|
|
|
{ |
37
|
|
|
$modules = $this->repository->enabled(); |
38
|
|
|
|
39
|
|
|
$modules->each(function($properties, $slug) { |
|
|
|
|
40
|
|
|
$this->registerServiceProvider($properties); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Register the module service provider. |
46
|
|
|
* |
47
|
|
|
* @param string $properties |
48
|
|
|
* @return string |
49
|
|
|
* @throws \Caffeinated\Modules\Exception\FileMissingException |
50
|
|
|
*/ |
51
|
|
|
protected function registerServiceProvider($properties) |
52
|
|
|
{ |
53
|
|
|
$module = studly_case($properties['slug']); |
54
|
|
|
$file = $this->repository->getPath()."/{$module}/Providers/{$module}ServiceProvider.php"; |
|
|
|
|
55
|
|
|
$namespace = $this->repository->getNamespace()."\\".$module."\\Providers\\{$module}ServiceProvider"; |
56
|
|
|
|
57
|
|
|
$this->app->register($namespace); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get all modules. |
62
|
|
|
* |
63
|
|
|
* @return Collection |
64
|
|
|
*/ |
65
|
|
|
public function all() |
66
|
|
|
{ |
67
|
|
|
return $this->repository->all(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get all module slugs. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function slugs() |
76
|
|
|
{ |
77
|
|
|
return $this->repository->slugs(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get modules based on where clause. |
82
|
|
|
* |
83
|
|
|
* @param string $key |
84
|
|
|
* @param mixed $value |
85
|
|
|
* @return Collection |
86
|
|
|
*/ |
87
|
|
|
public function where($key, $value) |
88
|
|
|
{ |
89
|
|
|
return $this->repository->where($key, $value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sort modules by given key in ascending order. |
94
|
|
|
* |
95
|
|
|
* @param string $key |
96
|
|
|
* @return Collection |
97
|
|
|
*/ |
98
|
|
|
public function sortBy($key) |
99
|
|
|
{ |
100
|
|
|
return $this->repository->sortBy($key); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sort modules by given key in ascending order. |
105
|
|
|
* |
106
|
|
|
* @param string $key |
107
|
|
|
* @return Collection |
108
|
|
|
*/ |
109
|
|
|
public function sortByDesc($key) |
110
|
|
|
{ |
111
|
|
|
return $this->repository->sortByDesc($key); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check if the given module exists. |
116
|
|
|
* |
117
|
|
|
* @param string $slug |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function exists($slug) |
121
|
|
|
{ |
122
|
|
|
return $this->repository->exists($slug); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns count of all modules. |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
public function count() |
131
|
|
|
{ |
132
|
|
|
return $this->repository->count(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get modules path. |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getPath() |
141
|
|
|
{ |
142
|
|
|
return $this->repository->getPath(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set modules path in "RunTime" mode. |
147
|
|
|
* |
148
|
|
|
* @param string $path |
149
|
|
|
* @return object $this |
150
|
|
|
*/ |
151
|
|
|
public function setPath($path) |
152
|
|
|
{ |
153
|
|
|
return $this->repository->setPath($path); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get path for the specified module. |
158
|
|
|
* |
159
|
|
|
* @param string $slug |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
public function getModulePath($slug) |
163
|
|
|
{ |
164
|
|
|
return $this->repository->getModulePath($slug); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get modules namespace. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getNamespace() |
173
|
|
|
{ |
174
|
|
|
return $this->repository->getNamespace(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get a module's properties. |
179
|
|
|
* |
180
|
|
|
* @param string $slug |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
public function getProperties($slug) |
184
|
|
|
{ |
185
|
|
|
return $this->repository->getProperties($slug); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get a module property value. |
190
|
|
|
* |
191
|
|
|
* @param string $property |
192
|
|
|
* @param mixed $default |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
public function getProperty($property, $default = null) |
196
|
|
|
{ |
197
|
|
|
return $this->repository->getProperty($property, $default); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Set a module property value. |
202
|
|
|
* |
203
|
|
|
* @param string $property |
204
|
|
|
* @param mixed $value |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function setProperty($property, $value) |
208
|
|
|
{ |
209
|
|
|
return $this->repository->setProperty($property, $value); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Gets all enabled modules. |
214
|
|
|
* |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
public function enabled() |
218
|
|
|
{ |
219
|
|
|
return $this->repository->enabled(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets all disabled modules. |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
public function disabled() |
228
|
|
|
{ |
229
|
|
|
return $this->repository->disabled(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Check if specified module is enabled. |
234
|
|
|
* |
235
|
|
|
* @param string $slug |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public function isEnabled($slug) |
239
|
|
|
{ |
240
|
|
|
return $this->repository->isEnabled($slug); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Check if specified module is disabled. |
245
|
|
|
* |
246
|
|
|
* @param string $slug |
247
|
|
|
* @return bool |
248
|
|
|
*/ |
249
|
|
|
public function isDisabled($slug) |
250
|
|
|
{ |
251
|
|
|
return $this->repository->isDisabled($slug); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Enables the specified module. |
256
|
|
|
* |
257
|
|
|
* @param string $slug |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
|
|
public function enable($slug) |
261
|
|
|
{ |
262
|
|
|
return $this->repository->enable($slug); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Disables the specified module. |
267
|
|
|
* |
268
|
|
|
* @param string $slug |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function disable($slug) |
272
|
|
|
{ |
273
|
|
|
return $this->repository->disable($slug); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.