Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tenant unaware feature and overall "features" feature improvements #1021

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
36afb7d
wip
abrardev99 Nov 23, 2022
5bbd095
tenantUnwareFeature interface
abrardev99 Nov 23, 2022
a6c0fa2
Update RedirectTest.php
abrardev99 Nov 23, 2022
c0d0dc9
wip
abrardev99 Nov 25, 2022
4c20e30
wip
abrardev99 Nov 25, 2022
399a78c
add test
abrardev99 Nov 25, 2022
9b788e5
Update CrossDomainRedirect.php
abrardev99 Nov 25, 2022
0866663
Fix code style (php-cs-fixer)
Nov 25, 2022
f98a02a
trigger CI
abrardev99 Nov 25, 2022
7b95bec
Delete BootstrapFeatures.php
abrardev99 Nov 25, 2022
7125e1f
Update UniversalRoutes.php
abrardev99 Nov 25, 2022
250f927
test name and other improvements
abrardev99 Nov 28, 2022
be6b2f8
remove `tenant_unaware_features` key and use static function
abrardev99 Nov 28, 2022
5556056
separate TestCase for redirect tests
abrardev99 Nov 30, 2022
bbb8865
function improvements
abrardev99 Nov 30, 2022
068cee8
move only one test to without tenancy folder
abrardev99 Nov 30, 2022
8f9b21d
Update RedirectTest.php
abrardev99 Nov 30, 2022
ec1c2e6
Update RedirectTest.php
abrardev99 Nov 30, 2022
afb942b
enable feature in TestCase
abrardev99 Dec 1, 2022
76c9a05
todo for moving test to separate folder
abrardev99 Dec 1, 2022
2585821
use proper return type
abrardev99 Dec 1, 2022
654a988
Add bootstrapFeatures static property and method
lukinovec Feb 20, 2023
92a4645
Delete setting `tenancy.features` in `getPackageProviders()`
lukinovec Feb 20, 2023
241cf1f
Delete WithoutTenancy directory, move tests
lukinovec Feb 20, 2023
c49f15b
Update RedirectTest
lukinovec Feb 21, 2023
06b62b8
Delete extra line
lukinovec Feb 21, 2023
b056033
Merge branch 'master' of github.com:archtechx/tenancy into unware-fea…
lukinovec Feb 23, 2023
8cbc855
Fix merge errors
lukinovec Feb 23, 2023
a569593
Update ViteBundler
lukinovec Feb 23, 2023
6873c54
Fix code style (php-cs-fixer)
Feb 23, 2023
bcf777d
Fix `$bootstrapFeatures` property
lukinovec Feb 23, 2023
e75aefb
Fix method/property
lukinovec Feb 23, 2023
3e6dd64
Don't access static property as non-static
lukinovec Feb 23, 2023
91f8adb
Bootstrap features manually in tests
lukinovec Feb 23, 2023
34547f7
Always bootstrap TenantConfig
lukinovec Feb 23, 2023
f9e0277
Update ViteBundler constructor
lukinovec Feb 23, 2023
10babdd
Merge branch 'master' into unware-feature
lukinovec Dec 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@
// Stancl\Tenancy\Features\TelescopeTags::class,
// Stancl\Tenancy\Features\UniversalRoutes::class,
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect
],

'tenant_unaware_features' => [
Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect
stancl marked this conversation as resolved.
Show resolved Hide resolved
],

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Contracts/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Stancl\Tenancy\Contracts;

use Stancl\Tenancy\Tenancy;

/** Additional features, like Telescope tags and tenant redirects. */
interface Feature
{
public function bootstrap(Tenancy $tenancy): void;
public function bootstrap(): void;
}
3 changes: 1 addition & 2 deletions src/Features/CrossDomainRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

use Illuminate\Http\RedirectResponse;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenancy;

class CrossDomainRedirect implements Feature
{
public function bootstrap(Tenancy $tenancy): void
public function bootstrap(): void
{
RedirectResponse::macro('domain', function (string $domain) {
/** @var RedirectResponse $this */
Expand Down
3 changes: 1 addition & 2 deletions src/Features/TelescopeTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenancy;

class TelescopeTags implements Feature
{
public function bootstrap(Tenancy $tenancy): void
public function bootstrap(): void
{
if (! class_exists(Telescope::class)) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/Features/TenantConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\RevertedToCentralContext;
use Stancl\Tenancy\Events\TenancyBootstrapped;
use Stancl\Tenancy\Tenancy;

class TenantConfig implements Feature
{
Expand All @@ -28,7 +27,7 @@ public function __construct(
) {
}

public function bootstrap(Tenancy $tenancy): void
public function bootstrap(): void
{
Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) {
/** @var Tenant $tenant */
Expand Down
3 changes: 1 addition & 2 deletions src/Features/UniversalRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Support\Facades\Route as Router;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Middleware;
use Stancl\Tenancy\Tenancy;

class UniversalRoutes implements Feature
{
Expand All @@ -22,7 +21,7 @@ class UniversalRoutes implements Feature
Middleware\InitializeTenancyBySubdomain::class,
];

public function bootstrap(Tenancy $tenancy): void
public function bootstrap(): void
{
foreach (static::$identificationMiddlewares as $middleware) {
$originalOnFail = $middleware::$onFail;
Expand Down
4 changes: 2 additions & 2 deletions src/Features/UserImpersonation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class UserImpersonation implements Feature
/** The lifespan of impersonation tokens (in seconds). */
public static int $ttl = 60;

public function bootstrap(Tenancy $tenancy): void
public function bootstrap(): void
{
$tenancy->macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string $authGuard = null): ImpersonationToken {
Tenancy::macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string $authGuard = null): ImpersonationToken {
return ImpersonationToken::create([
Tenancy::tenantKeyColumn() => $tenant->getTenantKey(),
'user_id' => $userId,
Expand Down
11 changes: 6 additions & 5 deletions src/TenancyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ public function register(): void
// Make sure Tenancy is stateful.
$this->app->singleton(Tenancy::class);

// Make sure features are bootstrapped as soon as Tenancy is instantiated.
$this->app->extend(Tenancy::class, function (Tenancy $tenancy) {
$this->app->resolving(Tenancy::class, function (Tenancy $tenancy, $app) {
foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) {
$this->app[$feature]->bootstrap($tenancy);
$this->app[$feature]->bootstrap();
}

return $tenancy;
});

foreach ($this->app['config']['tenancy.tenant_unaware_features'] ?? [] as $feature) {
$this->app[$feature]->bootstrap();
}
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved

// Make it possible to inject the current tenant by typehinting the Tenant contract.
$this->app->bind(Tenant::class, function ($app) {
return $app[Tenancy::class]->tenant;
Expand Down
19 changes: 18 additions & 1 deletion tests/Features/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

test('tenant redirect macro replaces only the hostname', function () {
config([
'tenancy.features' => [CrossDomainRedirect::class],
'tenancy.tenant_unaware_features' => [CrossDomainRedirect::class],
]);

Route::get('/foobar', function () {
Expand All @@ -34,3 +34,20 @@
expect(tenant_route('foo.localhost', 'foo', ['a' => 'as', 'b' => 'df']))->toBe('http://foo.localhost/abcdef/as/df');
expect(tenant_route('foo.localhost', 'foo', []))->toBe('http://foo.localhost/abcdef');
});

test('redirect from central to tenant works fine', function () {
abrardev99 marked this conversation as resolved.
Show resolved Hide resolved
config([
'tenancy.tenant_unaware_features' => [CrossDomainRedirect::class],
]);

Route::get('/foobar', function () {
return 'Foo';
})->name('home');

Route::get('/redirect', function () {
return redirect()->route('home')->domain('abcd');
});

pest()->get('/redirect')
->assertRedirect('http://abcd/foobar');
});