TenantsServiceProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 85
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 12 3
B boot() 0 40 8
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
        $this->loadRoutesFrom(__DIR__.'/../../routes/web/adminarea.php');
77
        $this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/tenants');
78
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/tenants');
79
        $this->app->runningInConsole() || $this->app->afterResolving('blade.compiler', function () {
80
            $accessarea = $this->app['request']->route('accessarea');
81
            ! file_exists($menus = __DIR__."/../../routes/menus/{$accessarea}.php") || require $menus;
82
            ! file_exists($breadcrumbs = __DIR__."/../../routes/breadcrumbs/{$accessarea}.php") || require $breadcrumbs;
83
        });
84
85
        // Inject tenantable middleware
86
        // before route bindings substitution
87
        $this->app->booted(function () {
88
            $router = $this->app['router'];
89
90
            $pointer = array_search(SubstituteBindings::class, $router->middlewarePriority);
91
            $before = array_slice($router->middlewarePriority, 0, $pointer);
92
            $after = array_slice($router->middlewarePriority, $pointer);
93
94
            $router->middlewarePriority = array_merge($before, [Tenantable::class], $after);
95
            $router->pushMiddlewareToGroup('web', Tenantable::class);
96
        });
97
98
        // Publish Resources
99
        ! $this->app->runningInConsole() || $this->publishesLang('cortex/tenants', true);
100
        ! $this->app->runningInConsole() || $this->publishesViews('cortex/tenants', true);
101
        ! $this->app->runningInConsole() || $this->publishesConfig('cortex/tenants', true);
102
        ! $this->app->runningInConsole() || $this->publishesMigrations('cortex/tenants', true);
103
    }
104
}
105