Completed
Push — master ( 2f76f5...b625cd )
by Iman
01:40
created

WidgetsServiceProvider::defineDirectives()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Support\ServiceProvider;
8
use Imanghafoori\Widgets\Utils\Normalizer;
9
use Imanghafoori\Widgets\Utils\Normalizers\CacheNormalizer;
10
use Imanghafoori\Widgets\Utils\Normalizers\ContextAsNormalizer;
11
use Imanghafoori\Widgets\Utils\Normalizers\TemplateNormalizer;
12
use Imanghafoori\Widgets\Utils\Normalizers\PresenterNormalizer;
13
use Imanghafoori\Widgets\Utils\Normalizers\ControllerNormalizer;
14
15
class WidgetsServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Bootstrap any application services.
19
     *
20
     * @return void
21
     */
22
    public function boot()
23
    {
24
        $this->publishes([
25
            __DIR__.'/config/config.php' => config_path('widgetize.php'),
26
        ]);
27
28
        $this->defineDirectives();
29
        $this->loadViewsFrom($this->app->basePath().'/app/Widgets/', 'Widgets');
30
    }
31
32
    /**
33
     * Define Blade Directives.
34
     */
35
    private function defineDirectives()
36
    {
37
        $omitParenthesis = version_compare($this->app->version(), '5.3', '<');
38
39
        Blade::directive('widget', function ($expression) use ($omitParenthesis) {
40
            $expression = $omitParenthesis ? $expression : "($expression)";
41
42
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
43
        });
44
    }
45
46
    /**
47
     * Register any application services.
48
     *
49
     * @return void
50
     */
51
    public function register()
52
    {
53
        $this->mergeConfigFrom(__DIR__.'/config/config.php', 'widgetize');
54
        $this->commands('command.imanghafoori.widget');
55
        $this->registerSingletons();
56
        $this->registerMacros();
57
    }
58
59
    /**
60
     * Register classes as singletons.
61
     */
62
    private function registerSingletons()
63
    {
64
        $this->app->singleton('command.imanghafoori.widget', function ($app) {
65
            return $app['Imanghafoori\Widgets\WidgetGenerator'];
66
        });
67
68
        $this->app->singleton(Normalizer::class, function () {
69
            $cacheNormalizer = new CacheNormalizer();
70
            $tplNormalizer = new TemplateNormalizer();
71
            $presenterNormalizer = new PresenterNormalizer();
72
            $ctrlNormalizer = new ControllerNormalizer();
73
            $contextAsNormalizer = new ContextAsNormalizer();
74
75
            return new Utils\Normalizer($tplNormalizer, $cacheNormalizer, $presenterNormalizer, $ctrlNormalizer, $contextAsNormalizer);
76
        });
77
78
        $this->app->singleton(Utils\HtmlMinifier::class, function () {
79
            return new Utils\HtmlMinifier();
80
        });
81
82
        $this->app->singleton(Utils\DebugInfo::class, function () {
83
            return new Utils\DebugInfo();
84
        });
85
86
        $this->app->singleton(Utils\Policies::class, function () {
87
            return new Utils\Policies();
88
        });
89
90
        $this->app->singleton(Utils\Cache::class, function () {
91
            return new Utils\Cache();
92
        });
93
94
        $this->app->singleton(Utils\CacheTag::class, function () {
95
            return new Utils\CacheTag();
96
        });
97
98
        $this->app->singleton(Utils\WidgetRenderer::class, function () {
99
            return new Utils\WidgetRenderer();
100
        });
101
    }
102
103
    private function registerMacros()
104
    {
105
        Route::macro('view', function ($url, $view, $name = null) {
106
            return Route::get($url, [
107
                'as' => $name,
108
                'uses' => function () use ($view) {
109
                    return view($view);
110
                },
111
            ]);
112
        });
113
114
        Route::macro('widget', function ($url, $widget, $name = null) {
115
            return Route::get($url, [
116
                'as' => $name,
117
                'uses' => function (...$args) use ($widget) {
118
                    return render_widget($widget, $args);
119
                },
120
            ]);
121
        });
122
123
        Route::macro('jsonWidget', function ($url, $widget, $name = null) {
124
            return Route::get($url, [
125
                'as' => $name,
126
                'uses' => function (...$args) use ($widget) {
127
                    return json_widget($widget, $args);
128
                },
129
            ]);
130
        });
131
    }
132
}
133