Completed
Push — master ( 21470b...961dbd )
by Elf
06:51
created

replaceDataTablesBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ElfSundae\DataTables;
4
5
use Illuminate\Foundation\AliasLoader;
6
use Illuminate\Support\ServiceProvider;
7
8
class DataTablesServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Register the service provider.
12
     *
13
     * @return void
14
     */
15 7
    public function register()
16
    {
17 7
        $this->registerOriginalDataTables();
18
19 7
        $this->replaceDataTablesBindings();
20
21 7
        $this->configureDataTables();
22 7
    }
23
24
    /**
25
     * Register the original DataTables service providers.
26
     *
27
     * @return void
28
     */
29 7
    protected function registerOriginalDataTables()
30
    {
31 7
        $this->app->register(\Yajra\DataTables\DataTablesServiceProvider::class);
32 7
        AliasLoader::getInstance()->alias('DataTables', \Yajra\DataTables\Facades\DataTables::class);
33
34 7
        $this->app->register(\Yajra\DataTables\ButtonsServiceProvider::class);
35 7
    }
36
37
    /**
38
     * Replace the original DataTables bindings.
39
     *
40
     * @return void
41
     */
42 7
    protected function replaceDataTablesBindings()
43
    {
44 7
        $this->app->alias('datatables', DataTables::class);
45 7
        $this->app->singleton('datatables', function () {
46 4
            return new DataTables;
47 7
        });
48
49 7
        $this->app->bind('datatables.html', Html\Builder::class);
50 7
    }
51
52
    /**
53
     * Configure DataTables.
54
     *
55
     * @return void
56
     */
57 7
    protected function configureDataTables()
58
    {
59
        // Restore the default "builders" configuration.
60
        // See https://github.com/yajra/laravel-datatables/pull/1462/commits/afdbe6bce9ade9b19d66034ace3d8a2c24da8a56
61 7
        $this->mergeConfig('datatables.builders', [
62 7
            \Illuminate\Database\Eloquent\Builder::class => 'eloquent',
63
            \Illuminate\Database\Eloquent\Relations\Relation::class => 'eloquent',
64
            \Illuminate\Database\Query\Builder::class => 'query',
65
            \Illuminate\Support\Collection::class => 'collection',
66
        ]);
67
68
        // Configure our custom DataTable service stub for "make" command
69 7
        $this->mergeConfig('datatables-buttons', [
70 7
            'stub' => '/vendor/elfsundae/laravel-datatables/src/stubs',
71
        ]);
72 7
    }
73
74
    /**
75
     * Merge the given configuration with the existing configuration.
76
     *
77
     * @param  string  $key
78
     * @param  array  $config
79
     * @return void
80
     */
81 7
    protected function mergeConfig($key, array $config)
82
    {
83 7
        $current = $this->app['config']->get($key, []);
84
85 7
        $this->app['config']->set($key, array_merge($config, $current));
86 7
    }
87
}
88