FormBuilderServiceProvider::alias()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Distilleries\FormBuilder;
4
5
use Collective\Html\FormBuilder as LaravelForm;
6
use Collective\Html\HtmlBuilder as LaravelHtml;
7
use Illuminate\Foundation\AliasLoader;
8
use Kris\LaravelFormBuilder\FormBuilder;
9
use Kris\LaravelFormBuilder\FormBuilderServiceProvider as BaseFormBuilderServiceProvider;
10
use Kris\LaravelFormBuilder\FormHelper;
11
12
class FormBuilderServiceProvider extends BaseFormBuilderServiceProvider
13
{
14 86
    public function boot()
15
    {
16 86
        parent::boot();
17
18 86
        $this->loadViewsFrom(__DIR__ . '/../../views', 'form-builder');
19 86
        $this->loadTranslationsFrom(__DIR__ . '/../../lang', 'form-builder');
20
21 86
        $this->publishes([__DIR__ . '/../../config/config.php' => config_path('form-builder.php')]);
22 86
        $this->publishes([__DIR__ . '/../../views' => base_path('resources/views/vendor/form-builder')], 'views');
23
24 86
        $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'form-builder');
25 86
        $this->registerCloudinaryConfig();
26
    }
27
28 86
    public function register()
29
    {
30 86
        $this->commands(\Kris\LaravelFormBuilder\Console\FormMakeCommand::class);
31
32 86
        $this->registerHtmlIfNeeded();
33 86
        $this->registerFormIfHeeded();
34
35 86
        $this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'laravel-form-builder');
36
37 86
        $this->registerFormHelper();
38
39
        $this->app->singleton('laravel-form-builder', function ($app) {
40 34
            return new FormBuilder($app, $app['laravel-form-helper']);
41 86
        });
42
43 86
        $this->commands(\Distilleries\FormBuilder\Console\FormMakeCommand::class);
44
45 86
        $this->alias();
46
    }
47
48 86
    protected function registerFormHelper()
49
    {
50
        $this->app->singleton('laravel-form-helper', function ($app) {
51 74
            $config = $app['config']->get('form-builder');
52 74
            return new FormHelper($app['view'], $app['request'], $config);
53 86
        });
54
55 86
        $this->app->alias('laravel-form-helper', 'Kris\LaravelFormBuilder\FormHelper');
56
    }
57
58 86
    private function registerHtmlIfNeeded()
59
    {
60 86
        if (! $this->app->offsetExists('html')) {
61
            $this->app->singleton('html', function ($app) {
62 42
                return new LaravelHtml($app['url'], $app['view']);
63 86
            });
64
65 86
            $this->registerAliasIfNotExists('Html', \Collective\Html\HtmlFacade::class);
66
        }
67
    }
68
69 86
    private function registerFormIfHeeded()
70
    {
71 86
        if (! $this->app->offsetExists('form')) {
72
            $this->app->singleton('form', function ($app) {
73 42
                $form = new LaravelForm($app['html'], $app['url'], $app['view'], $app['session.store']->token());
74 42
                return $form->setSessionStore($app['session.store']);
75 86
            });
76
77 86
            $this->registerAliasIfNotExists('Form', \Collective\Html\FormFacade::class);
78
        }
79
    }
80
81 86
    private function alias()
82
    {
83 86
        $this->registerAliasIfNotExists('FormBuilder', Facades\FormBuilder::class);
84 86
        $this->registerAliasIfNotExists('Request', \Illuminate\Support\Facades\Request::class);
85 86
        $this->registerAliasIfNotExists('Route', \Illuminate\Support\Facades\Route::class);
86 86
        $this->registerAliasIfNotExists('File', \Illuminate\Support\Facades\File::class);
87 86
        $this->registerAliasIfNotExists('Redirect', \Illuminate\Support\Facades\Redirect::class);
88
    }
89
90 86
    private function registerAliasIfNotExists($alias, $class)
91
    {
92 86
        if (! array_key_exists($alias, AliasLoader::getInstance()->getAliases())) {
93 2
            AliasLoader::getInstance()->alias($alias, $class);
94
        }
95
    }
96
97 86
    protected function registerCloudinaryConfig()
98
    {
99 86
        if (config('form-builder.cloudinary.enabled', false)) {
100 6
            \Cloudinary::config([
101 6
                'cloud_name' => config('form-builder.cloudinary.cloud_name'),
102 6
                'api_key'    => config('form-builder.cloudinary.api_key'),
103 6
                'api_secret' => config('form-builder.cloudinary.api_secret'),
104
            ]);
105
        }
106
    }
107
}
108