Passed
Push — master ( f2b4ae...a14f49 )
by Iman
55s queued 11s
created

WidgetsServiceProvider::defineSlotDirectives()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 2
eloc 8
c 4
b 0
f 2
nc 1
nop 1
dl 0
loc 12
rs 10
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
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
58
        });
59
    }
60
61
    /**
62
     * | ------------------------------------------ |
63
     * |       Define Wdgetize Slots Directives     |
64
     * | ------------------------------------------ |
65
     * | When you call @ slot from your widget      |
66
     * | The only thing that happens is that the    |
67
     * | `renderSlot` method Gets called on the     |
68
     * | `Utils\SlotRenderer` trait                 |
69
     * | ------------------------------------------ |.
70
     */
71
    private function defineSlotDirectives($omitParenthesis)
72
    {
73
        Blade::directive('slot', function ($slotName) use ($omitParenthesis) {
74
            $slotName = $omitParenthesis ? $slotName : "($slotName)";
75
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->startSlot{$slotName};?>";
76
        });
77
78
        Blade::directive('endSlot', function () {
79
            $contentKey = '$content';
80
            return "<?php 
81
                        $contentKey = ob_get_clean();
82
                        echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderSlot($contentKey);
83
                    ?>";
84
        });
85
    }
86
87
    /**
88
     * Register any application services.
89
     *
90
     * @return void
91
     */
92
    public function register()
93
    {
94
        $this->mergeConfigFrom(__DIR__ . '/config/config.php', 'widgetize');
95
        $this->commands('command.imanghafoori.widget');
96
        app(RouteMacros::class)->registerMacros();
97
        app(SingletonServices::class)->registerSingletons($this->app);
98
    }
99
100
    private function _registerDebugbar()
101
    {
102
        if (!$this->app->offsetExists('debugbar')) {
103
            return;
104
        }
105
106
        $this->app->singleton('widgetize.debugger', function () {
107
            return new MessagesCollector('Widgets');
108
        });
109
110
        $this->app->make('debugbar')->addCollector(app('widgetize.debugger'));
111
    }
112
}
113