Completed
Push — master ( a19a47...15bbc5 )
by Arjay
15:59
created

CoreServiceProvider::boot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Arrilot\Widgets\ServiceProvider as ArrilotWidgetServiceProvider;
6
use Barryvdh\Debugbar\ServiceProvider as DebugbarServiceProvider;
7
use Barryvdh\Snappy\ServiceProvider as SnappyServiceProvider;
8
use Baum\Providers\BaumServiceProvider;
9
use Caffeinated\Menus\MenusServiceProvider;
10
use Conner\Tagging\Providers\TaggingServiceProvider;
11
use Yajra\Breadcrumbs\ServiceProvider as BreadcrumbsServiceProvider;
12
use Illuminate\Foundation\AliasLoader;
13
use Illuminate\Support\ServiceProvider;
14
use Illuminate\View\Compilers\BladeCompiler;
15
use Laracasts\Flash\FlashServiceProvider;
16
use Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider;
17
use Spatie\Backup\BackupServiceProvider;
18
use Spatie\EloquentSortable\SortableServiceProvider;
19
use Spatie\Fractal\FractalServiceProvider;
20
use Yajra\Acl\AclServiceProvider;
21
use Yajra\CMS\Themes\ThemesServiceProvider;
22
use Yajra\CMS\View\Directives\PageHeaderDirective;
23
use Yajra\CMS\View\Directives\TooltipDirective;
24
use Yajra\Datatables\DatatablesServiceProvider;
25
use Yajra\Datatables\ButtonsServiceProvider;
26
27
class CoreServiceProvider extends ServiceProvider
28
{
29
    /**
30
     * Bootstrap the application services.
31
     *
32
     * @return void
33
     */
34
    public function boot()
35
    {
36
        $this->bootCustomValidations();
37
        $this->bootCustomBladeDirectives();
38
39
        $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'cms');
40
41
        /** @var \Illuminate\View\Factory $view */
42
        $view = $this->app['view'];
43
        $view->addLocation(__DIR__ . '/../resources/views');
44
45
        $this->publishes([
46
            __DIR__ . '/../resources/lang' => resource_path('lang/vendor/cms'),
47
        ]);
48
49
        if (config('app.debug') && config('app.debugbar')) {
50
            $this->app->registerDeferredProvider(DebugbarServiceProvider::class);
51
        }
52
    }
53
54
    /**
55
     * Boot custom app validations.
56
     */
57
    protected function bootCustomValidations()
58
    {
59
        $this->app['validator']->extend('view_exists', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
            return view()->exists($value);
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
61
        });
62
63
        $this->app['validator']->extend('slug', function ($attribute, $value) {
64
            return preg_match('/^[\pL\.\-\_\d+]+$/u', $value);
65
        });
66
67
        $this->app['validator']->extend('json', function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
            json_decode($value);
69
70
            return (json_last_error() == JSON_ERROR_NONE);
71
        });
72
    }
73
74
    /**
75
     * Boot custom blade directives.
76
     */
77
    protected function bootCustomBladeDirectives()
78
    {
79
        /** @var BladeCompiler $blade */
80
        $blade = $this->app['blade.compiler'];
81
        $blade->directive('tooltip', function ($expression) {
82
            return "<?php echo app('Yajra\\CMS\\View\\Directives\\TooltipDirective')->handle({$expression}); ?>";
83
        });
84
85
        $blade->directive('pageHeader', function ($expression) {
86
            return "<?php echo app('Yajra\\CMS\\View\\Directives\\PageHeaderDirective')->handle({$expression}); ?>";
87
        });
88
    }
89
90
    /**
91
     * Register the application services.
92
     *
93
     * @return void
94
     */
95
    public function register()
96
    {
97
        $this->registerProviders();
98
        $this->registerBindings();
99
        $this->registerAliases();
100
        $this->registerMigrations();
101
    }
102
103
    /**
104
     * Registered required administrator providers.
105
     */
106
    protected function registerProviders()
107
    {
108
        $this->app->register(ConfigurationServiceProvider::class);
109
        $this->app->register(ThemesServiceProvider::class);
110
        $this->app->register(RouteServiceProvider::class);
111
        $this->app->register(ViewComposerServiceProvider::class);
112
        $this->app->register(AclServiceProvider::class);
113
        $this->app->register(MenusServiceProvider::class);
114
        $this->app->register(BreadcrumbsServiceProvider::class);
115
        $this->app->register(FlashServiceProvider::class);
116
        $this->app->register(FractalServiceProvider::class);
117
        $this->app->register(SnappyServiceProvider::class);
118
        $this->app->register(BaumServiceProvider::class);
119
        $this->app->register(RepositoryServiceProvider::class);
120
        $this->app->register(BackupServiceProvider::class);
121
        $this->app->register(FormServiceProvider::class);
122
        $this->app->register(LaravelLogViewerServiceProvider::class);
123
        $this->app->register(ArrilotWidgetServiceProvider::class);
124
        $this->app->register(WidgetServiceProvider::class);
125
        $this->app->register(DatatablesServiceProvider::class);
126
        $this->app->register(ButtonsServiceProvider::class);
127
        $this->app->register(SortableServiceProvider::class);
128
        $this->app->register(TaggingServiceProvider::class);
129
    }
130
131
    /**
132
     * Register IOC bindings.
133
     */
134
    protected function registerBindings()
135
    {
136
        $this->app->singleton(PageHeaderDirective::class, PageHeaderDirective::class);
137
        $this->app->singleton(TooltipDirective::class, TooltipDirective::class);
138
    }
139
140
    /**
141
     * Register application aliases.
142
     */
143
    protected function registerAliases()
144
    {
145
        $loader = AliasLoader::getInstance();
146
        $loader->alias('Breadcrumbs', \Yajra\Breadcrumbs\Facade::class);
147
        $loader->alias('Form', \Collective\Html\FormFacade::class);
148
        $loader->alias('Html', \Collective\Html\HtmlFacade::class);
149
        $loader->alias('Excel', \Maatwebsite\Excel\Facades\Excel::class);
150
        $loader->alias('PDF', \Barryvdh\Snappy\Facades\SnappyPdf::class);
151
        $loader->alias('Image', \Barryvdh\Snappy\Facades\SnappyImage::class);
152
        $loader->alias('Widget', \Arrilot\Widgets\Facade::class);
153
        $loader->alias('AsyncWidget', \Arrilot\Widgets\AsyncFacade::class);
154
        $loader->alias('Module', \Pingpong\Modules\Facades\Module::class);
155
    }
156
157
    /**
158
     * Register core migration files.
159
     */
160
    protected function registerMigrations()
161
    {
162
        $this->loadMigrationsFrom(__DIR__ . '/../../migrations');
163
    }
164
}
165