mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:24:02 +00:00
merge 1.x
This commit is contained in:
commit
4d64542ce1
6 changed files with 84 additions and 11 deletions
|
|
@ -16,10 +16,10 @@
|
|||
},
|
||||
"require-dev": {
|
||||
"vlucas/phpdotenv": "^3.3",
|
||||
"league/flysystem-aws-s3-v3": "~1.0",
|
||||
"laravel/telescope": "^2.0",
|
||||
"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"
|
||||
},
|
||||
"autoload": {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,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;
|
||||
|
|
@ -52,6 +53,8 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
if (\class_exists(Telescope::class)) {
|
||||
$this->setTelescopeTags();
|
||||
}
|
||||
|
||||
$this->registerTenantRedirectMacro();
|
||||
}
|
||||
|
||||
public function setTelescopeTags()
|
||||
|
|
@ -75,6 +78,19 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
});
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -273,16 +273,23 @@ 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 +326,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 +341,15 @@ final class TenantManager
|
|||
|
||||
foreach ($key as $k => $v) {
|
||||
$target[$k] = $v;
|
||||
$key[$k] = \json_encode($v);
|
||||
|
||||
$v = $this->useJson() ? \json_encode($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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
23
tests/TenantRedirectMacroTest.php
Normal file
23
tests/TenantRedirectMacroTest.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Route;
|
||||
|
||||
class TenantRedirectMacroTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function tenant_redirect_macro_replaces_only_the_hostname()
|
||||
{
|
||||
Route::get('/foobar', function () {
|
||||
return 'Foo';
|
||||
})->name('home');
|
||||
|
||||
Route::get('/redirect', function () {
|
||||
return redirect()->route('home')->tenant('abcd');
|
||||
});
|
||||
|
||||
$this->get('/redirect')
|
||||
->assertRedirect('http://abcd/foobar');
|
||||
}
|
||||
}
|
||||
|
|
@ -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 */
|
||||
|
|
@ -147,9 +151,21 @@ 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());
|
||||
/** @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']));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue