Completed
Push — master ( c72a1f...5e0e31 )
by Christopher
01:10
created

BlogServiceProvider::handleMigrations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
        // TODO
82
        return [
83
            'namespace' => 'Blog\Http\Controllers',
84
            'prefix' => config('blog.path'),
85
            'middleware' => config('blog.middleware'),
86
        ];
87
    }
88
89
    /**
90
     * Register the package's migrations.
91
     *
92
     * @return void
93
     */
94
    private function handleMigrations()
95
    {
96
        if ($this->app->runningInConsole()) {
97
            $this->loadMigrationsFrom(__DIR__.'/../database/migrations/');
98
        }
99
    }
100
101
    /**
102
     * Register the package's publishable resources.
103
     *
104
     * @return void
105
     */
106
    private function handlePublishing()
107
    {
108
        if ($this->app->runningInConsole()) {
109
            $this->publishes([
110
                __DIR__.'/../database/factories/' => database_path('factories'),
111
            ], 'blog-factories');
112
113
            $this->publishes([
114
                __DIR__.'/../database/seeds/' => database_path('seeds'),
115
            ], 'blog-seeders');
116
117
            $this->publishes([
118
                __DIR__.'/../config/blogs.php' => config_path('blogs.php'),
119
            ], 'blog-config');
120
        }
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    private function handleConfig()
127
    {
128
        $this->mergeConfigFrom(
129
            __DIR__.'/../config/blog.php',
130
            'config'
131
        );
132
    }
133
134
    /**
135
     * @return void
136
     */
137
    private function handleCommands()
138
    {
139
        $this->commands([
140
            \Chriscreates\Blog\Console\Commands\InstallCommand::class,
141
            \Chriscreates\Blog\Console\Commands\PublishCommand::class,
142
            \Chriscreates\Blog\Console\Commands\SetupCommand::class,
143
        ]);
144
    }
145
}
146