Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 17 additions & 3 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public function __construct(Application $app)
if ((! $app->runningInConsole() || $app->runningUnitTests()) &&
$this->installed() &&
config('tenancy.hostname.auto-identification')) {
$this->identifyHostname();
// Identifies the current hostname, sets the binding using the native resolving strategy.
$app->make(CurrentHostname::class);
$this->registerHostnameIdentification();
}
}

Expand All @@ -73,7 +71,23 @@ public function installed(): bool
return $this->installed ?? $this->installed = $isInstalled();
}

/**
* Identify the hostname of the current request, and its tenant with it.
*/
public function identifyHostname()
{
$this->registerHostnameIdentification();

$this->app->make(CurrentHostname::class);
}

/**
* Arrange for the hostname to be identified, once something asks for it.
*
* Identification reads the current request, so it has to wait until there
* is one. Registering the binding is all that can be done up front.
*/
protected function registerHostnameIdentification()
{
$this->app->singleton(CurrentHostname::class, function () {
/** @var Hostname $hostname */
Expand Down
9 changes: 7 additions & 2 deletions src/Middleware/EagerIdentification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class EagerIdentification
{
public function handle(Request $request, Closure $next)
{
if (config('tenancy.hostname.early-identification')) {
app(Environment::class);
if (config('tenancy.hostname.auto-identification') &&
config('tenancy.hostname.early-identification')) {
// Identification reads the request, so it belongs here rather than
// in the environment's constructor. Asking again on every request
// is what keeps a long lived process from serving the tenant it
// identified for the first one.
app(Environment::class)->identifyHostname();
}

return $next($request);
Expand Down
6 changes: 2 additions & 4 deletions src/Providers/TenancyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ public function register()
);


$this->app->booted(function ($app) {
$app->singleton(Environment::class, function ($app) {
return new Environment($app);
});
$this->app->singleton(Environment::class, function ($app) {
return new Environment($app);
});
$this->app->singleton(Contracts\Repositories\HostnameRepository::class, Repositories\HostnameRepository::class);
$this->app->singleton(Contracts\Repositories\WebsiteRepository::class, Repositories\WebsiteRepository::class);
Expand Down
9 changes: 9 additions & 0 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Hyn\Tenancy\Tests;

use Hyn\Tenancy\Environment;
use Hyn\Tenancy\Providers\TenancyProvider;
use Hyn\Tenancy\Providers\WebserverProvider;
use Hyn\Tenancy\Tests\Traits\InteractsWithBuilds;
Expand Down Expand Up @@ -94,6 +95,14 @@ protected function setUp() : void
parent::setUp();

$this->migrateSystem();

// The application boots before this harness creates the schema, which
// no real one does: the environment decided tenancy was not installed.
// Building it again here, rather than leaving it to whoever asks
// first, keeps the request itself looking like a real one.
$this->app->forgetInstance(Environment::class);
$this->app->make(Environment::class);

$this->duringSetUp($this->app);
}

Expand Down