1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Contains the RouteRegistrar class. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2019 Attila Fulop |
9
|
|
|
* @author Attila Fulop |
10
|
|
|
* @license MIT |
11
|
|
|
* @since 2019-06-05 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Konekt\Concord\Routing; |
16
|
|
|
|
17
|
|
|
use Illuminate\Support\Arr; |
18
|
|
|
use Illuminate\Support\Facades\File; |
19
|
|
|
use Illuminate\Support\Facades\Route; |
20
|
|
|
use Konekt\Concord\Contracts\Convention; |
21
|
|
|
use Konekt\Concord\Contracts\Module; |
22
|
|
|
|
23
|
|
|
class RouteRegistrar |
24
|
|
|
{ |
25
|
|
|
/** @var Module */ |
26
|
|
|
private $module; |
27
|
|
|
|
28
|
|
|
/** @var Convention */ |
29
|
|
|
private $convention; |
30
|
|
|
|
31
|
|
|
public function __construct(Module $module, Convention $convention) |
32
|
|
|
{ |
33
|
|
|
$this->module = $module; |
34
|
|
|
$this->convention = $convention; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function registerAllRoutes() |
38
|
|
|
{ |
39
|
|
|
$routeFiles = collect(File::glob($this->getRoutesFolder() . '/*.php'))->map(function ($file) { |
|
|
|
|
40
|
|
|
return File::name($file); |
41
|
|
|
})->all(); |
42
|
|
|
|
43
|
|
|
$this->registerRoutes($routeFiles); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function registerRoutes(array $files, array $config = []) |
47
|
|
|
{ |
48
|
|
|
$path = $this->getRoutesFolder(); |
49
|
|
|
|
50
|
|
|
if (is_dir($path)) { |
51
|
|
|
$routes = $files; |
52
|
|
|
|
53
|
|
|
foreach ($routes as $route) { |
54
|
|
|
Route::group( |
55
|
|
|
[ |
56
|
|
|
'namespace' => Arr::get($config, 'namespace', $this->getDefaultRouteNamespace()), |
57
|
|
|
'prefix' => Arr::get($config, 'prefix', $this->module->shortName()), |
58
|
|
|
'as' => Arr::get($config, 'as', $this->module->shortName() . '.'), |
59
|
|
|
'middleware' => Arr::get($config, 'middleware', ['web']) |
60
|
|
|
], |
61
|
|
|
sprintf('%s/%s.php', $path, $route) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the default namespace for routes/controllers within a box/module |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function getDefaultRouteNamespace() |
73
|
|
|
{ |
74
|
|
|
return sprintf( |
75
|
|
|
'%s\\%s', |
76
|
|
|
$this->module->getNamespaceRoot(), |
77
|
|
|
str_replace('/', '\\', $this->convention->controllersFolder()) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getRoutesFolder(): string |
82
|
|
|
{ |
83
|
|
|
return $this->module->getBasePath() . '/' . $this->convention->routesFolder(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|