FractalServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use League\Fractal\Manager;
6
use Illuminate\Support\ServiceProvider;
7
use League\Fractal\Serializer\DataArraySerializer;
8
use Yajra\DataTables\Commands\TransformerMakeCommand;
9
use Yajra\DataTables\Transformers\FractalTransformer;
10
11
class FractalServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Indicates if loading of the provider is deferred.
15
     *
16
     * @var bool
17
     */
18
    protected $defer = false;
19
20
    /**
21
     * Bootstrap the application events.
22
     *
23
     * @return void
24
     */
25
    public function boot()
26
    {
27
        $this->mergeConfigFrom(__DIR__ . '/../config/datatables-fractal.php', 'datatables-fractal');
28
        $this->publishAssets();
29
30
        $this->registerMacro();
31
    }
32
33
    /**
34
     * Publish datatables assets.
35
     *
36
     * @return void
37
     */
38
    protected function publishAssets()
39
    {
40
        $this->publishes(
41
            [
42
            __DIR__ . '/../config/datatables-fractal.php' => config_path('datatables-fractal.php'),
43
            ], 'datatables-fractal'
44
        );
45
    }
46
47
    /**
48
     * Register DataTables macro methods.
49
     *
50
     * @return void
51
     */
52
    protected function registerMacro()
53
    {
54
        DataTableAbstract::macro(
55
            'setTransformer', function ($transformer) {
56
                $this->transformer = [$transformer];
0 ignored issues
show
Bug introduced by
The property transformer 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
58
                return $this;
59
            }
60
        );
61
62
        DataTableAbstract::macro(
63
            'addTransformer', function ($transformer) {
64
                $this->transformer[] = $transformer;
65
66
                return $this;
67
            }
68
        );
69
70
        DataTableAbstract::macro(
71
            'setSerializer', function ($serializer) {
72
                $this->serializer = $serializer;
0 ignored issues
show
Bug introduced by
The property serializer 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...
73
74
                return $this;
75
            }
76
        );
77
    }
78
79
    /**
80
     * Register the service provider.
81
     *
82
     * @return void
83
     */
84
    public function register()
85
    {
86
        $this->app->singleton(
87
            'datatables.fractal', function () {
88
                $fractal = new Manager;
89
                $config  = $this->app['config'];
90
                $request = $this->app['request'];
91
92
                $includesKey = $config->get('datatables-fractal.includes', 'include');
93
                if ($request->get($includesKey)) {
94
                    $fractal->parseIncludes($request->get($includesKey));
95
                }
96
97
                $serializer = $config->get('datatables-fractal.serializer', DataArraySerializer::class);
98
                $fractal->setSerializer(new $serializer);
99
100
                return $fractal;
101
            }
102
        );
103
104
        $this->app->singleton(
105
            'datatables.transformer', function () {
106
                return new FractalTransformer($this->app->make('datatables.fractal'));
107
            }
108
        );
109
110
        $this->commands(
111
            [
112
                TransformerMakeCommand::class,
113
            ]
114
        );
115
    }
116
117
    /**
118
     * Get the services provided by the provider.
119
     *
120
     * @return array
121
     */
122
    public function provides()
123
    {
124
        return [
125
            'datatables.fractal',
126
            'datatables.transformer',
127
        ];
128
    }
129
}
130