mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Add 'Features'
This commit is contained in:
parent
8c69d7847e
commit
61739dc5fc
5 changed files with 82 additions and 40 deletions
12
src/Contracts/Feature.php
Normal file
12
src/Contracts/Feature.php
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Contracts;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
|
/** Additional features, like Telescope tags and tenant redirects. */
|
||||||
|
interface Feature
|
||||||
|
{
|
||||||
|
// todo is the tenantManager argument necessary?
|
||||||
|
public function bootstrap(TenantManager $tenantManager): void;
|
||||||
|
}
|
||||||
38
src/Features/TelescopeTags.php
Normal file
38
src/Features/TelescopeTags.php
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Features;
|
||||||
|
|
||||||
|
use Laravel\Telescope\Telescope;
|
||||||
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
use Laravel\Telescope\IncomingEntry;
|
||||||
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
|
|
||||||
|
class TelescopeTags implements Feature
|
||||||
|
{
|
||||||
|
public function bootstrap(TenantManager $tenantManager): void
|
||||||
|
{
|
||||||
|
if (! class_exists(Telescope::class)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Telescope::tag(function (IncomingEntry $entry) {
|
||||||
|
$tags = $this->getTags($entry);
|
||||||
|
|
||||||
|
if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) {
|
||||||
|
$tags = array_merge($tags, [
|
||||||
|
'tenant:' . tenant('uuid'),
|
||||||
|
'domain:' . tenant('domain'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tags;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTags(IncomingEntry $entry): array
|
||||||
|
{
|
||||||
|
return array_reduce($this->callbacks, function ($tags, $listener) use($entry) {
|
||||||
|
return array_merge($tags, $listener($entry));
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/Features/TenantRedirect.php
Normal file
20
src/Features/TenantRedirect.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
|
|
||||||
|
class TenantRedirect implements Feature
|
||||||
|
{
|
||||||
|
public function bootstrap(TenantManager $tenantManager): void
|
||||||
|
{
|
||||||
|
RedirectResponse::macro('tenant', function (string $domain) {
|
||||||
|
// replace first occurance of hostname fragment with $domain
|
||||||
|
$url = $this->getTargetUrl();
|
||||||
|
$hostname = parse_url($url, PHP_URL_HOST);
|
||||||
|
$position = strpos($url, $hostname);
|
||||||
|
$this->setTargetUrl(substr_replace($url, $domain, $position, strlen($hostname)));
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Laravel\Telescope\Telescope;
|
|
||||||
use Stancl\Tenancy\Commands\Run;
|
use Stancl\Tenancy\Commands\Run;
|
||||||
use Stancl\Tenancy\Commands\Seed;
|
use Stancl\Tenancy\Commands\Seed;
|
||||||
use Illuminate\Cache\CacheManager;
|
use Illuminate\Cache\CacheManager;
|
||||||
|
|
@ -50,43 +49,7 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->app->register(TenantRouteServiceProvider::class);
|
$this->app->register(TenantRouteServiceProvider::class);
|
||||||
|
|
||||||
if (\class_exists(Telescope::class)) {
|
|
||||||
$this->setTelescopeTags();
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->registerTenantRedirectMacro();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTelescopeTags()
|
|
||||||
{
|
|
||||||
Telescope::tag(function (\Laravel\Telescope\IncomingEntry $entry) {
|
|
||||||
$tags = $this->app->make(TenantManager::class)->integration('telescope', $entry);
|
|
||||||
|
|
||||||
if (\in_array('tenancy', optional(request()->route())->middleware() ?? [])) {
|
|
||||||
$tags = \array_merge($tags, [
|
|
||||||
'tenant:' . tenant('uuid'),
|
|
||||||
'domain:' . tenant('domain'),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $tags;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function registerTenantRedirectMacro()
|
|
||||||
{
|
|
||||||
RedirectResponse::macro('tenant', function (string $domain) {
|
|
||||||
// replace first occurance of hostname fragment with $domain
|
|
||||||
$url = $this->getTargetUrl();
|
|
||||||
$hostname = \parse_url($url, PHP_URL_HOST);
|
|
||||||
$position = \strpos($url, $hostname);
|
|
||||||
$this->setTargetUrl(\substr_replace($url, $domain, $position, \strlen($hostname)));
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register services.
|
* Register services.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
|
|
||||||
// todo rethink integration events
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||||
*/
|
*/
|
||||||
|
|
@ -34,6 +32,8 @@ class TenantManagerv2
|
||||||
{
|
{
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
$this->storage = $storage;
|
$this->storage = $storage;
|
||||||
|
|
||||||
|
$this->bootstrapFeatures();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTenant(Tenant $tenant): self
|
public function createTenant(Tenant $tenant): self
|
||||||
|
|
@ -128,6 +128,15 @@ class TenantManagerv2
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function bootstrapFeatures(): self
|
||||||
|
{
|
||||||
|
foreach ($this->app['config']['tenancy.features'] as $feature) {
|
||||||
|
$this->app[$feature]->bootstrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a list of TenancyBoostrappers.
|
* Return a list of TenancyBoostrappers.
|
||||||
*
|
*
|
||||||
|
|
@ -136,7 +145,7 @@ class TenantManagerv2
|
||||||
*/
|
*/
|
||||||
public function tenancyBootstrappers($except = []): array
|
public function tenancyBootstrappers($except = []): array
|
||||||
{
|
{
|
||||||
return array_key_diff(config('tenancy.bootstrappers'), $except);
|
return array_key_diff($this->app['config']['tenancy.bootstrappers'], $except);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue