Completed
Push — master ( 3710bb...90cc04 )
by Maxime
09:06
created

FormBuilderServiceProvider::aliasExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace Distilleries\FormBuilder;
4
5
use Illuminate\Foundation\AliasLoader;
6
use Kris\LaravelFormBuilder\FormHelper;
7
use Kris\LaravelFormBuilder\FormBuilder;
8
use Collective\Html\FormBuilder as LaravelForm;
9
use Collective\Html\HtmlBuilder as LaravelHtml;
10
use Kris\LaravelFormBuilder\FormBuilderServiceProvider as BaseFormBuilderServiceProvider;
11
12
class FormBuilderServiceProvider extends BaseFormBuilderServiceProvider
13
{
14
    public function boot()
15
    {
16 164
        parent::boot();
17
18 164
        $this->loadViewsFrom(__DIR__ . '/../../views', 'form-builder');
19
        $this->loadTranslationsFrom(__DIR__ . '/../../lang', 'form-builder');
20 164
21 164
        $this->publishes([__DIR__ . '/../../config/config.php' => config_path('form-builder.php')]);
22
        $this->publishes([__DIR__ . '/../../views' => base_path('resources/views/vendor/form-builder')], 'views');
23 164
24 164
        $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'form-builder');
25
    }
26 164
27
    public function register()
28 164
    {
29
        $this->commands(\Kris\LaravelFormBuilder\Console\FormMakeCommand::class);
30
31
        $this->registerHtmlIfNeeded();
32 64
        $this->registerFormIfHeeded();
33 164
34
        $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'laravel-form-builder');
35 164
36
        $this->registerFormHelper();
37 164
38 164
        $this->app->singleton('laravel-form-builder', function ($app) {
39
            return new FormBuilder($app, $app['laravel-form-helper']);
40 164
        });
41
42
        $this->commands(\Distilleries\FormBuilder\Console\FormMakeCommand::class);
43
44
        $this->alias();
45 144
    }
46
47 144
    protected function registerFormHelper()
48 164
    {
49
        $this->app->singleton('laravel-form-helper', function ($app) {
50 164
            $config = $app['config']->get('form-builder');
51 164
            return new FormHelper($app['view'], $app['request'], $config);
52
        });
53
54 164
        $this->app->alias('laravel-form-helper', 'Kris\LaravelFormBuilder\FormHelper');
55
    }
56
57 164
    private function registerHtmlIfNeeded()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
58
    {
59 164
        if (! $this->app->offsetExists('html')) {
60 164
            $this->app->singleton('html', function ($app) {
61
                return new LaravelHtml($app['url'], $app['view']);
62 164
            });
63 164
64 164
            $this->registerAliasIfNotExists('Html', \Collective\Html\HtmlFacade::class);
65 164
        }
66 164
    }
67 164
68
    private function registerFormIfHeeded()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
69
    {
70 164
        if (! $this->app->offsetExists('form')) {
71 164
            $this->app->singleton('form', function ($app) {
72
                $form = new LaravelForm($app['html'], $app['url'], $app['view'], $app['session.store']->getToken());
73 164
                return $form->setSessionStore($app['session.store']);
74 164
            });
75
76
            $this->registerAliasIfNotExists('Form', \Collective\Html\FormFacade::class);
77
        }
78
    }
79
80 164
    private function alias()
81
    {
82 164
        $this->registerAliasIfNotExists('FormBuilder', Facades\FormBuilder::class);
83
        $this->registerAliasIfNotExists('Request', \Illuminate\Support\Facades\Request::class);
84
        $this->registerAliasIfNotExists('Route', \Illuminate\Support\Facades\Route::class);
85
        $this->registerAliasIfNotExists('File', \Illuminate\Support\Facades\File::class);
86 84
        $this->registerAliasIfNotExists('Redirect', \Illuminate\Support\Facades\Redirect::class);
87
    }
88 84
89 164
    private function registerAliasIfNotExists($alias, $class)
90
    {
91 164
        if (! array_key_exists($alias, AliasLoader::getInstance()->getAliases())) {
92
            AliasLoader::getInstance()->alias($alias, $class);
93 4
        }
94 4
    }
95
}
96