Completed
Push — master ( 4f0406...3f0c83 )
by Iman
107:14 queued 51:28
created

WidgetsServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
B register() 0 37 1
A _defineDirectives() 0 14 3
1
<?php
2
3
namespace Imanghafoori\Widgets;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
use Imanghafoori\Widgets\Utils\Normalizer;
8
use Imanghafoori\Widgets\Utils\Normalizers\CacheNormalizer;
9
use Imanghafoori\Widgets\Utils\Normalizers\TemplateNormalizer;
10
use Imanghafoori\Widgets\Utils\Normalizers\PresenterNormalizer;
11
use Imanghafoori\Widgets\Utils\Normalizers\ControllerNormalizer;
12
13
class WidgetsServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Bootstrap any application services.
17
     *
18
     * @return void
19
     */
20
    public function boot()
21
    {
22
        $omitParenthesis = version_compare($this->app->version(), '5.3', '<');
23
24
        $this->_defineDirectives($omitParenthesis);
25
26
        $this->loadViewsFrom($this->app->basePath().'/app/Widgets/', 'Widgets');
27
    }
28
29
    /**
30
     * Register any application services.
31
     *
32
     * @return void
33
     */
34
    public function register()
35
    {
36
        $this->app->singleton('command.imanghafoori.widget', function ($app) {
37
            return $app['Imanghafoori\Widgets\WidgetGenerator'];
38
        });
39
40
        $this->app->singleton(Normalizer::class, function () {
41
            $cacheNormalizer = new CacheNormalizer();
42
            $templateNormalizer = new TemplateNormalizer();
43
            $presenterNormalizer = new PresenterNormalizer();
44
            $controllerNormalizer = new ControllerNormalizer();
45
46
            return new Utils\Normalizer($templateNormalizer, $cacheNormalizer, $presenterNormalizer, $controllerNormalizer);
47
        });
48
49
        $this->app->singleton(Utils\HtmlMinifier::class, function () {
50
            return new Utils\HtmlMinifier();
51
        });
52
53
        $this->app->singleton(Utils\DebugInfo::class, function () {
54
            return new Utils\DebugInfo();
55
        });
56
57
        $this->app->singleton(Utils\Policies::class, function () {
58
            return new Utils\Policies();
59
        });
60
61
        $this->app->singleton(Utils\Cache::class, function () {
62
            return new Utils\Cache();
63
        });
64
65
        $this->app->singleton(Utils\WidgetRenderer::class, function () {
66
            return new Utils\WidgetRenderer();
67
        });
68
69
        $this->commands('command.imanghafoori.widget');
70
    }
71
72
    /**
73
     * @param $omitParenthesis
74
     */
75
    private function _defineDirectives($omitParenthesis)
76
    {
77
        Blade::directive('render_widget', function ($expression) use ($omitParenthesis) {
78
            $expression = $omitParenthesis ? $expression : "($expression)";
79
80
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
81
        });
82
83
        Blade::directive('widget', function ($expression) use ($omitParenthesis) {
84
            $expression = $omitParenthesis ? $expression : "($expression)";
85
86
            return "<?php echo app(\\Imanghafoori\\Widgets\\Utils\\WidgetRenderer::class)->renderWidget{$expression}; ?>";
87
        });
88
    }
89
}
90