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

DatatablesServiceProvider::publishAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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