|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Port\Provider\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use App; |
|
6
|
|
|
use App\Port\Exception\Exceptions\UnsupportedFractalSerializerException; |
|
7
|
|
|
use App\Port\Middleware\PortKernel; |
|
8
|
|
|
use DB; |
|
9
|
|
|
use File; |
|
10
|
|
|
use Illuminate\Support\Facades\Config; |
|
11
|
|
|
use Log; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PortServiceProviderTrait. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
trait PortServiceProviderTrait |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Write the DB queries in the Log and Display them in the |
|
23
|
|
|
* terminal (in case you want to see them while executing the tests). |
|
24
|
|
|
* |
|
25
|
|
|
* @param bool|false $terminal |
|
26
|
|
|
*/ |
|
27
|
|
|
public function debugDatabaseQueries($log = true, $terminal = false) |
|
28
|
|
|
{ |
|
29
|
|
|
if (Config::get('database.query_debugging')) { |
|
30
|
|
|
DB::listen(function ($event) use ($terminal, $log) { |
|
31
|
|
|
$fullQuery = vsprintf(str_replace(['%', '?'], ['%%', '%s'], $event->sql), $event->bindings); |
|
32
|
|
|
|
|
33
|
|
|
$text = $event->connectionName . ' (' . $event->time . '): ' . $fullQuery; |
|
34
|
|
|
|
|
35
|
|
|
if ($terminal) { |
|
36
|
|
|
dump($text); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($log) { |
|
40
|
|
|
Log::info($text); |
|
41
|
|
|
} |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* By default the Dingo API package (in the config file) creates an instance of the |
|
48
|
|
|
* fractal manager which takes the default serializer (specified by the fractal |
|
49
|
|
|
* package itself, and there's no way to override change it from the configurations of |
|
50
|
|
|
* the Dingo package). |
|
51
|
|
|
* |
|
52
|
|
|
* Here I am replacing the current default serializer (DataArraySerializer) by the |
|
53
|
|
|
* (JsonApiSerializer). |
|
54
|
|
|
* |
|
55
|
|
|
* "Serializers are what build the final response after taking the transformers data". |
|
56
|
|
|
*/ |
|
57
|
|
|
public function overrideDefaultFractalSerializer() |
|
58
|
|
|
{ |
|
59
|
|
|
$serializerName = Config::get('api.serializer'); |
|
60
|
|
|
|
|
61
|
|
|
// if DataArray `\League\Fractal\Serializer\DataArraySerializer` do noting since it's set by default by the Dingo API |
|
62
|
|
|
if ($serializerName !== 'DataArray') { |
|
63
|
|
|
app('Dingo\Api\Transformer\Factory')->setAdapter(function () use ($serializerName) { |
|
64
|
|
|
switch ($serializerName) { |
|
65
|
|
|
case 'JsonApi': |
|
66
|
|
|
$serializer = new \League\Fractal\Serializer\JsonApiSerializer(Config::get('api.domain')); |
|
67
|
|
|
break; |
|
68
|
|
|
case 'Array': |
|
69
|
|
|
$serializer = new \League\Fractal\Serializer\ArraySerializer(Config::get('api.domain')); |
|
70
|
|
|
break; |
|
71
|
|
|
default: |
|
72
|
|
|
throw new UnsupportedFractalSerializerException('Unsupported ' . $serializerName); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$fractal = new \League\Fractal\Manager(); |
|
76
|
|
|
$fractal->setSerializer($serializer); |
|
77
|
|
|
|
|
78
|
|
|
return new \Dingo\Api\Transformer\Adapter\Fractal($fractal, 'include', ',', false); |
|
79
|
|
|
}); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $middlewares |
|
85
|
|
|
* @param array $middlewareGroups |
|
86
|
|
|
* @param array $routeMiddlewares |
|
87
|
|
|
*/ |
|
88
|
|
|
public function registerAllMiddlewares( |
|
89
|
|
|
array $middlewares = [], |
|
90
|
|
|
array $middlewareGroups = [], |
|
91
|
|
|
array $routeMiddlewares = [] |
|
92
|
|
|
) { |
|
93
|
|
|
// Registering single and grouped middleware's |
|
94
|
|
|
(App::make(PortKernel::class)) |
|
95
|
|
|
->registerMiddlewares($middlewares) |
|
96
|
|
|
->registerMiddlewareGroups($middlewareGroups); |
|
97
|
|
|
|
|
98
|
|
|
// Registering Route Middleware's |
|
99
|
|
|
foreach ($routeMiddlewares as $key => $routeMiddleware) { |
|
100
|
|
|
$this->app['router']->middleware($key, $routeMiddleware); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: