Completed
Push — master ( 914022...6be129 )
by Abdelrahman
09:58 queued 08:52
created

TenantsServiceProvider::boot()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.7537
c 0
b 0
f 0
cc 6
nc 32
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Providers;
6
7
use Illuminate\Routing\Router;
8
use Cortex\Tenants\Models\Tenant;
9
use Illuminate\Support\ServiceProvider;
10
use Rinvex\Support\Traits\ConsoleTools;
11
use Cortex\Tenants\Http\Middleware\Tenantable;
12
use Cortex\Tenants\Console\Commands\SeedCommand;
13
use Cortex\Tenants\Console\Commands\InstallCommand;
14
use Cortex\Tenants\Console\Commands\MigrateCommand;
15
use Cortex\Tenants\Console\Commands\PublishCommand;
16
use Cortex\Tenants\Console\Commands\RollbackCommand;
17
use Illuminate\Database\Eloquent\Relations\Relation;
18
use Illuminate\Routing\Middleware\SubstituteBindings;
19
20
class TenantsServiceProvider extends ServiceProvider
21
{
22
    use ConsoleTools;
23
24
    /**
25
     * The commands to be registered.
26
     *
27
     * @var array
28
     */
29
    protected $commands = [
30
        SeedCommand::class => 'command.cortex.tenants.seed',
31
        InstallCommand::class => 'command.cortex.tenants.install',
32
        MigrateCommand::class => 'command.cortex.tenants.migrate',
33
        PublishCommand::class => 'command.cortex.tenants.publish',
34
        RollbackCommand::class => 'command.cortex.tenants.rollback',
35
    ];
36
37
    /**
38
     * Register any application services.
39
     *
40
     * This service provider is a great spot to register your various container
41
     * bindings with the application. As you can see, we are registering our
42
     * "Registrar" implementation here. You can add your own bindings too!
43
     *
44
     * @return void
45
     */
46
    public function register(): void
47
    {
48
        // Merge config
49
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.tenants');
50
51
        // Bind eloquent models to IoC container
52
        $this->app['config']['rinvex.tenants.models.tenant'] === Tenant::class
53
        || $this->app->alias('rinvex.tenants.tenant', Tenant::class);
54
55
        // Register console commands
56
        ! $this->app->runningInConsole() || $this->registerCommands();
57
    }
58
59
    /**
60
     * Bootstrap any application services.
61
     *
62
     * @return void
63
     */
64
    public function boot(Router $router): void
65
    {
66
        // Bind route models and constrains
67
        $router->pattern('tenant', '[a-zA-Z0-9-]+');
68
        $router->model('tenant', config('rinvex.tenants.models.tenant'));
69
70
        // Map relations
71
        Relation::morphMap([
72
            'tenant' => config('rinvex.tenants.models.tenant'),
73
        ]);
74
75
        // Load resources
76
        require __DIR__.'/../../routes/breadcrumbs/adminarea.php';
77
        $this->loadRoutesFrom(__DIR__.'/../../routes/web/adminarea.php');
78
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/tenants');
79
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/tenants');
80
        $this->app->runningInConsole() || $this->app->afterResolving('blade.compiler', function () {
81
            require __DIR__.'/../../routes/menus/adminarea.php';
82
        });
83
84
        // Inject tenantable middleware before route bindings substitution
85
        $pointer = array_search(SubstituteBindings::class, $router->middlewarePriority);
86
        $before = array_slice($router->middlewarePriority, 0, $pointer);
87
        $after = array_slice($router->middlewarePriority, $pointer);
88
89
        $router->middlewarePriority = array_merge($before, [Tenantable::class], $after);
90
        $router->pushMiddlewareToGroup('web', Tenantable::class);
91
92
        // Publish Resources
93
        ! $this->app->runningInConsole() || $this->publishesLang('cortex/tenants');
94
        ! $this->app->runningInConsole() || $this->publishesViews('cortex/tenants');
95
        ! $this->app->runningInConsole() || $this->publishesConfig('cortex/tenants');
96
        ! $this->app->runningInConsole() || $this->publishesMigrations('cortex/tenants');
97
    }
98
}
99