Completed
Push — master ( a18619...6f9ccc )
by Elf
01:11
created

DataTablesServiceProvider::mergeConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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