1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use League\Fractal\Manager; |
7
|
|
|
use League\Fractal\Serializer\DataArraySerializer; |
8
|
|
|
use Yajra\DataTables\Transformers\FractalTransformer; |
9
|
|
|
|
10
|
|
|
class FractalServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Indicates if loading of the provider is deferred. |
14
|
|
|
* |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $defer = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Bootstrap the application events. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function boot() |
25
|
|
|
{ |
26
|
|
|
$this->mergeConfigFrom(__DIR__ . '/config/config.php', 'datatables-fractal'); |
27
|
|
|
$this->publishAssets(); |
28
|
|
|
|
29
|
|
|
$this->registerMacro(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Publish datatables assets. |
34
|
|
|
*/ |
35
|
|
|
protected function publishAssets() |
36
|
|
|
{ |
37
|
|
|
$this->publishes([ |
38
|
|
|
__DIR__ . '/config/config.php' => config_path('datatables-fractal.php'), |
39
|
|
|
], 'datatables-fractal'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register DataTables macro methods. |
44
|
|
|
*/ |
45
|
|
|
protected function registerMacro() |
46
|
|
|
{ |
47
|
|
|
DataTableAbstract::macro('setTransformer', function ($transformer) { |
48
|
|
|
$this->transformer = $transformer; |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
DataTableAbstract::macro('setSerializer', function ($serializer) { |
54
|
|
|
$this->serializer = $serializer; |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register the service provider. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function register() |
66
|
|
|
{ |
67
|
|
|
$this->app->singleton('datatables.fractal', function () { |
68
|
|
|
$fractal = new Manager; |
69
|
|
|
$config = $this->app['config']; |
70
|
|
|
$request = $this->app['request']; |
71
|
|
|
|
72
|
|
|
$includesKey = $config->get('datatables-fractal.includes', 'include'); |
73
|
|
|
if ($request->get($includesKey)) { |
74
|
|
|
$fractal->parseIncludes($request->get($includesKey)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$serializer = $config->get('datatables-fractal.serializer', DataArraySerializer::class); |
78
|
|
|
$fractal->setSerializer(new $serializer); |
79
|
|
|
|
80
|
|
|
return $fractal; |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$this->app->singleton('datatables.transformer', function () { |
84
|
|
|
return new FractalTransformer($this->app->make('datatables.fractal')); |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the services provided by the provider. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function provides() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
'datatables.fractal', |
97
|
|
|
'datatables.transformer', |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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: