Completed
Push — master ( 969ba2...61b422 )
by Abdelrahman
02:56 queued 01:47
created

WidgetsServiceProvider::publishResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
     * Register the service provider.
20
     *
21
     * @return void
22
     */
23
    public function register(): void
24
    {
25
        // Merge config
26
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.widgets');
27
28
        $this->registerWidgetFactory();
29
        $this->registerWidgetCollection();
30
31
        // Register console commands
32
        ! $this->app->runningInConsole() || $this->registerWidgetCommands();
33
    }
34
35
    /**
36
     * Register the widget collection.
37
     *
38
     * @return void
39
     */
40
    public function registerWidgetCollection(): void
41
    {
42
        // Register widget collection
43
        $this->app->singleton('rinvex.widgets.list', function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
            return collect();
45
        });
46
    }
47
48
    /**
49
     * Register the widget factory.
50
     *
51
     * @return void
52
     */
53
    public function registerWidgetFactory(): void
54
    {
55
        $this->app->singleton('rinvex.widgets', WidgetFactory::class);
56
57
        $this->app->singleton('rinvex.widgets.group', function () {
58
            return collect();
59
        });
60
    }
61
62
    /**
63
     * Bootstrap the application events.
64
     *
65
     * @return void
66
     */
67
    public function boot(Router $router): void
68
    {
69
        // Load resources
70
        $this->loadRoutes($router);
71
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'rinvex/widgets');
72
73
        // Publish Resources
74
        ! $this->app->runningInConsole() || $this->publishesViews('rinvex/laravel-widgets');
75
        ! $this->app->runningInConsole() || $this->publishesConfig('rinvex/laravel-widgets');
76
77
        // Register blade extensions
78
        $this->registerBladeExtensions();
79
    }
80
81
    /**
82
     * Load the routes.
83
     *
84
     * @param \Illuminate\Routing\Router $router
85
     *
86
     * @return void
87
     */
88
    protected function loadRoutes(Router $router): void
89
    {
90
        // Load routes
91
        if (! $this->app->routesAreCached() && config('rinvex.widgets.register_routes')) {
92
            $router->get('widget', function () {
93
                $factory = app('rinvex.widgets');
94
                $widgetName = urldecode(request()->input('name'));
95
                $widgetParams = $factory->decryptWidgetParams(request()->input('params', ''));
96
97
                return call_user_func_array([$factory, $widgetName], $widgetParams);
98
            })->name('rinvex.widgets.async')->middleware('web');
99
100
            $this->app->booted(function () use ($router) {
101
                $router->getRoutes()->refreshNameLookups();
102
                $router->getRoutes()->refreshActionLookups();
103
            });
104
        }
105
    }
106
107
    /**
108
     * Register console commands.
109
     *
110
     * @return void
111
     */
112
    protected function registerWidgetCommands(): void
113
    {
114
        // Register artisan commands
115
        $this->app->singleton('command.rinvex.widgets.make', function ($app) {
116
            return new WidgetMakeCommand($app['files']);
117
        });
118
119
        $this->commands(['command.rinvex.widgets.make']);
120
    }
121
122
    /**
123
     * Register the blade extensions.
124
     *
125
     * @return void
126
     */
127
    protected function registerBladeExtensions(): void
128
    {
129
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
130
            // @widget('App\Widgets\ExampleWidget', $paramArray, $asyncFlag)
131
            $bladeCompiler->directive('widget', function ($expression) {
132
                return "<?php echo app('rinvex.widgets')->make({$expression}); ?>";
133
            });
134
135
            // @widgetGroup('widgetGroupName')
136
            $bladeCompiler->directive('widgetGroup', function ($expression) {
137
                return "<?php echo app('rinvex.widgets.group')->group({$expression})->render(); ?>";
138
            });
139
        });
140
    }
141
}
142