Passed
Pull Request — master (#68)
by yasin
02:43
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
            if (strpos($expression, 'slotable') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($expression, 'slotable') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
46
                return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
47
            }
48
49
            $this->expression = preg_replace("/, 'slotable'|,'slotable'/", '', $expression);
50
        });
51
52
        $this->defineSlotDirectives($omitParenthesis);
53
54
        Blade::directive('endwidget', function () {
55
            $expression = $this->expression;
56
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
57
        });
58
    }
59
60
    /**
61
     * | ------------------------------------------ |
62
     * |       Define Wdgetize Slots Directives     |
63
     * | ------------------------------------------ |
64
     * | When you call @ slot from your widget      |
65
     * | The only thing that happens is that the    |
66
     * | `renderSlot` method Gets called on the     |
67
     * | `Utils\SlotRenderer` trait                 |
68
     * | ------------------------------------------ |.
69
     */
70
    private function defineSlotDirectives($omitParenthesis)
71
    {
72
        Blade::directive('slot', function ($slotName) use ($omitParenthesis) {
73
            $slotName = $omitParenthesis ? $slotName : "($slotName)";
74
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->startSlot{$slotName};?>";
75
        });
76
77
        Blade::directive('endslot', function () {
78
            $contentKey = '$content';
79
            return "<?php 
80
                        $contentKey = ob_get_clean();
81
                        echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderSlot($contentKey);
82
                    ?>";
83
        });
84
    }
85
86
    /**
87
     * Register any application services.
88
     *
89
     * @return void
90
     */
91
    public function register()
92
    {
93
        $this->mergeConfigFrom(__DIR__ . '/config/config.php', 'widgetize');
94
        $this->commands('command.imanghafoori.widget');
95
        app(RouteMacros::class)->registerMacros();
96
        app(SingletonServices::class)->registerSingletons($this->app);
97
    }
98
99
    private function _registerDebugbar()
100
    {
101
        if (!$this->app->offsetExists('debugbar')) {
102
            return;
103
        }
104
105
        $this->app->singleton('widgetize.debugger', function () {
106
            return new MessagesCollector('Widgets');
107
        });
108
109
        $this->app->make('debugbar')->addCollector(app('widgetize.debugger'));
110
    }
111
}
112