|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the EOffice project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace EOffice\Core\Providers; |
|
15
|
|
|
|
|
16
|
|
|
use Illuminate\Support\Facades\Route; |
|
17
|
|
|
use Illuminate\Support\ServiceProvider; |
|
18
|
|
|
use Laravel\Lumen\Routing\Router; |
|
19
|
|
|
|
|
20
|
|
|
class RouteServiceProvider extends ServiceProvider |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array<array-key,array<array-key,null|string>> |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
private static array $webRoutes = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array<array-key|string,null|string> |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
private static array $apiRoutes = []; |
|
31
|
|
|
|
|
32
|
|
|
public function boot(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->loadRoutes(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function register(): void |
|
38
|
|
|
{ |
|
39
|
|
|
route_register_web(__DIR__.'/../Resources/routes/web.php'); |
|
40
|
|
|
route_register_api(__DIR__.'/../Resources/routes/api.php'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function addWebRoute(string $filePath, string $prefix = '/', ?string $namespace = null): void |
|
44
|
|
|
{ |
|
45
|
|
|
$filePath = realpath($filePath); |
|
46
|
|
|
static::$webRoutes[$filePath] = [ |
|
|
|
|
|
|
47
|
|
|
'namespace' => $namespace, |
|
48
|
|
|
'prefix' => $prefix, |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function addApiRoute(string $filePath, ?string $namespace = null): void |
|
53
|
|
|
{ |
|
54
|
|
|
$filePath = realpath($filePath); |
|
55
|
|
|
static::$apiRoutes[$filePath] = $namespace; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @psalm-suppress UnresolvableInclude |
|
60
|
|
|
*/ |
|
61
|
|
|
private function loadRoutes(): void |
|
62
|
|
|
{ |
|
63
|
|
|
foreach (static::$webRoutes as $filePath => $namespace) { |
|
|
|
|
|
|
64
|
|
|
Route::group(['prefix' => '/', 'namespace' => $namespace], function (Router $router) use ($filePath) { |
|
65
|
|
|
require $filePath; |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
foreach (static::$apiRoutes as $filePath => $namespace) { |
|
|
|
|
|
|
70
|
|
|
Route::group(['prefix' => 'api', 'namespace' => $namespace], function (Router $router) use ($filePath) { |
|
71
|
|
|
require $filePath; |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|