Completed
Push — master ( f8b6bf...bec27e )
by Mahmoud
03:02
created

loadRoutesFromContainersForApi()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 41
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
rs 8.8571
cc 3
eloc 16
nc 3
nop 2
1
<?php
2
3
namespace App\Port\Loader\Loaders;
4
5
use App\Port\Loader\Helpers\Facade\LoaderHelper;
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 Route;
11
use Symfony\Component\Finder\SplFileInfo;
12
13
/**
14
 * Class RoutesLoaderTrait.
15
 *
16
 * @author  Mahmoud Zalt <[email protected]>
17
 */
18
trait RoutesLoaderTrait
19
{
20
21
    /**
22
     * Register all the containers routes files in the framework
23
     */
24
    public function runRoutesAutoLoader()
25
    {
26
        $containersPaths = LoaderHelper::getContainersPaths();
27
        $containersNamespace = LoaderHelper::getContainersNamespace();
28
29
        foreach ($containersPaths as $containerPath) {
30
            $this->loadRoutesFromContainersForApi($containerPath, $containersNamespace);
31
            $this->loadRoutesFromContainersForWeb($containerPath, $containersNamespace);
32
        }
33
    }
34
35
    /**
36
     * Register the Containers API routes files
37
     *
38
     * @param $containerPath
39
     * @param $containersNamespace
40
     */
41
    private function loadRoutesFromContainersForApi($containerPath, $containersNamespace)
42
    {
43
        // get the container api routes path
44
        $apiRoutesPath = $containerPath . '/UI/API/Routes';
45
46
        if (File::isDirectory($apiRoutesPath)) {
47
48
            // get all files from the container API routes directory
49
            $files = File::allFiles($apiRoutesPath);
50
51
            foreach ($files as $file) {
52
53
                // get the version from the file name to register it
54
                $apiVersionNumber = $this->getRouteFileVersionNumber($file);
55
56
                $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...
57
                    function (DingoApiRouter $router) use ($file, $containerPath, $containersNamespace) {
58
59
                        $controllerNamespace = $containersNamespace . '\\Containers\\' . basename($containerPath) . '\\UI\API\Controllers';
60
61
                        $router->group([
62
                            // Routes Namespace
63
                            'namespace'  => $controllerNamespace,
64
                            // Add Middleware's - 'api' is a group of middleware's
65
                            'middleware' => ['api'],
66
                            // The API limit time.
67
                            'limit'      => Config::get('hello.api.limit'),
68
                            // The API limit expiry time.
69
                            'expires'    => Config::get('hello.api.limit_expires'),
70
                        ], function ($router) use ($file) {
71
72
                            require $file->getPathname();
73
74
                        });
75
76
                    });
77
78
            }
79
80
        }
81
    }
82
83
    /**
84
     * Register the Containers WEB routes files
85
     *
86
     * @param $containerPath
87
     * @param $containersNamespace
88
     */
89
    private function loadRoutesFromContainersForWeb($containerPath, $containersNamespace)
90
    {
91
        // get the container web routes path
92
        $webRoutesPath = $containerPath . '/UI/WEB/Routes';
93
94
        if (File::isDirectory($webRoutesPath)) {
95
            // get all files from the container Web routes directory
96
            $files = File::allFiles($webRoutesPath);
97
98
            $controllerNamespace = $containersNamespace . '\\Containers\\' . basename($containerPath) . '\\UI\WEB\Controllers';
99
100
            foreach ($files as $file) {
101
                $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...
102
                    'middleware' => ['web'],
103
                    'namespace'  => $controllerNamespace,
104
                ], function (LaravelRouter $router) use ($file) {
105
                    require $file->getPathname();
106
                });
107
            }
108
        }
109
110
    }
111
112
113
    /**
114
     * @param $file
115
     *
116
     * @return  int
117
     */
118
    private function getRouteFileVersionNumber($file)
119
    {
120
        $fileNameWithoutExtension = $this->getRouteFileNameWithoutExtension($file);
121
122
        $fileNameWithoutExtensionExploded = explode('.', $fileNameWithoutExtension);
123
124
        end($fileNameWithoutExtensionExploded);
125
        $apiVersion = prev($fileNameWithoutExtensionExploded); // get the array before the last one
126
127
        $apiVersionNumber = str_replace('v', '', $apiVersion);
128
129
        return (is_int($apiVersionNumber) ? $apiVersionNumber : 1);
130
    }
131
132
    /**
133
     * @param \Symfony\Component\Finder\SplFileInfo $file
134
     *
135
     * @return  mixed
136
     */
137
    private function getRouteFileNameWithoutExtension(SplFileInfo $file)
138
    {
139
        $fileInfo = pathinfo($file->getFileName());
140
141
        return $fileInfo['filename'];
142
    }
143
}
144