Multitenanted Context

In your application you'll have code that runs both inside and outside the multitenanted context. When outside, you don't want multitenanted code running, right?

Introduction

Within Sprout, there's a lot of functionality that requires there to be a current tenant, or at the very least, a current tenancy. This code is often hooked up to be called automatically in particular situations. These situations, however, are not unique to the multitenanted part of your application, and will most likely occur within the parts that do not have a tenant or tenancy.

This presents an interesting problem because you want the multitenanted code to error if there isn't a tenancy or tenant, but only when dealing with multitenancy. This is where the "multitenanted context" comes in.

How it works

This whole subset of functionality is remarkably simple, and comes down to three methods on the core Sprout\Sprout class, which either read or write a single boolean.

1public function markAsInContext(): self;
2 
3public function markAsOutsideContext(): self;
4 
5public function withinContext(): bool;

Because the Sprout\Sprout class is registered as a singleton with Laravel's container, and is instantiated during the application boot process (inside Sprouts' service provider), the class can be used to keep track of "state".

There's only a single place in the entirety of Sprout that sets the current multitenanted context, and it's when the current tenancy is set. During this process the application is marked as being within multitenanted context using the markAsInContext() method. If you wish to check the context within your own code, you can use withinContext() through the core Sprout\Sprout class, the helper or the facade.

1// From the Sprout core class
2app(Sprout\Sprout::class)->withinContext();
3 
4// From the Sprout helper
5sprout()->withinContext();
6 
7// From the facade
8Sprout\Facades\Sprout::withinContext()

The markAsOutsideContext() and markAsInContext() methods should only be used if you fully understand what you're doing, and are prepared to deal with the possible side effects.