Skip to content

Commit 88056aa

Browse files
committed
Adds path remapping for module directories
1 parent 90de059 commit 88056aa

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

config/modules.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@
5555
*/
5656

5757
'driver' => 'local',
58+
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Remap Module Subdirectories
62+
|--------------------------------------------------------------------------
63+
|
64+
| Redefine how module directories are structured. The mapping here will
65+
| be respected by all commands and generators.
66+
|
67+
*/
68+
69+
'pathMap' => [
70+
// To change where migrations go, specify the default
71+
// location as the key and the new location as the value:
72+
// 'Database/Migrations' => 'src/Database/Migrations',
73+
],
5874
];

src/Console/Commands/ModuleMigrateCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ protected function migrate($slug)
136136
*/
137137
protected function getMigrationPath($slug)
138138
{
139-
$path = $this->module->getModulePath($slug);
140-
141-
return $path.'Database/Migrations/';
139+
return module_path($slug, 'Database/Migrations');
142140
}
143141

144142
/**

src/Console/Commands/ModuleMigrateResetCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ protected function getParameters($slug)
185185
*/
186186
protected function getMigrationPath($slug)
187187
{
188-
$path = $this->module->getModulePath($slug).'Database/Migrations';
189-
190-
return $path;
188+
return module_path($slug, 'Database/Migrations');
191189
}
192190

193191
/**

src/Console/GeneratorCommand.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ protected function parseName($name)
4040
*/
4141
protected function getPath($name)
4242
{
43-
$rootNamespace = config('modules.namespace');
44-
$name = str_replace($rootNamespace, '', $name);
43+
$slug = $this->argument('slug');
44+
45+
// take everything after the module name in the given path (ignoring case)
46+
$key = array_search(strtolower($slug), explode('\\', strtolower($name)));
47+
if ($key === false) {
48+
$newPath = str_replace('\\', '/', $name);
49+
} else {
50+
$newPath = implode('/', array_slice(explode('\\', $name), $key + 1));
51+
}
4552

46-
return module_path().'/'.str_replace('\\', '/', $name).'.php';
53+
return module_path($slug, "$newPath.php");
4754
}
4855
}

src/Console/Generators/MakeModuleCommand.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,33 @@ protected function generateModule()
154154
$this->files->makeDirectory(module_path());
155155
}
156156

157+
$pathMap = config('modules.pathMap');
157158
$directory = module_path(null, $this->container['basename']);
158159
$source = __DIR__.'/../../../resources/stubs/module';
159160

160161
$this->files->makeDirectory($directory);
161-
$this->files->copyDirectory($source, $directory, null);
162162

163-
$files = $this->files->allFiles($directory);
163+
$sourceFiles = $this->files->allFiles($source, true);
164164

165-
foreach ($files as $file) {
165+
if (!empty($pathMap)) {
166+
$search = array_keys($pathMap);
167+
$replace = array_values($pathMap);
168+
}
169+
170+
foreach ($sourceFiles as $file) {
166171
$contents = $this->replacePlaceholders($file->getContents());
167-
$filePath = module_path(null, $this->container['basename'].'/'.$file->getRelativePathname());
172+
$subPath = $file->getRelativePathname();
173+
174+
if (!empty($pathMap)) {
175+
$subPath = str_replace($search, $replace, $subPath);
176+
}
177+
178+
$filePath = $directory . '/' . $subPath;
179+
$dir = dirname($filePath);
180+
181+
if (!$this->files->isDirectory($dir)) {
182+
$this->files->makeDirectory($dir, 0755, true);
183+
}
168184

169185
$this->files->put($filePath, $contents);
170186
}

src/Helpers/module.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,33 @@
1010
*/
1111
function module_path($module = null, $file = '')
1212
{
13+
$modulesPath = config('modules.path');
14+
$pathMap = config('modules.pathMap');
15+
16+
if (!empty($file) && !empty($pathMap)) {
17+
$file = str_replace(
18+
array_keys($pathMap),
19+
array_values($pathMap),
20+
$file
21+
);
22+
}
23+
24+
$filePath = $file ? '/' . ltrim($file, '/') : '';
25+
1326
if (is_null($module)) {
1427
if (empty($file)) {
15-
return config('modules.path');
28+
return $modulesPath;
1629
}
1730

18-
return config('modules.path').'/'.$file;
31+
return $modulesPath . $filePath;
1932
}
2033

2134
$module = Module::where('slug', $module);
2235

23-
if (empty($file)) {
24-
return config('modules.path').'/'.$module['basename'];
25-
}
26-
27-
return config('modules.path').'/'.$module['basename'].'/'.$file;
36+
return $modulesPath . '/' . $module['basename'] . $filePath;
2837
}
2938
}
3039

31-
3240
if (! function_exists('module_class')) {
3341
/**
3442
* Return the full path to the given module class.

0 commit comments

Comments
 (0)