WidgetsServiceProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 24
Bugs 0 Features 3
Metric Value
eloc 34
c 24
b 0
f 3
dl 0
loc 105
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 9 1
A defineSlotDirectives() 0 14 2
A _registerDebugbar() 0 11 2
A defineDirectives() 0 20 3
A register() 0 6 1
1
<?php
2
3
namespace Imanghafoori\Widgets;
4
5
use DebugBar\DataCollector\MessagesCollector;
0 ignored issues
show
Bug introduced by
The type DebugBar\DataCollector\MessagesCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Blade;
7
use Illuminate\Support\ServiceProvider;
8
9
class WidgetsServiceProvider extends ServiceProvider
10
{
11
    private $expression;
12
13
    /**
14
     * Bootstrap any application services.
15
     *
16
     * @return void
17
     */
18
    public function boot()
19
    {
20
        $this->_registerDebugbar();
21
        $this->publishes([
22
            __DIR__.'/config/config.php' => config_path('widgetize.php'),
23
        ]);
24
25
        $this->defineDirectives();
26
        $this->loadViewsFrom($this->app->basePath().'/app/Widgets/', 'Widgets');
27
    }
28
29
    /**
30
     * | ------------------------------------------ |
31
     * |         Define Blade Directives            |
32
     * | ------------------------------------------ |
33
     * | When you call @ widget from your views     |
34
     * | The only thing that happens is that the    |
35
     * | `renderWidget` method Gets called on the   |
36
     * | `Utils\WidgetRenderer` class               |
37
     * | ------------------------------------------ |.
38
     */
39
    private function defineDirectives()
40
    {
41
        $omitParenthesis = version_compare($this->app->version(), '5.3', '<');
42
43
        Blade::directive('widget', function ($expression) use ($omitParenthesis) {
44
            $expression = $omitParenthesis ? $expression : "($expression)";
45
46
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
47
        });
48
49
        Blade::directive('slotWidget', function ($expression) use ($omitParenthesis) {
50
            $this->expression = $omitParenthesis ? $expression : "($expression)";
51
        });
52
53
        $this->defineSlotDirectives($omitParenthesis);
54
55
        Blade::directive('endSlotWidget', function () {
56
            $expression = $this->expression;
57
58
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
59
        });
60
    }
61
62
    /**
63
     * | ------------------------------------------ |
64
     * |       Define Wdgetize Slots Directives     |
65
     * | ------------------------------------------ |
66
     * | When you call @ slot from your widget      |
67
     * | The only thing that happens is that the    |
68
     * | `renderSlot` method Gets called on the     |
69
     * | `Utils\SlotRenderer` trait                 |
70
     * | ------------------------------------------ |.
71
     */
72
    private function defineSlotDirectives($omitParenthesis)
73
    {
74
        Blade::directive('slot', function ($slotName) use ($omitParenthesis) {
75
            $slotName = $omitParenthesis ? $slotName : "($slotName)";
76
77
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->startSlot{$slotName};?>";
78
        });
79
80
        Blade::directive('endSlot', function () {
81
            $contentKey = '$content';
82
83
            return "<?php 
84
                        $contentKey = ob_get_clean();
85
                        echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderSlot($contentKey);
86
                    ?>";
87
        });
88
    }
89
90
    /**
91
     * Register any application services.
92
     *
93
     * @return void
94
     */
95
    public function register()
96
    {
97
        $this->mergeConfigFrom(__DIR__.'/config/config.php', 'widgetize');
98
        $this->commands('command.imanghafoori.widget');
99
        app(RouteMacros::class)->registerMacros();
100
        app(SingletonServices::class)->registerSingletons($this->app);
101
    }
102
103
    private function _registerDebugbar()
104
    {
105
        if (! $this->app->offsetExists('debugbar')) {
106
            return;
107
        }
108
109
        $this->app->singleton('widgetize.debugger', function () {
110
            return new MessagesCollector('Widgets');
111
        });
112
113
        $this->app->make('debugbar')->addCollector(app('widgetize.debugger'));
114
    }
115
}
116