Completed
Push — master ( 522891...7baf45 )
by Anton
01:22
created

PaketServiceProvider::registerMiddlewareGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Laravel Paket.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Paket;
15
16
use Cog\Contracts\Paket\Job\Repositories\Job as JobRepositoryContract;
17
use Cog\Laravel\Paket\Console\Commands\Setup;
18
use Cog\Laravel\Paket\Job\Events\JobHasBeenCreated;
19
use Cog\Laravel\Paket\Job\Listeners\JobListener;
20
use Cog\Laravel\Paket\Job\Repositories\JobFileRepository;
21
use Cog\Laravel\Paket\Support\Composer;
22
use Illuminate\Filesystem\Filesystem;
23
use Illuminate\Support\Facades\Config;
24
use Illuminate\Support\Facades\Event;
25
use Illuminate\Support\Facades\Route;
26
use Illuminate\Support\ServiceProvider;
27
28
final class PaketServiceProvider extends ServiceProvider
29
{
30
    public function register(): void
31
    {
32
        $this->configure();
33
        $this->registerConsoleCommands();
34
    }
35
36
    public function boot(): void
37
    {
38
        $this->registerMiddlewareGroups();
39
        $this->registerPublishes();
40
        $this->registerResources();
41
        $this->registerRoutes();
42
        $this->registerBindings();
43
        $this->registerListeners();
44
    }
45
46
    private function getRouteConfiguration(): array
47
    {
48
        return [
49
            'namespace' => 'Cog\Laravel\Paket\Http\Controllers',
50
            'prefix' => Config::get('paket.base_uri'),
51
            'middleware' => 'paket',
52
        ];
53
    }
54
55
    /**
56
     * Merge Paket configuration with the application configuration.
57
     *
58
     * @return void
59
     */
60
    private function configure(): void
61
    {
62
        if (!$this->app->configurationIsCached()) {
63
            $this->mergeConfigFrom(__DIR__ . '/../config/paket.php', 'paket');
64
        }
65
    }
66
67
    private function registerMiddlewareGroups(): void
68
    {
69
        Route::middlewareGroup('paket', Config::get('paket.middlewares', []));
70
    }
71
72
    private function registerResources(): void
73
    {
74
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'paket');
75
    }
76
77
    private function registerPublishes(): void
78
    {
79
        if ($this->app->runningInConsole()) {
80
            $this->publishes([
81
                __DIR__ . '/../public' => public_path('vendor/paket'),
82
            ], 'paket-assets');
83
84
            $this->publishes([
85
                __DIR__ . '/../config/paket.php' => config_path('paket.php'),
86
            ], 'paket-config');
87
        }
88
    }
89
90
    private function registerRoutes(): void
91
    {
92
        Route::group($this->getRouteConfiguration(), function () {
93
            $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');
94
        });
95
    }
96
97
    private function registerConsoleCommands(): void
98
    {
99
        $this->commands([
100
            Setup::class,
101
        ]);
102
    }
103
104
    private function registerBindings(): void
105
    {
106
        $this->app->singleton(Composer::class, function () {
107
            return new Composer(
108
                $this->app->make(Filesystem::class),
109
                base_path(),
110
                storage_path('paket/jobs')
111
            );
112
        });
113
114
        $this->app->singleton(JobRepositoryContract::class, function () {
115
            return new JobFileRepository(
116
                $this->app->make(Filesystem::class),
117
                storage_path('paket')
118
            );
119
        });
120
    }
121
122
    private function registerListeners(): void
123
    {
124
        Event::listen(JobHasBeenCreated::class, JobListener::class);
125
    }
126
}
127