Completed
Push — master ( bdecd9...23aff6 )
by Arjay
06:02
created

DatatablesServiceProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 89
rs 10
c 2
b 0
f 0
wmc 8
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
B register() 0 29 3
A isLumen() 0 4 1
A registerAliases() 0 7 2
A provides() 0 4 1
1
<?php
2
3
namespace Yajra\Datatables;
4
5
use Collective\Html\HtmlServiceProvider;
6
use Illuminate\Support\ServiceProvider;
7
use League\Fractal\Manager;
8
use League\Fractal\Serializer\DataArraySerializer;
9
10
/**
11
 * Class DatatablesServiceProvider.
12
 *
13
 * @package Yajra\Datatables
14
 * @author  Arjay Angeles <[email protected]>
15
 */
16
class DatatablesServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Indicates if loading of the provider is deferred.
20
     *
21
     * @var bool
22
     */
23
    protected $defer = false;
24
25
    /**
26
     * Bootstrap the application events.
27
     *
28
     * @return void
29
     */
30
    public function boot()
31
    {
32
        $this->mergeConfigFrom(__DIR__ . '/config/datatables.php.php', 'datatables');
33
34
        $this->publishes([
35
            __DIR__ . '/config/datatables.php.php' => config_path('datatables.php'),
36
        ], 'datatables');
37
    }
38
39
    /**
40
     * Register the service provider.
41
     *
42
     * @return void
43
     */
44
    public function register()
45
    {
46
        if ($this->isLumen()) {
47
            require_once 'fallback.php';
48
        }
49
50
        $this->app->singleton('datatables.fractal', function () {
51
            $fractal = new Manager;
52
            $config  = $this->app['config'];
53
            $request = $this->app['request'];
54
55
            $includesKey = $config->get('datatables.fractal.includes', 'include');
56
            if ($request->get($includesKey)) {
57
                $fractal->parseIncludes($request->get($includesKey));
58
            }
59
60
            $serializer = $config->get('datatables.fractal.serializer', DataArraySerializer::class);
61
            $fractal->setSerializer(new $serializer);
62
63
            return $fractal;
64
        });
65
66
        $this->app->alias('datatables', Datatables::class);
67
        $this->app->singleton('datatables', function () {
68
            return new Datatables($this->app->make(Request::class));
69
        });
70
71
        $this->registerAliases();
72
    }
73
74
    /**
75
     * Check if app uses Lumen.
76
     *
77
     * @return bool
78
     */
79
    protected function isLumen()
80
    {
81
        return str_contains($this->app->version(), 'Lumen');
82
    }
83
84
    /**
85
     * Create aliases for the dependency.
86
     */
87
    protected function registerAliases()
88
    {
89
        if (class_exists('Illuminate\Foundation\AliasLoader')) {
90
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
91
            $loader->alias('Datatables', \Yajra\Datatables\Facades\Datatables::class);
92
        }
93
    }
94
95
    /**
96
     * Get the services provided by the provider.
97
     *
98
     * @return string[]
99
     */
100
    public function provides()
101
    {
102
        return ['datatables', 'datatables.fractal'];
103
    }
104
}
105