From b12b9c3a5445e768c2b49cbec510c7320ae263dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 18 Aug 2019 12:48:10 +0200 Subject: [PATCH 1/7] [1.8.0] Tenant redirect (#103) --- composer.json | 2 +- src/TenancyServiceProvider.php | 16 ++++++++++++++++ tests/TenantRedirectMacroTest.php | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/TenantRedirectMacroTest.php diff --git a/composer.json b/composer.json index 78357d1c..63196393 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require-dev": { "vlucas/phpdotenv": "^3.3", "laravel/framework": "5.8.*", - "orchestra/testbench": "~3.8", + "orchestra/testbench-browser-kit": "~3.8", "league/flysystem-aws-s3-v3": "~1.0", "phpunit/phpcov": "^6.0" }, diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 09ffeaaf..790e7210 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -7,6 +7,7 @@ use Stancl\Tenancy\Commands\Seed; use Illuminate\Cache\CacheManager; use Stancl\Tenancy\Commands\Install; use Stancl\Tenancy\Commands\Migrate; +use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Commands\Rollback; use Illuminate\Support\ServiceProvider; @@ -47,6 +48,21 @@ class TenancyServiceProvider extends ServiceProvider ]); $this->app->register(TenantRouteServiceProvider::class); + + $this->registerTenantRedirectMacro(); + } + + 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; + }); } /** diff --git a/tests/TenantRedirectMacroTest.php b/tests/TenantRedirectMacroTest.php new file mode 100644 index 00000000..de44a650 --- /dev/null +++ b/tests/TenantRedirectMacroTest.php @@ -0,0 +1,23 @@ +name('home'); + + Route::get('/redirect', function () { + return redirect()->route('home')->tenant('abcd'); + }); + + $this->get('/redirect') + ->assertRedirect('http://abcd/foobar'); + } +} From e41a46951051f50534e903421e872fcedebb1ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 18 Aug 2019 23:25:10 +0200 Subject: [PATCH 2/7] Remove defaulting to 'default' DB connection --- src/Tenant.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tenant.php b/src/Tenant.php index 418b78fa..0f14eaac 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -30,7 +30,7 @@ class Tenant extends Model public function getConnectionName() { - return config('tenancy.storage.db.connection') ?: config('database.default'); + return config('tenancy.storage.db.connection'); } public static function getAllTenants(array $uuids) From 8456280ac4930f7db9be29eb0fe4cf663faa167f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 18 Aug 2019 23:30:40 +0200 Subject: [PATCH 3/7] Test: Remove defaulting to 'default' DB connection --- tests/TenantStorageTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index da32f228..523f6bf8 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -147,9 +147,5 @@ class TenantStorageTest extends TestCase { config(['tenancy.storage.db.connection' => 'foo']); $this->assertSame('foo', (new Tenant)->getConnectionName()); - - config(['tenancy.storage.db.connection' => null]); - config(['database.default' => 'foobar']); - $this->assertSame('foobar', (new Tenant)->getConnectionName()); } } From 314b2d300388b4e44e84516e3daffe79ed52b6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 19 Aug 2019 10:55:48 +0200 Subject: [PATCH 4/7] Fix bug with JSON encoding --- src/TenantManager.php | 26 +++++++++++++++++++++----- tests/TenantStorageTest.php | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 5c10d80c..417c661d 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -273,16 +273,21 @@ final class TenantManager { $uuid = $uuid ?: $this->tenant['uuid']; + // todo make this cache work with arrays if (\array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && ! \is_array($key) && \array_key_exists($key, $this->tenant)) { return $this->tenant[$key]; } if (\is_array($key)) { - return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); + $data = $this->storage->getMany($uuid, $key); + $data = $this->useJson() ? $this->jsonDecodeArrayValues($data) : $data; + return $data; } - return \json_decode($this->storage->get($uuid, $key), true); + $data = $this->storage->get($uuid, $key); + $data = $this->useJson() ? \json_decode($data, true) : $data; + return $data; } /** @@ -319,7 +324,13 @@ final class TenantManager } if (! \is_null($value)) { - return $target[$key] = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true); + if ($this->useJson()) { + $data = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true); + } else { + $data = $this->storage->put($uuid, $key, $value); + } + + return $target[$key] = $data; } if (! \is_array($key)) { @@ -328,10 +339,15 @@ final class TenantManager foreach ($key as $k => $v) { $target[$k] = $v; - $key[$k] = \json_encode($v); + + $v = $this->useJson() ? \json_decode($v) : $v; + $key[$k] = $v; } - return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key)); + $data = $this->storage->putMany($uuid, $key); + $data = $this->useJson() ? $this->jsonDecodeArrayValues($data) : $data; + + return $data; } /** diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 523f6bf8..e997f5d9 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -148,4 +148,20 @@ class TenantStorageTest extends TestCase config(['tenancy.storage.db.connection' => 'foo']); $this->assertSame('foo', (new Tenant)->getConnectionName()); } + + /** @test */ + public function retrieving_data_without_cache_works() + { + tenant()->create('foo.localhost'); + tenancy()->init('foo.localhost'); + + tenancy()->put('foo', 'bar'); + $this->assertSame('bar', tenancy()->get('foo')); + $this->assertSame(['bar'], tenancy()->get(['foo'])); + + tenancy()->end(); + tenancy()->init('foo.localhost'); + $this->assertSame('bar', tenancy()->get('foo')); + $this->assertSame(['bar'], tenancy()->get(['foo'])); + } } From 1818e590e0ae766edcf888825c0e93eb403bf939 Mon Sep 17 00:00:00 2001 From: stancl Date: Mon, 19 Aug 2019 08:56:13 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI --- src/TenantManager.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TenantManager.php b/src/TenantManager.php index 417c661d..111d0e51 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -282,11 +282,13 @@ final class TenantManager if (\is_array($key)) { $data = $this->storage->getMany($uuid, $key); $data = $this->useJson() ? $this->jsonDecodeArrayValues($data) : $data; + return $data; } $data = $this->storage->get($uuid, $key); $data = $this->useJson() ? \json_decode($data, true) : $data; + return $data; } From b268e4bc854cc1361b9c97fdaf5ab5e028c801e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 19 Aug 2019 11:01:45 +0200 Subject: [PATCH 6/7] Add test --- tests/TenantStorageTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index e997f5d9..2dd3e1b8 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -131,15 +131,19 @@ class TenantStorageTest extends TestCase { tenancy()->put('someBool', false); $this->assertSame('boolean', \gettype(tenancy()->get('someBool'))); + $this->assertSame('boolean', \gettype(tenancy()->get(['someBool'])[0])); tenancy()->put('someInt', 5); $this->assertSame('integer', \gettype(tenancy()->get('someInt'))); + $this->assertSame('integer', \gettype(tenancy()->get(['someInt'])[0])); tenancy()->put('someDouble', 11.40); $this->assertSame('double', \gettype(tenancy()->get('someDouble'))); + $this->assertSame('double', \gettype(tenancy()->get(['someDouble'])[0])); tenancy()->put('string', 'foo'); $this->assertSame('string', \gettype(tenancy()->get('string'))); + $this->assertSame('string', \gettype(tenancy()->get(['string'])[0])); } /** @test */ From d04f4bb2a1bf25035487b2cd44d59b49d8209c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 19 Aug 2019 11:21:44 +0200 Subject: [PATCH 7/7] decode -> encode --- src/TenantManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 111d0e51..de466648 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -342,7 +342,7 @@ final class TenantManager foreach ($key as $k => $v) { $target[$k] = $v; - $v = $this->useJson() ? \json_decode($v) : $v; + $v = $this->useJson() ? \json_encode($v) : $v; $key[$k] = $v; }