1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 19:24:02 +00:00

Merge branch '1.x' into telescope-tags

This commit is contained in:
Samuel Štancl 2019-08-02 20:06:52 +02:00
commit f23fd125b1
9 changed files with 112 additions and 6 deletions

View file

@ -1,5 +1,11 @@
# Release Notes for 1.x
## [v1.6.0 (2019-07-30)](https://github.com/stancl/tenancy/compare/v1.5.1...v1.6.0)
### Added
- `GlobalCache` facade [#78](https://github.com/stancl/tenancy/pull/78)
## [v1.5.1 (2019-07-25)](https://github.com/stancl/tenancy/compare/v1.5.0...v1.5.1)
### Fixed

View file

@ -1,4 +1,4 @@
# Tenancy
# [stancl/tenancy](https://stancl.github.io/tenancy/)
[![Laravel 5.7+](https://img.shields.io/badge/laravel-5.7+-red.svg)](https://laravel.com)
[![Latest Stable Version](https://poser.pugx.org/stancl/tenancy/version)](https://packagist.org/packages/stancl/tenancy)
@ -20,7 +20,7 @@ You won't have to change a thing in your application's code.\*
<details>
<summary><strong>Click to expand/collapse</strong></summary>
- [Tenancy](#tenancy)
- [stancl/tenancy](#stancltenancy)
+ [*A Laravel multi-database tenancy package that respects your code.*](#-a-laravel-multi-database-tenancy-package-that-respects-your-code-)
- [Installation](#installation)
+ [Requirements](#requirements)
@ -392,8 +392,9 @@ Connections listed in the `tenancy.redis.prefixed_connections` config array use
## Cache
Both `cache()` and `Cache` will use `Stancl\Tenancy\CacheManager`, which adds a tag (`prefix_base` + tenant UUID) to all methods called on it.
Both the `cache()` helper and the `Cache` facade will use `Stancl\Tenancy\CacheManager`, which adds a tag (`prefix_base` + tenant UUID) to all methods called on it.
If you need to store something in global, non-tenant cache, you can use the `GlobalCache` facade the same way you'd use the `Cache` facade.
## Filesystem/Storage
@ -475,6 +476,8 @@ Storage::disk('public')->put($filename, $data);
Storage::disk('local')->put("public/$filename", $data);
```
If you want to store something globally, simply create a new disk and *don't* add it to the `tenancy.filesystem.disks` config.
## Artisan commands
```

View file

@ -41,7 +41,8 @@
"Stancl\\Tenancy\\TenancyServiceProvider"
],
"aliases": {
"Tenancy": "Stancl\\Tenancy\\TenancyFacade"
"Tenancy": "Stancl\\Tenancy\\TenancyFacade",
"GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade"
}
}
},

View file

@ -24,7 +24,7 @@
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="CACHE_DRIVER" value="redis"/>
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>

13
src/GlobalCacheFacade.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Stancl\Tenancy;
use Illuminate\Support\Facades\Facade;
class GlobalCacheFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'globalCache';
}
}

View file

@ -13,6 +13,7 @@ use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Commands\TenantList;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Interfaces\ServerConfigManager;
use Illuminate\Cache\CacheManager;
class TenancyServiceProvider extends ServiceProvider
{
@ -83,5 +84,9 @@ class TenancyServiceProvider extends ServiceProvider
$this->app->singleton(Seed::class, function ($app) {
return new Seed($app['db'], $app[DatabaseManager::class]);
});
$this->app->bind('globalCache', function ($app) {
return new CacheManager($app);
});
}
}

View file

@ -73,4 +73,36 @@ class CacheManagerTest extends TestCase
cache(['foo' => 'xyz'], 1);
$this->assertSame('xyz', cache('foo'));
}
/** @test */
public function cache_is_persisted()
{
tenant()->create('foo.localhost');
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->end();
tenancy()->init('foo.localhost');
$this->assertSame('bar', cache('foo'));
}
/** @test */
public function cache_is_persisted_when_reidentification_is_used()
{
tenant()->create('foo.localhost');
tenant()->create('bar.localhost');
tenancy()->init('foo.localhost');
cache(['foo' => 'bar'], 10);
$this->assertSame('bar', cache('foo'));
tenancy()->init('bar.localhost');
tenancy()->end();
tenancy()->init('foo.localhost');
$this->assertSame('bar', cache('foo'));
}
}

43
tests/GlobalCacheTest.php Normal file
View file

@ -0,0 +1,43 @@
<?php
namespace Stancl\Tenancy\Tests;
use GlobalCache;
class GlobalCacheTest extends TestCase
{
public $autoCreateTenant = false;
public $autoInitTenancy = false;
/** @test */
public function global_cache_manager_stores_data_in_global_cache()
{
$this->assertSame(null, cache('foo'));
GlobalCache::put(['foo' => 'bar'], 1);
$this->assertSame('bar', GlobalCache::get('foo'));
tenant()->create('foo.localhost');
tenancy()->init('foo.localhost');
$this->assertSame('bar', GlobalCache::get('foo'));
GlobalCache::put(['abc' => 'xyz'], 1);
cache(['def' => 'ghi'], 10);
$this->assertSame('ghi', cache('def'));
tenancy()->end();
$this->assertSame('xyz', GlobalCache::get('abc'));
$this->assertSame('bar', GlobalCache::get('foo'));
$this->assertSame(null, cache('def'));
tenant()->create('bar.localhost');
tenancy()->init('bar.localhost');
$this->assertSame('xyz', GlobalCache::get('abc'));
$this->assertSame('bar', GlobalCache::get('foo'));
$this->assertSame(null, cache('def'));
cache(['def' => 'xxx'], 1);
$this->assertSame('xxx', cache('def'));
tenancy()->init('foo.localhost');
$this->assertSame('ghi', cache('def'));
}
}

View file

@ -19,6 +19,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
parent::setUp();
Redis::connection('tenancy')->flushdb();
Redis::connection('cache')->flushdb();
if ($this->autoCreateTenant) {
$this->createTenant();
@ -77,6 +78,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
's3',
],
'tenancy.redis.tenancy' => true,
'tenancy.redis.prefixed_connections' => ['default'],
'tenancy.migrations_directory' => database_path('../migrations'),
]);
}
@ -98,7 +100,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
protected function getPackageAliases($app)
{
return [
'Tenancy' => \Stancl\Tenancy\TenancyFacade::class
'Tenancy' => \Stancl\Tenancy\TenancyFacade::class,
'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class,
];
}