Completed
Push — master ( 485292...d7caaf )
by Mahmoud
06:15
created

RoutesLoaderTrait::loadApiRoute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 3
1
<?php
2
3
namespace App\Ship\Engine\Loaders;
4
5
use App\Ship\Engine\Butlers\Facades\ShipButler;
6
use Dingo\Api\Routing\Router as DingoApiRouter;
7
use Illuminate\Routing\Router as LaravelRouter;
8
use Illuminate\Support\Facades\Config;
9
use Illuminate\Support\Facades\File;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
/**
13
 * Class RoutesLoaderTrait.
14
 *
15
 * @author  Mahmoud Zalt <[email protected]>
16
 */
17
trait RoutesLoaderTrait
18
{
19
20
    /**
21
     * Register all the containers routes files in the framework
22
     */
23
    public function runRoutesAutoLoader()
24
    {
25
        $containersPaths = ShipButler::getContainersPaths();
26
        $containersNamespace = ShipButler::getContainersNamespace();
27
28
        foreach ($containersPaths as $containerPath) {
29
            $this->loadRoutesFromContainersForApi($containerPath, $containersNamespace);
30
            $this->loadRoutesFromContainersForWeb($containerPath, $containersNamespace);
31
        }
32
    }
33
34
    /**
35
     * Register the Containers API routes files
36
     *
37
     * @param $containerPath
38
     * @param $containersNamespace
39
     */
40
    private function loadRoutesFromContainersForApi($containerPath, $containersNamespace)
41
    {
42
        // get the container api routes path
43
        $apiRoutesPath = $containerPath . '/UI/API/Routes';
44
45
        if (File::isDirectory($apiRoutesPath)) {
46
            // get all files from the container API routes directory
47
            $files = File::allFiles($apiRoutesPath);
48
49
            foreach ($files as $file) {
50
                $this->loadApiRoute($file, $containerPath, $containersNamespace);
51
            }
52
        }
53
    }
54
55
    /**
56
     * Register the Containers WEB routes files
57
     *
58
     * @param $containerPath
59
     * @param $containersNamespace
60
     */
61
    private function loadRoutesFromContainersForWeb($containerPath, $containersNamespace)
62
    {
63
        // get the container web routes path
64
        $webRoutesPath = $containerPath . '/UI/WEB/Routes';
65
66
        if (File::isDirectory($webRoutesPath)) {
67
            // get all files from the container Web routes directory
68
            $files = File::allFiles($webRoutesPath);
69
70
            $controllerNamespace = $containersNamespace . '\\Containers\\' . basename($containerPath) . '\\UI\WEB\Controllers';
71
72
            foreach ($files as $file) {
73
                $this->loadWebRoute($file, $controllerNamespace);
74
            }
75
        }
76
    }
77
78
    /**
79
     * @param $file
80
     * @param $controllerNamespace
81
     */
82
    private function loadWebRoute($file, $controllerNamespace)
83
    {
84
        $this->webRouter->group([
0 ignored issues
show
Bug introduced by
The property webRouter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
            'middleware' => ['web'],
86
            'namespace'  => $controllerNamespace,
87
        ], function (LaravelRouter $router) use ($file) {
88
            require $file->getPathname();
89
        });
90
    }
91
92
    /**
93
     * @param $file
94
     * @param $containerPath
95
     * @param $containersNamespace
96
     */
97
    private function loadApiRoute($file, $containerPath, $containersNamespace)
98
    {
99
        // get the version from the file name to register it
100
        $apiVersionNumber = $this->getRouteFileVersionNumber($file);
101
102
        $this->apiRouter->version('v' . $apiVersionNumber,
0 ignored issues
show
Bug introduced by
The property apiRouter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103
            function (DingoApiRouter $router) use ($file, $containerPath, $containersNamespace) {
104
105
                $controllerNamespace = $containersNamespace . '\\Containers\\' . basename($containerPath) . '\\UI\API\Controllers';
106
107
                $router->group([
108
                    // Routes Namespace
109
                    'namespace'  => $controllerNamespace,
110
                    // Add Middleware's - 'api' is a group of middleware's
111
                    'middleware' => ['api'],
112
                    // The API limit time.
113
                    'limit'      => Config::get('hello.api.limit'),
114
                    // The API limit expiry time.
115
                    'expires'    => Config::get('hello.api.limit_expires'),
116
                ], function ($router) use ($file) {
117
118
                    require $file->getPathname();
119
120
                });
121
122
            });
123
    }
124
125
126
    /**
127
     * @param $file
128
     *
129
     * @return  int
130
     */
131
    private function getRouteFileVersionNumber($file)
132
    {
133
        $fileNameWithoutExtension = $this->getRouteFileNameWithoutExtension($file);
134
135
        $fileNameWithoutExtensionExploded = explode('.', $fileNameWithoutExtension);
136
137
        end($fileNameWithoutExtensionExploded);
138
        $apiVersion = prev($fileNameWithoutExtensionExploded); // get the array before the last one
139
140
        $apiVersionNumber = str_replace('v', '', $apiVersion);
141
142
        return (is_int($apiVersionNumber) ? $apiVersionNumber : 1);
143
    }
144
145
    /**
146
     * @param \Symfony\Component\Finder\SplFileInfo $file
147
     *
148
     * @return  mixed
149
     */
150
    private function getRouteFileNameWithoutExtension(SplFileInfo $file)
151
    {
152
        $fileInfo = pathinfo($file->getFileName());
153
154
        return $fileInfo['filename'];
155
    }
156
}
157