-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleRepository.php
255 lines (226 loc) · 7.34 KB
/
ModuleRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace Mods\Foundation;
use Mods\Theme\Theme;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use MJS\TopSort\ElementNotFoundException;
use MJS\TopSort\Implementations\FixedArraySort as SortModules;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
class ModuleRepository
{
/**
* The application implementation.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The path to the manifest file.
*
* @var string
*/
protected $manifestPath;
/**
* Create a new service repository instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $manifestPath
* @return void
*/
public function __construct(ApplicationContract $app, Filesystem $files, $manifestPath)
{
$this->app = $app;
$this->files = $files;
$this->manifestPath = $manifestPath;
}
/**
* Register the application modules.
*
* @param array $modules
* @return void
*/
public function load(array $modules, array $paths)
{
$manifest = $this->loadManifest();
$modules = collect($modules)->filter(function ($value, $key) {
return $value;
})->keys()->all();
$modules = array_combine($modules, $modules);
if ($this->shouldRecompile($manifest, $modules)) {
$manifest = $this->compileManifest($modules, $paths);
}
try {
$autoload = $this->app['autoloader'];
} catch (\Exception $e) {
$autoload = require $this->app->bootstrapPath().'/autoload.php';
}
$this->registerAutoload($autoload, $manifest['autoload']);
$config = $this->app['config'];
$config->set('module.manifest', $manifest);
foreach ($manifest['themes'] as $area => $themes) {
$config->set('theme.'.$area.'.themes',
collect($themes)->map(function ($item, $key) {
return new Theme($item);
})->toArray()
);
}
$config->set('app.providers', $manifest['providers']);
$config->set('app.aliases', $manifest['aliases']);
}
/**
* Compile the application manifest file.
*
* @param array $modules
* @return array
*/
protected function compileManifest($activeModules, $paths)
{
// The service manifest should contain a list of all of the modules for
// the application so we can compare it on each request to the service
// and determine if the manifest should be recompiled or is current.
$manifest = $this->freshManifest($activeModules);
$moduleSorter = new SortModules();
$moduleSorter->add('mod_foundation', []);
foreach (Finder::create()->files()->name('register.php')->in($paths) as $file) {
$modules = $this->files->getRequire($file->getRealPath());
foreach ($modules as $key => $module) {
if ($module['type'] == 'module') {
if ($key != 'mod_foundation' && isset($activeModules[$key])) {
$moduleSorter->add($key, $module['depends']);
}
$manifest['modules'][$key] = $module;
} else {
$manifest['themes'][$module['area']][$key] = $module;
}
}
}
$notFoundModule = array_diff_key($activeModules, $manifest['modules']);
if ($notFoundModuleCount = count($notFoundModule)) {
$word = ($notFoundModuleCount > 1)?'Modules':'Module';
echo(implode(', ', $notFoundModule). " $word not Found");
exit(0);
}
try {
$manifest['relsoved'] = $moduleSorter->sort();
} catch (ElementNotFoundException $e) {
echo($e->getMessage());
exit(0);
}
foreach ($manifest['relsoved'] as $code) {
$manifest['providers'] = array_merge(
$manifest['providers'],
$manifest['modules'][$code]['providers']
);
$manifest['aliases'] = array_merge(
$manifest['aliases'],
$manifest['modules'][$code]['aliases']
);
if (isset($manifest['modules'][$code]['autoload'])) {
$manifest['autoload'] = array_merge_recursive(
$manifest['autoload'],
$manifest['modules'][$code]['autoload']
);
}
}
return $this->writeManifest($manifest);
}
protected function registerAutoload($loader, $autoload)
{
$map = $autoload['psr-0'];
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = $autoload['psr-4'];
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = $autoload['classmap'];
if ($classMap) {
$loader->addClassMap($classMap);
}
$files = $autoload['files'];
if ($files) {
foreach ($files as $file) {
$fileIdentifier = md5($file);
$this->composerRequireFile($fileIdentifier, $file);
}
}
}
/**
* Determine if the manifest should be compiled.
*
* @param array $manifest
* @param array $modules
* @return bool
*/
public function shouldRecompile($manifest, $modules)
{
return is_null($manifest) || $manifest['active'] != $modules;
}
/**
* Load the service provider manifest JSON file.
*
* @return array|null
*/
public function loadManifest()
{
if ($this->files->exists($this->manifestPath)) {
$manifest = $this->files->getRequire($this->manifestPath);
if ($manifest) {
return $manifest;
}
}
}
/**
* Write the service manifest file to disk.
*
* @param array $manifest
* @return array
*/
public function writeManifest($manifest)
{
if ($this->app['config']->get('app.env') == 'production') {
$this->files->put(
$this->manifestPath, '<?php return '.var_export($manifest, true).';'
);
}
return $manifest;
}
/**
* Create a fresh service manifest data structure.
*
* @param array $modules
* @return array
*/
protected function freshManifest(array $modules)
{
return [
'active' => $modules,
'relsoved' => [] ,
'modules' => [],
'themes' => [],
'providers' => [],
'aliases' => [],
'autoload' => [
'psr-4' => [],
'psr-0' => [],
'files' => [],
'classmap' => [],
],
];
}
protected function composerRequireFile($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
}
}
}