Completed
Push — develop ( 462b65...dca6d7 )
by Abdelrahman
06:01
created

WidgetsServiceProvider::publishResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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\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
     * The commands to be registered.
17
     *
18
     * @var array
19
     */
20
    protected $commands = [
21
        WidgetMakeCommand::class => 'command.rinvex.widgets.make',
22
    ];
23
24
    /**
25
     * Register the service provider.
26
     *
27
     * @return void
28
     */
29
    public function register()
30
    {
31
        // Merge config
32
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.widgets');
33
34
        $this->registerWidgetFactory();
35
        $this->registerWidgetCollection();
36
37
        // Register console commands
38
        ! $this->app->runningInConsole() || $this->registerCommands();
39
    }
40
41
    /**
42
     * Register the widget collection.
43
     *
44
     * @return void
45
     */
46
    public function registerWidgetCollection()
47
    {
48
        // Register widget collection
49
        $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...
50
            return collect();
51
        });
52
    }
53
54
    /**
55
     * Register the widget factory.
56
     *
57
     * @return void
58
     */
59
    public function registerWidgetFactory()
60
    {
61
        $this->app->singleton('rinvex.widgets', 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...
62
            return new WidgetFactory();
63
        });
64
65
        $this->app->alias('rinvex.widgets', WidgetFactory::class);
66
67
        $this->app->singleton('rinvex.widgets.group', function () {
68
            return collect();
69
        });
70
    }
71
72
    /**
73
     * Bootstrap the application events.
74
     *
75
     * @return void
76
     */
77
    public function boot(Router $router)
78
    {
79
        // Load resources
80
        $this->loadRoutes($router);
81
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'rinvex/widgets');
82
83
        // Publish Resources
84
        ! $this->app->runningInConsole() || $this->publishResources();
85
86
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
87
            // @widget('widgetName')
88
            $bladeCompiler->directive('widget', function ($expression) {
89
                return "<?php echo app('rinvex.widgets')->make({$expression}); ?>";
90
            });
91
92
            // @widgetGroup('widgetName')
93
            $bladeCompiler->directive('widgetGroup', function ($expression) {
94
                return "<?php echo app('rinvex.widgets.group')->group({$expression})->render(); ?>";
95
            });
96
        });
97
    }
98
99
    /**
100
     * Load the routes.
101
     *
102
     * @param \Illuminate\Routing\Router $router
103
     *
104
     * @return void
105
     */
106
    protected function loadRoutes(Router $router)
107
    {
108
        // Load routes
109
        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...
110
            $router->get('widget', function () {
111
                $factory = app('rinvex.widgets');
112
                $widgetName = urldecode(request()->input('name'));
113
                $widgetParams = $factory->decryptWidgetParams(request()->input('params', ''));
114
115
                return call_user_func_array([$factory, $widgetName], $widgetParams);
116
            })->name('rinvex.widgets.async')->middleware('web');
117
118
            $this->app->booted(function () use ($router) {
119
                $router->getRoutes()->refreshNameLookups();
120
                $router->getRoutes()->refreshActionLookups();
121
            });
122
        }
123
    }
124
125
    /**
126
     * Publish resources.
127
     *
128
     * @return void
129
     */
130
    protected function publishResources()
131
    {
132
        $this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('rinvex.widgets.php')], 'rinvex-widgets-config');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
133
        $this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/rinvex/widgets')], 'rinvex-widgets-views');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 143 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
134
    }
135
136
    /**
137
     * Register console commands.
138
     *
139
     * @return void
140
     */
141
    protected function registerCommands()
142
    {
143
        // Register artisan commands
144
        foreach ($this->commands as $key => $value) {
145
            $this->app->singleton($value, function ($app) use ($key) {
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...
146
                return new $key();
147
            });
148
        }
149
150
        $this->commands(array_values($this->commands));
151
    }
152
}
153