TranslatableBootFormsServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Propaganistas\LaravelTranslatableBootForms;
2
3
use Illuminate\Foundation\Application;
4
use Illuminate\Support\ServiceProvider;
5
6
class TranslatableBootFormsServiceProvider extends ServiceProvider
7
{
8
9
    /**
10
     * Indicates if loading of the provider is deferred.
11
     *
12
     * @var bool
13
     */
14
    protected $defer = true;
15
16
    /**
17
     * Boot the application events.
18
     *
19
     * @return void
20
     */
21
    public function boot()
22
    {
23
        $this->publishes([
24
            __DIR__ . '/../config/config.php' => config_path('translatable-bootforms.php'),
25
        ], 'config');
26
    }
27
28
    /**
29
     * Register the service provider.
30
     *
31
     * @return void
32
     */
33
    public function register()
34
    {
35
        $this->mergeConfigFrom(
36
            __DIR__ . '/../config/config.php', 'translatable-bootforms'
37
        );
38
39
        // Override BootForm's form builder in order to get model binding
40
        // between BootForm & TranslatableBootForm working.
41
        $this->app->singleton('adamwathan.form', function ($app) {
42
            $formBuilder = new Form\FormBuilder();
43
            $formBuilder->setLocales($this->getLocales());
44
            $formBuilder->setErrorStore($app['adamwathan.form.errorstore']);
45
            $formBuilder->setOldInputProvider($app['adamwathan.form.oldinput']);
46
47
            $token = version_compare(Application::VERSION, '5.4', '<')
48
                ? $app['session.store']->getToken()
49
                : $app['session.store']->token();
50
51
            $formBuilder->setToken($token);
52
53
            return $formBuilder;
54
        });
55
56
        // Define TranslatableBootForm.
57
        $this->app->singleton('translatable-bootform', function ($app) {
58
            $form = new TranslatableBootForm($app['bootform']);
59
            $form->locales($this->getLocales());
60
61
            return $form;
62
        });
63
    }
64
65
    /**
66
     * Get the services provided by the provider.
67
     *
68
     * @return array
69
     */
70
    public function provides()
71
    {
72
        return [
73
            'adamwathan.form',
74
            'translatable-bootform',
75
        ];
76
    }
77
78
    /**
79
     * Get Translatable's locales.
80
     *
81
     * @return array
82
     */
83
    protected function getLocales()
84
    {
85
        return with(new Translatable\TranslatableWrapper)->getLocales();
86
    }
87
}
88