WidgetsServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 11 1
A registerWidgetCollection() 0 7 1
A registerWidgetFactory() 0 8 1
A boot() 0 13 1
A loadRoutes() 0 18 3
A registerCommands() 0 9 1
A registerBladeExtensions() 0 14 1
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) {
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...
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')) {
0 ignored issues
show
Bug introduced by
The method routesAreCached() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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