Passed
Pull Request — master (#2)
by ANTHONIUS
05:01
created

RouteServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key,array<array-key,null|string>> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,array<array-key,null|string>>.
Loading history...
24
     */
25
    private static array $webRoutes = [];
26
27
    /**
28
     * @var array<array-key|string,null|string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key|string,null|string> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key|string,null|string>.
Loading history...
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] = [
0 ignored issues
show
Bug introduced by
Since $webRoutes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $webRoutes to at least protected.
Loading history...
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;
0 ignored issues
show
Bug introduced by
Since $apiRoutes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $apiRoutes to at least protected.
Loading history...
56
    }
57
58
    /**
59
     * @psalm-suppress UnresolvableInclude
60
     */
61
    private function loadRoutes(): void
62
    {
63
        foreach (static::$webRoutes as $filePath => $namespace) {
0 ignored issues
show
Bug introduced by
Since $webRoutes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $webRoutes to at least protected.
Loading history...
64
            Route::group(['prefix' => '/', 'namespace' => $namespace], function (Router $router) use ($filePath) {
65
                require $filePath;
66
            });
67
        }
68
69
        foreach (static::$apiRoutes as $filePath => $namespace) {
0 ignored issues
show
Bug introduced by
Since $apiRoutes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $apiRoutes to at least protected.
Loading history...
70
            Route::group(['prefix' => 'api', 'namespace' => $namespace], function (Router $router) use ($filePath) {
71
                require $filePath;
72
            });
73
        }
74
    }
75
}
76