1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelModulize\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\DetectsApplicationNamespace; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Factory as EloquentFactory; |
9
|
|
|
use LaravelModulize\Contracts\ModulizerRepositoryInterface; |
10
|
|
|
|
11
|
|
|
class ModulizerRepository implements ModulizerRepositoryInterface |
12
|
|
|
{ |
13
|
|
|
use DetectsApplicationNamespace; |
14
|
|
|
|
15
|
|
|
public $migrations = []; |
16
|
|
|
|
17
|
|
|
public $translations = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instance of Filesystem |
21
|
|
|
* |
22
|
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem |
23
|
|
|
*/ |
24
|
|
|
protected $filesystem; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Construct ModulizerRepository |
28
|
|
|
* |
29
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Filesystem $filesystem) |
32
|
|
|
{ |
33
|
|
|
$this->filesystem = $filesystem; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the configurable base path to the folder the modules will be in. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getBasePath(): string |
42
|
|
|
{ |
43
|
|
|
return config('modulizer.modules_path'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine if there are modules available |
48
|
|
|
* |
49
|
|
|
* @return boolean |
50
|
|
|
*/ |
51
|
|
|
public function hasModules(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->filesExist( |
54
|
|
|
$this->getBasePath() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Collect the available modules |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Support\Collection |
62
|
|
|
*/ |
63
|
|
|
public function getModules(): Collection |
64
|
|
|
{ |
65
|
|
|
return collect($this->filesystem->directories($this->getBasePath())) |
66
|
|
|
->map(function ($directory) { |
67
|
|
|
return class_basename($directory); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieve the path for a single module |
73
|
|
|
* |
74
|
|
|
* @param string $module |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getModulePath(string $module): string |
78
|
|
|
{ |
79
|
|
|
return $this->getBasePath() . "/{$module}"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Collect all files preset at the given path |
84
|
|
|
* |
85
|
|
|
* @param string $path |
86
|
|
|
* @return \Illuminate\Support\Collection |
87
|
|
|
*/ |
88
|
|
|
public function getFiles(string $path): Collection |
89
|
|
|
{ |
90
|
|
|
return collect($this->filesystem->files($path)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Collect all files preset at the given pattern |
95
|
|
|
* |
96
|
|
|
* @param string $path |
97
|
|
|
* @return \Illuminate\Support\Collection |
98
|
|
|
*/ |
99
|
|
|
public function glob(string $pattern): Collection |
100
|
|
|
{ |
101
|
|
|
return collect($this->filesystem->glob($pattern)); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Determine if a path or file exists |
106
|
|
|
* |
107
|
|
|
* @param string $path |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public function filesExist(string $path): bool |
111
|
|
|
{ |
112
|
|
|
return $this->filesystem->exists( |
113
|
|
|
$path |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the app's root namespace |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getRootNamespace(): string |
123
|
|
|
{ |
124
|
|
|
return $this->getAppNamespace(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the namespace the modules should recieve |
129
|
|
|
* This namespace will be a child of the root namespace |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getModulesNamespace(): string |
134
|
|
|
{ |
135
|
|
|
return config('modulizer.namespace'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Retrieve the namespace of a single module |
140
|
|
|
* |
141
|
|
|
* @param string $module |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getModuleNamespace(string $module): string |
145
|
|
|
{ |
146
|
|
|
return $this->getRootNamespace() . $this->getModulesNamespace() . $module; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Add a translation path |
151
|
|
|
* |
152
|
|
|
* @param string $path |
153
|
|
|
* @param string $namespace |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function addTranslation(string $path, string $namespace) |
157
|
|
|
{ |
158
|
|
|
$this->translations[] = (object) [ |
159
|
|
|
'path' => $path, |
160
|
|
|
'namespace' => $namespace, |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Add a migration to the array |
166
|
|
|
* |
167
|
|
|
* @param string $migrationPath |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function addMigration(string $migrationPath) |
171
|
|
|
{ |
172
|
|
|
$this->migrations[] = $migrationPath; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Register factories. |
177
|
|
|
* |
178
|
|
|
* @param string $path |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
public function registerEloquentFactoriesFrom($path) |
182
|
|
|
{ |
183
|
|
|
$this->app->make(EloquentFactory::class)->load($path); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..