MobileFirstServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/laravel-mobile-first
4
 *
5
 * @copyright (c) Vuong Xuong Minh
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace VXM\MobileFirst;
10
11
use Illuminate\Support\ServiceProvider;
12
13
/**
14
 * @author Vuong Minh <[email protected]>
15
 * @since  1.0.0
16
 */
17
class MobileFirstServiceProvider extends ServiceProvider
18
{
19
    /**
20
     * Register package.
21
     */
22
    public function register(): void
23
    {
24
        $this->mergeDefaultConfigs();
25
        $this->registerServices();
26
    }
27
28
    /**
29
     * Merge default package configs to config service.
30
     */
31
    protected function mergeDefaultConfigs(): void
32
    {
33
        $this->mergeConfigFrom(__DIR__.'/../config/mobilefirst.php', 'mobilefirst');
34
    }
35
36
    /**
37
     * Register package services.
38
     */
39
    protected function registerServices(): void
40
    {
41
        $this->app->singleton(MobileRedirect::class, function ($app) {
42
            $config = $app['config']->get('mobilefirst');
43
44
            return new MobileRedirect($config['mobile_url'], $config['keep_url_path'], $config['redirect_status_code'], $config['redirect_methods']);
45
        });
46
47
        $this->app->singleton(ViewComposer::class, function ($app) {
48
            return new ViewComposer($app['agent'], $app['config']->get('mobilefirst.device_sub_dirs'), $app['files']);
49
        });
50
    }
51
52
    /**
53
     * Boot package.
54
     */
55
    public function boot(): void
56
    {
57
        $this->publishConfigs();
58
        $this->registerViewComposeListener();
59
    }
60
61
    /**
62
     * Publish package config files.
63
     */
64
    protected function publishConfigs(): void
65
    {
66
        $this->publishes([
67
            __DIR__.'/../config/mobilefirst.php' => config_path('mobilefirst.php'),
68
        ], 'config');
69
    }
70
71
    /**
72
     * Register view compose listener for auto switch view by end-user device.
73
     */
74
    protected function registerViewComposeListener(): void
75
    {
76
        if ($this->app['config']->get('mobilefirst.auto_switch_view_by_device')) {
77
            $this->app['view']->composer('*', ViewComposer::class);
78
        }
79
    }
80
}
81