Completed
Push — master ( 390c77...fb089c )
by Mahmoud
03:19
created

PortServiceProviderTrait::loadConfigs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace App\Port\Provider\Traits;
4
5
use App;
6
use App\Port\Butler\Portals\Facade\PortButler;
7
use App\Port\Exception\Exceptions\UnsupportedFractalSerializerException;
8
use App\Port\Middleware\PortKernel;
9
use DB;
10
use File;
11
use Illuminate\Support\Facades\Config;
12
use Illuminate\Support\Facades\View;
13
use Log;
14
15
/**
16
 * Class PortServiceProviderTrait.
17
 *
18
 * @author  Mahmoud Zalt <[email protected]>
19
 */
20
trait PortServiceProviderTrait
21
{
22
23
    /**
24
     * Write the DB queries in the Log and Display them in the
25
     * terminal (in case you want to see them while executing the tests).
26
     *
27
     * @param bool|false $terminal
28
     */
29
    public function debugDatabaseQueries($log = true, $terminal = false)
30
    {
31
        if (Config::get('database.query_debugging')) {
32
            DB::listen(function ($event) use ($terminal, $log) {
33
                $fullQuery = vsprintf(str_replace(['%', '?'], ['%%', '%s'], $event->sql), $event->bindings);
34
35
                $text = $event->connectionName . ' (' . $event->time . '): ' . $fullQuery;
36
37
                if ($terminal) {
38
                    dump($text);
39
                }
40
41
                if ($log) {
42
                    Log::info($text);
43
                }
44
            });
45
        }
46
    }
47
48
    /**
49
     * By default Laravel takes (server/database/factories) as the
50
     * path to the factories, this function changes the path to load
51
     * the factories from the infrastructure directory.
52
     */
53
    public function changeTheDefaultDatabaseModelsFactoriesPath($customPath)
54
    {
55
        App::singleton(\Illuminate\Database\Eloquent\Factory::class, function ($app) use ($customPath) {
56
            $faker = $app->make(\Faker\Generator::class);
57
58
            return \Illuminate\Database\Eloquent\Factory::construct($faker, base_path() . $customPath);
59
        });
60
    }
61
62
    /**
63
     * Get the containers Service Providers full classes names.
64
     *
65
     * @return  array
66
     */
67
    public function getMainServiceProviders()
68
    {
69
        $containersNamespace = PortButler::getContainersNamespace();
70
71
        $allServiceProviders = [];
72
73
        foreach (PortButler::getContainersNames() as $containerName) {
74
            // append the Module main service provider
75
            $allServiceProviders[] = PortButler::buildMainServiceProvider($containersNamespace, $containerName);
76
        }
77
78
        return array_unique($allServiceProviders) ? : [];
79
    }
80
81
    /**
82
     * Load views from inside the Containers
83
     */
84
    public function autoLoadViewsFromContainers()
85
    {
86
        foreach (PortButler::getContainersNames() as $containerName) {
87
88
            $containerViewDirectory = base_path('app/Containers/' . $containerName . '/UI/WEB/Views/');
89
90
            if (File::isDirectory($containerViewDirectory)) {
91
                View::addLocation($containerViewDirectory);
92
            }
93
        }
94
    }
95
96
    /**
97
     * Load migrations files from Containers to Laravel
98
     */
99
    public function autoMigrationsFromContainers()
100
    {
101
        foreach (PortButler::getContainersNames() as $containerName) {
102
103
            $containerMigrationDirectory = base_path('app/Containers/' . $containerName . '/Data/Migrations');
104
105
            if (File::isDirectory($containerMigrationDirectory)) {
106
107
                App::afterResolving('migrator', function ($migrator) use ($containerMigrationDirectory) {
108
                    foreach ((array)$containerMigrationDirectory as $path) {
109
                        $migrator->path($path);
110
                    }
111
                });
112
            }
113
        }
114
    }
115
116
    /**
117
     * TODO: needs refactoring, was created in 5 min
118
     *
119
     * @return  array
120
     */
121
    public function getAllContainersConsoleCommandsForAutoLoading()
122
    {
123
        $classes = [];
124 View Code Duplication
        foreach (PortButler::getContainersNames() as $containerName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $containerCommandsDirectory = base_path('app/Containers/' . $containerName . '/UI/CLI/Commands/');
126
            if (File::isDirectory($containerCommandsDirectory)) {
127
                $files = \File::allFiles($containerCommandsDirectory);
128
                foreach ($files as $consoleFile) {
129
                    if (\File::isFile($consoleFile)) {
130
                        $pathName = $consoleFile->getPathname();
131
                        $classes[] = PortButler::getClassFullNameFromFile($pathName);
132
                    }
133
                }
134
            }
135
        };
136
137
        return $classes;
138
    }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    /**
158
     * By default the Dingo API package (in the config file) creates an instance of the
159
     * fractal manager which takes the default serializer (specified by the fractal
160
     * package itself, and there's no way to override change it from the configurations of
161
     * the Dingo package).
162
     *
163
     * Here I am replacing the current default serializer (DataArraySerializer) by the
164
     * (JsonApiSerializer).
165
     *
166
     * "Serializers are what build the final response after taking the transformers data".
167
     */
168
    public function overrideDefaultFractalSerializer()
169
    {
170
        $serializerName = Config::get('api.serializer');
171
172
        // if DataArray `\League\Fractal\Serializer\DataArraySerializer` do noting since it's set by default by the Dingo API
173
        if ($serializerName !== 'DataArray') {
174
            app('Dingo\Api\Transformer\Factory')->setAdapter(function () use ($serializerName) {
175
                switch ($serializerName) {
176
                    case 'JsonApi':
177
                        $serializer = new \League\Fractal\Serializer\JsonApiSerializer(Config::get('api.domain'));
178
                        break;
179
                    case 'Array':
180
                        $serializer = new \League\Fractal\Serializer\ArraySerializer(Config::get('api.domain'));
181
                        break;
182
                    default:
183
                        throw new UnsupportedFractalSerializerException('Unsupported ' . $serializerName);
184
                }
185
186
                $fractal = new \League\Fractal\Manager();
187
                $fractal->setSerializer($serializer);
188
189
                return new \Dingo\Api\Transformer\Adapter\Fractal($fractal, 'include', ',', false);
190
            });
191
        }
192
    }
193
194
    /**
195
     * @param array $middlewares
196
     * @param array $middlewareGroups
197
     * @param array $routeMiddlewares
198
     */
199
    public function registerAllMiddlewares(array $middlewares = [], array $middlewareGroups = [], array $routeMiddlewares = [])
200
    {
201
        // Registering single and grouped middleware's
202
        (App::make(PortKernel::class))
203
            ->registerMiddlewares($middlewares)
204
            ->registerMiddlewareGroups($middlewareGroups);
205
206
        // Registering Route Middleware's
207
        foreach ($routeMiddlewares as $key => $routeMiddleware){
208
            $this->app['router']->middleware($key, $routeMiddleware);
0 ignored issues
show
Bug introduced by
The property app 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...
209
        }
210
211
    }
212
213
214
}
215