1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelModulize\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\DetectsApplicationNamespace; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Illuminate\Database\Eloquent\Factory as EloquentFactory; |
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use LaravelModulize\Contracts\ModulizerRepositoryInterface; |
11
|
|
|
|
12
|
|
|
class ModulizerRepository implements ModulizerRepositoryInterface |
13
|
|
|
{ |
14
|
|
|
use DetectsApplicationNamespace; |
15
|
|
|
|
16
|
|
|
public $migrations = []; |
17
|
|
|
|
18
|
|
|
public $translations = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Instance of Filesystem |
22
|
|
|
* |
23
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
24
|
|
|
*/ |
25
|
|
|
protected $filesystem; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
29
|
|
|
*/ |
30
|
|
|
protected $app; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Construct ModulizerRepository |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
36
|
|
|
*/ |
37
|
16 |
|
public function __construct(Filesystem $filesystem, Application $app) |
38
|
|
|
{ |
39
|
16 |
|
$this->app = $app; |
40
|
16 |
|
$this->filesystem = $filesystem; |
41
|
16 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the configurable base path to the folder the modules will be in. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
16 |
|
public function getBasePath(): string |
49
|
|
|
{ |
50
|
16 |
|
return app_path(config('modulizer.modules_path')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Determine if there are modules available |
55
|
|
|
* |
56
|
|
|
* @return boolean |
57
|
|
|
*/ |
58
|
16 |
|
public function hasModules(): bool |
59
|
|
|
{ |
60
|
16 |
|
return $this->filesExist( |
61
|
16 |
|
$this->getBasePath() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Collect the available modules |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Support\Collection |
69
|
|
|
*/ |
70
|
|
|
public function getModules(): Collection |
71
|
|
|
{ |
72
|
|
|
return collect($this->filesystem->directories($this->getBasePath())) |
73
|
|
|
->map(function ($directory) { |
74
|
|
|
return class_basename($directory); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieve the path for a single module |
80
|
|
|
* |
81
|
|
|
* @param string $module |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getModulePath(string $module): string |
85
|
|
|
{ |
86
|
|
|
return $this->getBasePath() . "/{$module}"; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Collect all files preset at the given path |
91
|
|
|
* |
92
|
|
|
* @param string $path |
93
|
|
|
* @return \Illuminate\Support\Collection |
94
|
|
|
*/ |
95
|
|
|
public function getFiles(string $path): Collection |
96
|
|
|
{ |
97
|
|
|
return collect($this->filesystem->files($path)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Collect all files preset at the given pattern |
102
|
|
|
* |
103
|
|
|
* @param string $path |
104
|
|
|
* @return \Illuminate\Support\Collection |
105
|
|
|
*/ |
106
|
|
|
public function glob(string $pattern): Collection |
107
|
|
|
{ |
108
|
|
|
return collect($this->filesystem->glob($pattern)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Determine if a path or file exists |
113
|
|
|
* |
114
|
|
|
* @param string $path |
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
16 |
|
public function filesExist(string $path): bool |
118
|
|
|
{ |
119
|
16 |
|
return $this->filesystem->exists( |
120
|
16 |
|
$path |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the app's root namespace |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function getRootNamespace(): string |
130
|
|
|
{ |
131
|
|
|
return $this->getAppNamespace(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the namespace the modules should recieve |
136
|
|
|
* This namespace will be a child of the root namespace |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getModulesNamespace(): string |
141
|
|
|
{ |
142
|
|
|
return config('modulizer.namespace'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Retrieve the namespace of a single module |
147
|
|
|
* |
148
|
|
|
* @param string $module |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getModuleNamespace(string $module): string |
152
|
|
|
{ |
153
|
|
|
return $this->getRootNamespace() . $this->getModulesNamespace() . $module; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add a translation path |
158
|
|
|
* |
159
|
|
|
* @param string $path |
160
|
|
|
* @param string $namespace |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public function addTranslation(string $path, string $namespace) |
164
|
|
|
{ |
165
|
|
|
$this->translations[] = (object) [ |
166
|
|
|
'path' => $path, |
167
|
|
|
'namespace' => $namespace, |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add a migration to the array |
173
|
|
|
* |
174
|
|
|
* @param string $migrationPath |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function addMigration(string $migrationPath) |
178
|
|
|
{ |
179
|
|
|
$this->migrations[] = $migrationPath; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Register factories. |
184
|
|
|
* |
185
|
|
|
* @param string $path |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function registerEloquentFactoriesFrom($path) |
189
|
|
|
{ |
190
|
|
|
$this->app->make(EloquentFactory::class)->load($path); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Create directory at given path |
195
|
|
|
* |
196
|
|
|
* @param string $path |
197
|
|
|
* @return void |
198
|
|
|
* @author Roy Freij <[email protected]> |
199
|
|
|
* @version 2019-03-04 |
200
|
|
|
*/ |
201
|
|
|
public function createDirectory(string $path): void |
202
|
|
|
{ |
203
|
|
|
if (!$this->filesExist($path)) { |
204
|
|
|
$this->filesystem->makeDirectory($path, 0755, true); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Retrieve the database folder path of given module |
210
|
|
|
* |
211
|
|
|
* @param string $module |
212
|
|
|
* @return string |
213
|
|
|
* @author Roy Freij <[email protected]> |
214
|
|
|
* @version 2019-03-04 |
215
|
|
|
*/ |
216
|
|
|
public function databasePath(string $module): string |
217
|
|
|
{ |
218
|
|
|
return $this->getModulePath($module) . '/database'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function exceptionPath(string $module): string |
222
|
|
|
{ |
223
|
|
|
return $this->getModulePath($module) . '/Exceptions'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function modelPath(string $module): string |
227
|
|
|
{ |
228
|
|
|
return $this->getModulePath($module) . '/Models'; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function controllerPath(string $module): string |
232
|
|
|
{ |
233
|
|
|
return $this->getModulePath($module) . '/Http/Controllers'; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|