1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Yajra\DataTables\DataTables; |
7
|
|
|
use Yajra\DataTables\Utilities\Config; |
8
|
|
|
use Yajra\DataTables\Utilities\Request; |
9
|
|
|
|
10
|
|
|
class DataTablesServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Register the service provider. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
public function register() |
18
|
|
|
{ |
19
|
|
|
if ($this->isLumen()) { |
20
|
|
|
require_once 'lumen.php'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->setupAssets(); |
24
|
|
|
|
25
|
|
|
$this->app->alias('datatables', DataTables::class); |
26
|
|
|
$this->app->singleton('datatables', function () { |
27
|
|
|
return new DataTables; |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
$this->app->singleton('datatables.request', function () { |
31
|
|
|
return new Request; |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
$this->app->singleton('datatables.config', Config::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Boot the instance, add macros for datatable engines. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function boot() |
43
|
|
|
{ |
44
|
|
|
$engines = config('datatables.engines'); |
45
|
|
|
foreach ($engines as $engine => $class) { |
46
|
|
|
$engine = camel_case($engine); |
47
|
|
|
|
48
|
|
|
if (!method_exists(DataTables::class, $engine) && !DataTables::hasMacro($engine)) { |
49
|
|
|
DataTables::macro($engine, function () use ($class) { |
50
|
|
|
if (!call_user_func_array(array($class, 'canCreate'), func_get_args())) { |
51
|
|
|
throw new \InvalidArgumentException(); |
52
|
|
|
} |
53
|
|
|
return call_user_func_array(array($class, 'create'), func_get_args()); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Setup package assets. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
protected function setupAssets() |
65
|
|
|
{ |
66
|
|
|
$this->mergeConfigFrom($config = __DIR__ . '/config/datatables.php', 'datatables'); |
67
|
|
|
|
68
|
|
|
if ($this->app->runningInConsole()) { |
69
|
|
|
$this->publishes([$config => config_path('datatables.php')], 'datatables'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if app uses Lumen. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected function isLumen() |
79
|
|
|
{ |
80
|
|
|
return str_contains($this->app->version(), 'Lumen'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|