1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Tenants\Providers; |
6
|
|
|
|
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use Rinvex\Menus\Facades\Menu; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Illuminate\Support\ServiceProvider; |
11
|
|
|
use Rinvex\Menus\Factories\MenuFactory; |
12
|
|
|
use Rinvex\Tenants\Contracts\TenantContract; |
13
|
|
|
use Cortex\Tenants\Http\Middleware\Tenantable; |
14
|
|
|
use Cortex\Tenants\Console\Commands\SeedCommand; |
15
|
|
|
use Cortex\Tenants\Console\Commands\InstallCommand; |
16
|
|
|
use Cortex\Tenants\Console\Commands\MigrateCommand; |
17
|
|
|
use Cortex\Tenants\Console\Commands\PublishCommand; |
18
|
|
|
use Cortex\Tenants\Console\Commands\RollbackCommand; |
19
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
20
|
|
|
use Illuminate\Routing\Middleware\SubstituteBindings; |
21
|
|
|
use Cortex\Tenants\Overrides\Illuminate\Auth\EloquentUserProvider; |
22
|
|
|
|
23
|
|
|
class TenantsServiceProvider extends ServiceProvider |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The commands to be registered. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $commands = [ |
31
|
|
|
SeedCommand::class => 'command.cortex.tenants.seed', |
32
|
|
|
InstallCommand::class => 'command.cortex.tenants.install', |
33
|
|
|
MigrateCommand::class => 'command.cortex.tenants.migrate', |
34
|
|
|
PublishCommand::class => 'command.cortex.tenants.publish', |
35
|
|
|
RollbackCommand::class => 'command.cortex.tenants.rollback', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Register any application services. |
40
|
|
|
* |
41
|
|
|
* This service provider is a great spot to register your various container |
42
|
|
|
* bindings with the application. As you can see, we are registering our |
43
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function register() |
48
|
|
|
{ |
49
|
|
|
// Merge config |
50
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.tenants'); |
51
|
|
|
|
52
|
|
|
// Register console commands |
53
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Bootstrap any application services. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function boot(Router $router) |
62
|
|
|
{ |
63
|
|
|
// Bind route models and constrains |
64
|
|
|
$router->pattern('tenant', '[a-z0-9-]+'); |
65
|
|
|
$router->model('tenant', TenantContract::class); |
66
|
|
|
|
67
|
|
|
// Map relations |
68
|
|
|
Relation::morphMap([ |
69
|
|
|
'tenant' => config('rinvex.tenants.models.tenant'), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
// Load resources |
73
|
|
|
require __DIR__.'/../../routes/breadcrumbs.php'; |
74
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php'); |
75
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/tenants'); |
76
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/tenants'); |
77
|
|
|
! $this->app->runningInConsole() || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); |
78
|
|
|
$this->app->afterResolving('blade.compiler', function () { |
79
|
|
|
require __DIR__.'/../../routes/menus.php'; |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
// Publish Resources |
83
|
|
|
! $this->app->runningInConsole() || $this->publishResources(); |
84
|
|
|
|
85
|
|
|
// Inject tenantable middleware before route bindings substitution |
86
|
|
|
$pointer = array_search(SubstituteBindings::class, $router->middlewarePriority); |
87
|
|
|
$before = array_slice($router->middlewarePriority, 0, $pointer); |
88
|
|
|
$after = array_slice($router->middlewarePriority, $pointer); |
89
|
|
|
|
90
|
|
|
$router->middlewarePriority = array_merge($before, [Tenantable::class], $after); |
91
|
|
|
$router->pushMiddlewareToGroup('web', Tenantable::class); |
92
|
|
|
|
93
|
|
|
// Override EloquentUserProvider to remove tenantable |
94
|
|
|
// global scope when retrieving authenticated user instance |
95
|
|
|
Auth::provider('eloquent', function ($app, array $config) { |
96
|
|
|
return new EloquentUserProvider($app['hash'], $config['model']); |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
// Override fort controllers |
100
|
|
|
$this->app->singleton(\Cortex\Fort\Http\Controllers\Frontarea\RegistrationController::class, \Cortex\Tenants\Http\Controllers\Frontarea\RegistrationController::class); |
|
|
|
|
101
|
|
|
|
102
|
|
|
// Register menus |
103
|
|
|
$this->registerMenus(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Register menus. |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected function registerMenus() |
112
|
|
|
{ |
113
|
|
|
Menu::make('tenantarea.header', function (MenuFactory $menu) { |
|
|
|
|
114
|
|
|
}); |
115
|
|
|
Menu::make('managerarea.header', function (MenuFactory $menu) { |
|
|
|
|
116
|
|
|
}); |
117
|
|
|
Menu::make('managerarea.sidebar', function (MenuFactory $menu) { |
|
|
|
|
118
|
|
|
}); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Publish resources. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function publishResources() |
127
|
|
|
{ |
128
|
|
|
$this->publishes([realpath(__DIR__.'/../../config/config.php') => config_path('cortex.tenants.php')], 'cortex-tenants-config'); |
|
|
|
|
129
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/lang') => resource_path('lang/vendor/cortex/tenants')], 'cortex-tenants-lang'); |
|
|
|
|
130
|
|
|
$this->publishes([realpath(__DIR__.'/../../resources/views') => resource_path('views/vendor/cortex/tenants')], 'cortex-tenants-views'); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Register console commands. |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
protected function registerCommands() |
139
|
|
|
{ |
140
|
|
|
// Register artisan commands |
141
|
|
|
foreach ($this->commands as $key => $value) { |
142
|
|
|
$this->app->singleton($value, function ($app) use ($key) { |
|
|
|
|
143
|
|
|
return new $key(); |
144
|
|
|
}); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->commands(array_values($this->commands)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.