|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Multitenancy; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Application; |
| 6 | +use Illuminate\Foundation\Bootstrap\LoadConfiguration; |
| 7 | +use Spatie\Multitenancy\Contracts\IsTenant; |
| 8 | +use Spatie\Multitenancy\Tasks\SwitchTenantTask; |
| 9 | + |
| 10 | +class SwitchTenant implements SwitchTenantTask |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Make the given tenant current. |
| 14 | + * |
| 15 | + * @param IsTenant $tenant |
| 16 | + * @return void |
| 17 | + */ |
| 18 | + public function makeCurrent(IsTenant $tenant): void |
| 19 | + { |
| 20 | + $app = app(); |
| 21 | + |
| 22 | + // Set the tenant-specific storage path |
| 23 | + $tenantStoragePath = base_path('storage/tenant_' . $tenant->id); |
| 24 | + |
| 25 | + $app->setStoragePath($tenantStoragePath); |
| 26 | + |
| 27 | + // Create the tenant storage directory if it doesn't exist |
| 28 | + |
| 29 | + // TODO: Move these to somewhere else - should not be run on every request |
| 30 | + if (!file_exists($tenantStoragePath)) { |
| 31 | + mkdir($tenantStoragePath, 0755, true); |
| 32 | + } |
| 33 | + $tennantCacheFolder = base_path('bootstrap/cache/tenant_' . $tenant->id); |
| 34 | + if (!file_exists($tennantCacheFolder)) { |
| 35 | + mkdir($tennantCacheFolder, 0755, true); |
| 36 | + } |
| 37 | + |
| 38 | + // Reload the config |
| 39 | + putenv('APP_CONFIG_CACHE=' . $tennantCacheFolder . '/config.php'); |
| 40 | + (new LoadConfiguration())->bootstrap($app); |
| 41 | + |
| 42 | + // Set the app.url config |
| 43 | + $app->config->set('app.url', $app->config->get('app.protocol') . '://' . $tenant->domain); |
| 44 | + |
| 45 | + if ($tenant->config_overrides) { |
| 46 | + foreach ($tenant->config_overrides as $key => $value) { |
| 47 | + $app->config->set($key, $value); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Forget the current tenant. |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function forgetCurrent(): void |
| 58 | + { |
| 59 | + } |
| 60 | +} |
0 commit comments