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

Refactor more old code and get tests to pass

This commit is contained in:
Samuel Štancl 2020-05-13 04:51:37 +02:00
parent c5377a16f7
commit c32f229dd5
72 changed files with 425 additions and 531 deletions

View file

@ -1,63 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Features;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains;
use Stancl\Tenancy\Tenancy;
// todo rewrite this
class TelescopeTags implements Feature
{
/** @var callable User-specific callback that returns tags. */
protected $callback;
public function __construct()
{
$this->callback = function ($entry) {
return [];
};
}
public function bootstrap(Tenancy $tenancy): void
{
if (! class_exists(Telescope::class)) {
return;
}
Telescope::tag(function (IncomingEntry $entry) {
$tags = $this->getTags($entry);
if (! request()->route()) {
return $tags;
}
// todo lines below
$tenantRoute = PreventAccessFromTenantDomains::routeHasMiddleware(request()->route(), 'tenancy')
|| PreventAccessFromTenantDomains::routeHasMiddleware(request()->route(), 'universal');
// Don't do anything if we're visiting a universal route on a central domain
if ($tenantRoute && tenancy()->initialized) {
$tags = array_merge($tags, [
'tenant:' . tenant('id'),
]);
}
return $tags;
});
}
public function getTags(IncomingEntry $entry): array
{
return ($this->callback)($entry);
}
public function setCallback(callable $callback)
{
$this->callback = $callback;
}
}

View file

@ -5,12 +5,13 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Features;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Events\RevertedToCentralContext;
use Stancl\Tenancy\Events\TenancyBootstrapped;
use Stancl\Tenancy\Tenancy;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\TenantManager;
use Stancl\Tenancy\Contracts\Tenant;
// todo rewrite this
class TenantConfig implements Feature
{
/** @var Repository */
@ -27,26 +28,26 @@ class TenantConfig implements Feature
{
$this->config = $config;
foreach ($this->getStorageToConfigMap() as $configKey) {
foreach (static::$storageToConfigMap as $configKey) {
$this->originalConfig[$configKey] = $this->config[$configKey];
}
}
public function bootstrap(Tenancy $tenancy): void
{
$tenantManager->eventListener('bootstrapped', function (TenantManager $manager) {
$this->setTenantConfig($manager->getTenant());
Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) {
$this->setTenantConfig($event->tenancy->tenant);
});
$tenantManager->eventListener('ended', function () {
Event::listen(RevertedToCentralContext::class, function () {
$this->unsetTenantConfig();
});
}
public function setTenantConfig(Tenant $tenant): void
{
foreach ($this->getStorageToConfigMap() as $storageKey => $configKey) {
$override = $tenant->data[$storageKey] ?? null;
foreach (static::$storageToConfigMap as $storageKey => $configKey) {
$override = $tenant->$storageKey ?? null;
if (! is_null($override)) {
$this->config[$configKey] = $override;
}
@ -55,13 +56,8 @@ class TenantConfig implements Feature
public function unsetTenantConfig(): void
{
foreach ($this->getStorageToConfigMap() as $configKey) {
foreach (static::$storageToConfigMap as $configKey) {
$this->config[$configKey] = $this->originalConfig[$configKey];
}
}
public function getStorageToConfigMap(): array
{
return static::$storageToConfigMap;
}
}

View file

@ -1,46 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Features;
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Date;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\TenantManager;
// todo rewrite this
class Timestamps implements Feature
{
/** @var Repository */
protected $config;
public static $format = 'c'; // ISO 8601
public function __construct(Repository $config)
{
$this->config = $config;
}
public function bootstrap(TenantManager $tenantManager): void
{
$tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) {
$tenant->with('created_at', $this->now());
$tenant->with('updated_at', $this->now());
});
$tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) {
$tenant->with('updated_at', $this->now());
});
$tenantManager->hook('tenant.softDeleting', function ($tm, Tenant $tenant) {
$tenant->with('deleted_at', $this->now());
});
}
public function now(): string
{
return Date::now()->format(static::$format);
}
}