1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Widgets\Providers; |
6
|
|
|
|
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Rinvex\Widgets\Factories\WidgetFactory; |
10
|
|
|
use Illuminate\View\Compilers\BladeCompiler; |
11
|
|
|
use Rinvex\Widgets\Console\Commands\WidgetMakeCommand; |
12
|
|
|
|
13
|
|
|
class WidgetsServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Register the service provider. |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function register(): void |
21
|
|
|
{ |
22
|
|
|
// Merge config |
23
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.widgets'); |
24
|
|
|
|
25
|
|
|
$this->registerWidgetFactory(); |
26
|
|
|
$this->registerWidgetCollection(); |
27
|
|
|
|
28
|
|
|
// Register console commands |
29
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Register the widget collection. |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function registerWidgetCollection(): void |
38
|
|
|
{ |
39
|
|
|
// Register widget collection |
40
|
|
|
$this->app->singleton('rinvex.widgets.list', function ($app) { |
|
|
|
|
41
|
|
|
return collect(); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Register the widget factory. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function registerWidgetFactory(): void |
51
|
|
|
{ |
52
|
|
|
$this->app->singleton('rinvex.widgets', WidgetFactory::class); |
53
|
|
|
|
54
|
|
|
$this->app->singleton('rinvex.widgets.group', function () { |
55
|
|
|
return collect(); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Bootstrap the application events. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function boot(Router $router): void |
65
|
|
|
{ |
66
|
|
|
// Load resources |
67
|
|
|
$this->loadRoutes($router); |
68
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views', 'rinvex/widgets'); |
69
|
|
|
|
70
|
|
|
// Publish Resources |
71
|
|
|
! $this->app->runningInConsole() || $this->publishResources(); |
72
|
|
|
|
73
|
|
|
// Register blade extensions |
74
|
|
|
$this->registerBladeExtensions(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Load the routes. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Routing\Router $router |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
protected function loadRoutes(Router $router): void |
85
|
|
|
{ |
86
|
|
|
// Load routes |
87
|
|
|
if (! $this->app->routesAreCached() && config('rinvex.widgets.register_routes')) { |
|
|
|
|
88
|
|
|
$router->get('widget', function () { |
89
|
|
|
$factory = app('rinvex.widgets'); |
90
|
|
|
$widgetName = urldecode(request()->input('name')); |
91
|
|
|
$widgetParams = $factory->decryptWidgetParams(request()->input('params', '')); |
92
|
|
|
|
93
|
|
|
return call_user_func_array([$factory, $widgetName], $widgetParams); |
94
|
|
|
})->name('rinvex.widgets.async')->middleware('web'); |
95
|
|
|
|
96
|
|
|
$this->app->booted(function () use ($router) { |
97
|
|
|
$router->getRoutes()->refreshNameLookups(); |
98
|
|
|
$router->getRoutes()->refreshActionLookups(); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Publish resources. |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
protected function publishResources(): void |
109
|
|
|
{ |
110
|
|
|
$this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('rinvex.widgets.php')], 'rinvex-widgets-config'); |
|
|
|
|
111
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/rinvex/widgets')], 'rinvex-widgets-views'); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Register console commands. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
protected function registerCommands(): void |
120
|
|
|
{ |
121
|
|
|
// Register artisan commands |
122
|
|
|
$this->app->singleton('command.rinvex.widgets.make', function ($app) { |
123
|
|
|
return new WidgetMakeCommand($app['files']); |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
$this->commands(['command.rinvex.widgets.make']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Register the blade extensions. |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
protected function registerBladeExtensions(): void |
135
|
|
|
{ |
136
|
|
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) { |
137
|
|
|
// @widget('App\Widgets\ExampleWidget', $paramArray, $asyncFlag) |
138
|
|
|
$bladeCompiler->directive('widget', function ($expression) { |
139
|
|
|
return "<?php echo app('rinvex.widgets')->make({$expression}); ?>"; |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
// @widgetGroup('widgetGroupName') |
143
|
|
|
$bladeCompiler->directive('widgetGroup', function ($expression) { |
144
|
|
|
return "<?php echo app('rinvex.widgets.group')->group({$expression})->render(); ?>"; |
145
|
|
|
}); |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.