|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelModulize\Services\Loaders; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Support\Facades\Route; |
|
8
|
|
|
use LaravelModulize\Contracts\LoadsFiles; |
|
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
10
|
|
|
use LaravelModulize\Contracts\ModulizerRepositoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class RoutesLoader extends BaseFileLoader implements LoadsFiles |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Load the files to load and register them |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $module |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
public function loadFiles(string $module): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->getFilesToLoad($module)->each(function ($routeFile) use ($module) { |
|
23
|
|
|
$this->registerRoute( |
|
24
|
|
|
$this->parseNamespace($module, $routeFile->getBasename()), |
|
25
|
|
|
$routeFile->getRealPath() |
|
26
|
|
|
); |
|
27
|
|
|
}); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Retrieve the path where the files to load should be at |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $module |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getFilesPath(string $module): string |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->repo->getModulePath($module) . "/Http/Routes"; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Retrieve the namespace to be used when registering the files |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $module |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getNamespace(string $module): string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->repo |
|
50
|
|
|
->getModuleNamespace($module) . '\\Http\\Controllers'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Parse the namespace that will be used in the Router |
|
55
|
|
|
* This enables the user to create as many route files as needed |
|
56
|
|
|
* If the baseName does not contain 'Routes' the namespace will be at the base Controller |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $module |
|
59
|
|
|
* @param string $baseName |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
private function parseNamespace(string $module, string $baseName): string |
|
63
|
|
|
{ |
|
64
|
|
|
$namespace = Str::contains($baseName, 'Routes') |
|
65
|
|
|
? Str::start(Str::studly(Str::before($baseName, 'Routes')), '\\') |
|
66
|
|
|
: ''; |
|
67
|
|
|
|
|
68
|
|
|
return $this->getNamespace($module) . $namespace; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* First we check if the routes have been cached, if not |
|
73
|
|
|
* Load the routes while registering the namespace. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $namespace |
|
76
|
|
|
* @param string $realPath |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
private function registerRoute(string $namespace, string $realPath) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$this->app->routesAreCached()) { |
|
82
|
|
|
Route::middleware('api') |
|
83
|
|
|
->namespace($namespace) |
|
84
|
|
|
->group($realPath); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|