Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Added caching and improved code.
Browse files Browse the repository at this point in the history
  • Loading branch information
MSnoeren committed Oct 29, 2021
1 parent 96e2cf7 commit 36820ab
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
26 changes: 26 additions & 0 deletions config/domain-scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,30 @@
// 'App\Models\User'
],

/*
|--------------------------------------------------------------------------
| Caching
|--------------------------------------------------------------------------
|
| The cache settings define how to cache domain-lookups. Lookups are done
| on every request, which is unnecessary as domains are pretty static data.
|
| ttl
| -----
| The time-to-live for cached domains in seconds. 0 disables the cache.
|
| key
| -----
| {domain} will be replaced by the lowercase variant of the domain, based
| on mode. It will insert the subdomain in sub mode and the full domain
| in full mode. You, as developer, can re-use this key to populate or clear
| the cache in your part of the application.
|
*/

'cache' => [
'ttl' => (int) env('DOMAINSCOPE_CACHE_TTL', 60),
'key' => env('DOMAINSCOPE_CACHE_KEY', 'domain-scope.{domain}'),
],

];
2 changes: 1 addition & 1 deletion src/Http/Middleware/DetectDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DetectDomain
*/
public function handle(Request $request, Closure $next) // phpcs:ignore
{
$domain = $request->getHost();
$domain = strtolower($request->getHost());

/** @var \SnoerenDevelopment\DomainScope\Resolvers\Resolver $resolver */
$resolver = app('domain-scope.resolver');
Expand Down
22 changes: 16 additions & 6 deletions src/Resolvers/FullDomainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,35 @@

namespace SnoerenDevelopment\DomainScope\Resolvers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;

class FullDomainResolver implements Resolver
{
/**
* Resolve the current domain.
*
* @param string $domain The domain.
* @return null|\App\Models\Domain
* @return null|\Illuminate\Database\Eloquent\Model
*/
public function resolve(string $domain) // phpcs:ignore
public function resolve(string $domain): ?Model
{
$model = config('domain-scope.model');

// Check if the domain is on the ignore list.
if (in_array(strtolower($domain), config('domain-scope.ignore'))) {
if (in_array($domain, config('domain-scope.ignore'))) {
return null;
}

return (new $model)
->where('domain', strtolower($domain))
->first();
return Cache::remember(
str_replace('{domain}', $domain, config('domain-scope.cache.key')),
config('domain-scope.cache.ttl'),
function () use ($model, $domain): ?Model {
return (new $model)
->setTable(config('domain-scope.table'))
->where(config('domain-scope.column'), $domain)
->first();
}
);
}
}
6 changes: 4 additions & 2 deletions src/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace SnoerenDevelopment\DomainScope\Resolvers;

use Illuminate\Database\Eloquent\Model;

interface Resolver
{
/**
* Resolve the current domain.
*
* @param string $domain The domain.
* @return null|\App\Models\Domain
* @return null|\Illuminate\Database\Eloquent\Model
*/
public function resolve(string $domain); // phpcs:ignore
public function resolve(string $domain): ?Model;
}
40 changes: 29 additions & 11 deletions src/Resolvers/SubDomainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@

namespace SnoerenDevelopment\DomainScope\Resolvers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;

class SubDomainResolver implements Resolver
{
/**
* Resolve the current domain.
*
* @param string $domain The domain.
* @return null|\App\Models\Domain
* @return null|\Illuminate\Database\Eloquent\Model
*/
public function resolve(string $domain) // phpcs:ignore
public function resolve(string $domain): ?Model
{
$parts = explode('.', $domain);
$model = config('domain-scope.model');

// Check if a developer is developing locally.
if (!app()->environment('production') && count($parts) === 2 && strtolower($parts[1]) === 'localhost') {
if (!app()->environment('production') && count($parts) === 2 && $parts[1] === 'localhost') {
return in_array($parts[0], config('domain-scope.ignore'))
? null
: (new $model)
->setTable(config('domain-scope.table'))
->where(config('domain-scope.column'), strtolower($parts[0]))
->first();
: $this->resolveDomain($model, $parts[0]);
}

// Typically, a domain with a subdomain consists of at least 3 elements.
Expand All @@ -36,9 +36,27 @@ public function resolve(string $domain) // phpcs:ignore
return null;
}

return (new $model)
->setTable(config('domain-scope.table'))
->where(config('domain-scope.column'), strtolower($parts[0]))
->first();
return $this->resolveDomain($model, $parts[0]);
}

/**
* Resolve and cache the domain model.
*
* @param string $model The model classname.
* @param string $domain The domain name.
* @return null|\Illuminate\Database\Eloquent\Model
*/
private function resolveDomain(string $model, string $domain): ?Model
{
return Cache::remember(
str_replace('{domain}', $domain, config('domain-scope.cache.key')),
config('domain-scope.cache.ttl'),
function () use ($model, $domain): ?Model {
return (new $model)
->setTable(config('domain-scope.table'))
->where(config('domain-scope.column'), $domain)
->first();
}
);
}
}

0 comments on commit 36820ab

Please sign in to comment.