1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 02:04:03 +00:00

Add 'Features'

This commit is contained in:
Samuel Štancl 2019-09-08 11:11:44 +02:00
parent 8c69d7847e
commit 61739dc5fc
5 changed files with 82 additions and 40 deletions

View 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));
}, []);
}
}

View 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;
});
}
}