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