Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions ProcessMaker/Multitenancy/SwitchTenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public function makeCurrent(IsTenant $tenant): void
return new TenantAwareBroadcastManager($app, $tenant->id);
});

// Setup tenant-specific route cache. This is needed for plugins.
if ($app->routesAreCached()) {
Env::getRepository()->set('APP_ROUTES_CACHE', storage_path('routes-v7.php'));
}
// Configure the tenant-specific route cache before Laravel checks whether it
// exists. This lets `TENANT=<id> php artisan route:cache` create the cache
// for a tenant that does not have one yet.
Env::getRepository()->set('APP_ROUTES_CACHE', storage_path('routes-v7.php'));
$this->reloadRouteCache($app);
}

/**
Expand All @@ -61,6 +62,7 @@ public function forgetCurrent(): void
{
$app = app();
$app->useStoragePath(base_path('storage'));
Env::getRepository()->clear('APP_ROUTES_CACHE');

$this->setConfig('logging.channels.daily.path', storage_path('logs/processmaker.log'));
$app->make('log')->reset();
Expand Down Expand Up @@ -90,6 +92,29 @@ private function setEnvironmentVariable($key, $value)
$_ENV[$key] = $value;
}

/**
* Reload the tenant's cached routes when the router can persist in memory.
*
* Octane keeps the router in memory between requests. The console and test
* environments may also initialize the router before a tenant is made
* current. Updating APP_ROUTES_CACHE alone is insufficient in those cases.
*/
private function reloadRouteCache(Application $app): void
{
if (!$app->routesAreCached() || !$this->shouldReinitializeRouter()) {
return;
}

require $app->getCachedRoutesPath();
}

private function shouldReinitializeRouter(): bool
{
return isset($_SERVER['LARAVEL_OCTANE'])
|| app()->runningInConsole()
|| app()->runningUnitTests();
}

private function overrideConfigs(Application $app, IsTenant $tenant)
{
$this->setEnvironmentVariable('APP_URL', $tenant->config['app.url']);
Expand Down
Loading