BlogServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Chriscreates\Blog;
4
5
use Illuminate\Contracts\Container\BindingResolutionException;
6
use Illuminate\Events\Dispatcher;
7
use Illuminate\Support\Facades\Route;
8
use Illuminate\Support\ServiceProvider;
9
10
class BlogServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * All of the event / listener mappings.
14
     *
15
     * @var array
16
     */
17
    protected $events = [
18
        \Chriscreates\Blog\Events\PostViewed::class => [
19
            \Chriscreates\Blog\Listeners\StoreViewData::class,
20
        ],
21
    ];
22
23
    /**
24
     * Bootstrap the application services.
25
     */
26
    public function boot()
27
    {
28
        $this->handleEvents();
29
        $this->handleRoutes();
30
        $this->handleMigrations();
31
        $this->handlePublishing();
32
    }
33
34
    /**
35
     * Register bindings in the container.
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        $this->handleConfig();
42
        $this->handleCommands();
43
    }
44
45
    /**
46
     * Register the events and listeners.
47
     *
48
     * @return void
49
     * @throws BindingResolutionException
50
     */
51
    private function handleEvents()
52
    {
53
        $events = $this->app->make(Dispatcher::class);
54
55
        foreach ($this->events as $event => $listeners) {
56
            foreach ($listeners as $listener) {
57
                $events->listen($event, $listener);
58
            }
59
        }
60
    }
61
62
    /**
63
     * Register the package routes.
64
     *
65
     * @return void
66
     */
67
    private function handleRoutes()
68
    {
69
        Route::group($this->routeConfiguration(), function () {
70
            $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
71
        });
72
    }
73
74
    /**
75
     * Get the Blog route group configuration array.
76
     *
77
     * @return array
78
     */
79
    private function routeConfiguration()
80
    {
81
        return [
82
            'namespace' => 'Blog\Http\Controllers',
83
            'prefix' => config('blog.path'),
84
            'middleware' => config('blog.middleware'),
85
        ];
86
    }
87
88
    /**
89
     * Register the package's migrations.
90
     *
91
     * @return void
92
     */
93
    private function handleMigrations()
94
    {
95
        if ($this->app->runningInConsole()) {
96
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
97
        }
98
    }
99
100
    /**
101
     * Register the package's publishable resources.
102
     *
103
     * @return void
104
     */
105
    private function handlePublishing()
106
    {
107
        if ($this->app->runningInConsole()) {
108
            $this->publishes([
109
                __DIR__.'/../public/' => public_path('vendor/blog'),
110
            ], 'blog-assets');
111
112
            $this->publishes([
113
                __DIR__.'/../resources/views/' => resource_path('views'),
114
            ], 'blog-views');
115
116
            $this->publishes([
117
                __DIR__.'/../database/factories/' => database_path('factories'),
118
            ], 'blog-factories');
119
120
            $this->publishes([
121
                __DIR__.'/../config/blog.php' => config_path('blog.php'),
122
            ], 'blog-config');
123
        }
124
    }
125
126
    /**
127
     * @return void
128
     */
129
    private function handleConfig()
130
    {
131
        $this->mergeConfigFrom(
132
            __DIR__.'/../config/blog.php',
133
            'config'
134
        );
135
    }
136
137
    /**
138
     * @return void
139
     */
140
    private function handleCommands()
141
    {
142
        $this->commands([
143
            \Chriscreates\Blog\Console\Commands\InstallCommand::class,
144
            \Chriscreates\Blog\Console\Commands\SetupCommand::class,
145
        ]);
146
    }
147
}
148