forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchTenant.php
More file actions
82 lines (67 loc) · 2.46 KB
/
SwitchTenant.php
File metadata and controls
82 lines (67 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace ProcessMaker\Multitenancy;
use Illuminate\Broadcasting\BroadcastManager;
use ProcessMaker\Application;
use ProcessMaker\Multitenancy\Broadcasting\TenantAwareBroadcastManager;
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
use Spatie\Multitenancy\Contracts\IsTenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
class SwitchTenant implements SwitchTenantTask
{
use UsesMultitenancyConfig;
/**
* Make the given tenant current.
*
* @param IsTenant $tenant
* @return void
*/
public function makeCurrent(IsTenant $tenant): void
{
$app = app();
\Log::debug('SwitchTenant: ' . $tenant->id, ['domain' => request()->getHost()]);
// Set the tenant's domain in the request headers. Used for things like the global url() helper.
request()->headers->set('host', $tenant->domain);
$this->overrideConfigs($app, $tenant);
// Extend BroadcastManager to our custom implementation that prefixes the channel names with the tenant id.
$app->extend(BroadcastManager::class, function ($manager, $app) use ($tenant) {
return new TenantAwareBroadcastManager($app, $tenant->id);
});
}
/**
* Forget the current tenant.
*
* @return void
*/
public function forgetCurrent(): void
{
}
private function overrideConfigs(Application $app, IsTenant $tenant)
{
if ($app->configurationIsCached()) {
return;
}
$newConfig = [
'app.instance' => config('app.instance') . '_' . $tenant->id,
];
if (!isset($tenant->config['script-runner-microservice.callback'])) {
$newConfig['script-runner-microservice.callback'] = str_replace(
$tenant->getOriginalValue('APP_URL'),
config('app.url'),
$tenant->getOriginalValue('SCRIPT_MICROSERVICE_CALLBACK')
);
}
if (!isset($tenant->config['app.docker_host_url'])) {
// There is no specific override in the tenant's config so set it to the app url
$newConfig['app.docker_host_url'] = config('app.url');
}
// Set config from the entry in the tenants table
$config = $tenant->config;
foreach ($config as $key => $value) {
if ($key === 'app.key' || $key === 'app.url') {
continue;
}
$newConfig[$key] = $value;
}
config($newConfig);
}
}