mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 02:04:03 +00:00
Add 'Features'
This commit is contained in:
parent
8c69d7847e
commit
61739dc5fc
5 changed files with 82 additions and 40 deletions
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue