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