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

Specify namespace on global functions (fix #35)

This commit is contained in:
Samuel Štancl 2019-04-23 17:02:43 +02:00
parent e1c4dc8934
commit 7f323aaa94
15 changed files with 42 additions and 44 deletions

View file

@ -8,17 +8,17 @@ class CacheManager extends BaseCacheManager
{
public function __call($method, $parameters)
{
$tags = [config('tenancy.cache.tag_base') . tenant('uuid')];
$tags = [\config('tenancy.cache.tag_base') . \tenant('uuid')];
if ($method === "tags") {
if (count($parameters) !== 1) {
if (\count($parameters) !== 1) {
throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed.");
}
$names = $parameters[0];
$names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/5.7/cache#removing-tagged-cache-items
return $this->store()->tags(array_merge($tags, $names));
return $this->store()->tags(\array_merge($tags, $names));
}
return $this->store()->tags($tags)->$method(...$parameters);

View file

@ -49,7 +49,7 @@ class Rollback extends RollbackCommand
$this->input->setOption('database', 'tenant');
tenant()->all($this->option('tenants'))->each(function ($tenant) {
\tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
$this->database->connectToTenant($tenant);

View file

@ -49,7 +49,7 @@ class Seed extends SeedCommand
$this->input->setOption('database', 'tenant');
tenant()->all($this->option('tenants'))->each(function ($tenant) {
\tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
$this->database->connectToTenant($tenant);

View file

@ -38,7 +38,7 @@ class TenantList extends Command
public function handle()
{
$this->info("Listing all tenants.");
tenancy()->all()->each(function ($tenant) {
\tenancy()->all()->each(function ($tenant) {
$this->line("[Tenant] uuid: {$tenant['uuid']} @ {$tenant['domain']}");
});
}

View file

@ -14,9 +14,9 @@ class TenantAssetsController extends Controller
public function asset($path)
{
try {
return response()->file(storage_path("app/public/$path"));
return \response()->file(\storage_path("app/public/$path"));
} catch (\Throwable $th) {
abort(404);
\abort(404);
}
}
}

View file

@ -13,7 +13,7 @@ class DatabaseManager
public function __construct(BaseDatabaseManager $database)
{
$this->originalDefaultConnection = config('database.default');
$this->originalDefaultConnection = \config('database.default');
$this->database = $database;
}
@ -26,7 +26,7 @@ class DatabaseManager
public function connectToTenant($tenant)
{
$this->connect(tenant()->getDatabaseName($tenant));
$this->connect(\tenant()->getDatabaseName($tenant));
}
public function disconnect()
@ -41,16 +41,16 @@ class DatabaseManager
$this->createTenantConnection($name);
$driver = $driver ?: $this->getDriver();
$databaseManagers = config('tenancy.database_managers');
$databaseManagers = \config('tenancy.database_managers');
if (! array_key_exists($driver, $databaseManagers)) {
if (! \array_key_exists($driver, $databaseManagers)) {
throw new \Exception("Database could not be created: no database manager for driver $driver is registered.");
}
if (config('tenancy.queue_database_creation', false)) {
QueuedTenantDatabaseCreator::dispatch(app($databaseManagers[$driver]), $name, 'create');
if (\config('tenancy.queue_database_creation', false)) {
QueuedTenantDatabaseCreator::dispatch(\app($databaseManagers[$driver]), $name, 'create');
} else {
app($databaseManagers[$driver])->createDatabase($name);
\app($databaseManagers[$driver])->createDatabase($name);
}
}

View file

@ -23,7 +23,7 @@ class InitializeTenancy
public function handle($request, Closure $next)
{
try {
tenancy()->init();
\tenancy()->init();
} catch (\Exception $e) {
// Pass the exception to the onFail function if it takes any parameters.
$callback = $this->onFail;

View file

@ -38,7 +38,7 @@ class RedisStorageDriver implements StorageDriver
return $this->redis->hgetall("tenants:$uuid");
}
return array_combine($fields, $this->redis->hmget("tenants:$uuid", $fields));
return \array_combine($fields, $this->redis->hmget("tenants:$uuid", $fields));
}
public function getTenantIdByDomain(string $domain): ?string
@ -49,7 +49,7 @@ class RedisStorageDriver implements StorageDriver
public function createTenant(string $domain, string $uuid): array
{
$this->redis->hmset("domains:$domain", 'tenant_id', $uuid);
$this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain));
$this->redis->hmset("tenants:$uuid", 'uuid', \json_encode($uuid), 'domain', \json_encode($domain));
return $this->redis->hgetall("tenants:$uuid");
}
@ -67,13 +67,13 @@ class RedisStorageDriver implements StorageDriver
public function getAllTenants(array $uuids = []): array
{
$hashes = array_map(function ($hash) {
$hashes = \array_map(function ($hash) {
return "tenants:{$hash}";
}, $uuids);
$hashes = $hashes ?: $this->redis->scan(null, 'tenants:*');
return array_map(function ($tenant) {
return \array_map(function ($tenant) {
return $this->redis->hgetall($tenant);
}, $hashes);
}

View file

@ -12,7 +12,6 @@ use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Commands\TenantList;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Interfaces\ServerConfigManager;
use Stancl\Tenancy\StorageDrivers\RedisStorageDriver;
class TenancyServiceProvider extends ServiceProvider
{
@ -33,7 +32,7 @@ class TenancyServiceProvider extends ServiceProvider
}
$this->publishes([
__DIR__ . '/config/tenancy.php' => config_path('tenancy.php'),
__DIR__ . '/config/tenancy.php' => \config_path('tenancy.php'),
], 'config');
$this->loadRoutesFrom(__DIR__ . '/routes.php');

View file

@ -9,7 +9,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
public function createDatabase(string $name): bool
{
try {
return fclose(fopen(database_path($name), 'w'));
return \fclose(\fopen(\database_path($name), 'w'));
} catch (\Throwable $th) {
return false;
}
@ -18,7 +18,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
public function deleteDatabase(string $name): bool
{
try {
return unlink(database_path($name));
return \unlink(\database_path($name));
} catch (\Throwable $th) {
return false;
}

View file

@ -2,7 +2,6 @@
namespace Stancl\Tenancy;
use Illuminate\Support\Facades\Redis;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Traits\BootstrapsTenancy;
use Illuminate\Contracts\Foundation\Application;
@ -160,13 +159,13 @@ class TenantManager
public static function currentDomain(): ?string
{
return request()->getHost() ?? null;
return \request()->getHost() ?? null;
}
public function getDatabaseName($tenant = []): string
{
$tenant = $tenant ?: $this->tenant;
return config('tenancy.database.prefix') . $tenant['uuid'] . config('tenancy.database.suffix');
return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix'];
}
/**
@ -204,7 +203,7 @@ class TenantManager
{
$uuid = (array) $uuids;
return collect(array_map(function ($tenant_array) {
return \collect(\array_map(function ($tenant_array) {
return $this->jsonDecodeArrayValues($tenant_array);
}, $this->storage->getAllTenants($uuids)));
}
@ -233,11 +232,11 @@ class TenantManager
{
$uuid = $uuid ?: $this->tenant['uuid'];
if (is_array($key)) {
if (\is_array($key)) {
return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key));
}
return json_decode($this->storage->get($uuid, $key), true);
return \json_decode($this->storage->get($uuid, $key), true);
}
/**
@ -250,7 +249,7 @@ class TenantManager
*/
public function put($key, $value = null, string $uuid = null)
{
if (is_null($uuid)) {
if (\is_null($uuid)) {
if (! isset($this->tenant['uuid'])) {
throw new \Exception("No UUID supplied (and no tenant is currently identified).");
}
@ -264,17 +263,17 @@ class TenantManager
$target = []; // black hole
}
if (! is_null($value)) {
return $target[$key] = json_decode($this->storage->put($uuid, $key, json_encode($value)), true);
if (! \is_null($value)) {
return $target[$key] = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true);
}
if (! is_array($key)) {
if (! \is_array($key)) {
throw new \Exception("No value supplied for key $key.");
}
foreach ($key as $k => $v) {
$target[$k] = $v;
$key[$k] = json_encode($v);
$key[$k] = \json_encode($v);
}
return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key));
@ -295,8 +294,8 @@ class TenantManager
protected function jsonDecodeArrayValues(array $array)
{
array_walk($array, function (&$value, $key) {
$value = json_decode($value, true);
\array_walk($array, function (&$value, $key) {
$value = \json_decode($value, true);
});
return $array;
@ -310,7 +309,7 @@ class TenantManager
*/
public function __invoke($attribute)
{
if (is_null($attribute)) {
if (\is_null($attribute)) {
return $this->tenant;
}

View file

@ -9,11 +9,11 @@ class TenantRouteServiceProvider extends RouteServiceProvider
{
public function map()
{
if (! in_array(request()->getHost(), $this->app['config']['tenancy.exempt_domains'] ?? [])
&& file_exists(base_path('routes/tenant.php'))) {
if (! \in_array(\request()->getHost(), $this->app['config']['tenancy.exempt_domains'] ?? [])
&& \file_exists(\base_path('routes/tenant.php'))) {
Route::middleware(['web', 'tenancy'])
->namespace($this->app['config']['tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
->group(\base_path('routes/tenant.php'));
}
}
}

View file

@ -26,7 +26,7 @@ trait BootstrapsTenancy
public function setPhpRedisPrefix($connections = ['default'])
{
foreach ($connections as $connection) {
$prefix = config('tenancy.redis.prefix_base') . $this->tenant['uuid'];
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid'];
$client = Redis::connection($connection)->client();
$client->setOption($client::OPT_PREFIX, $prefix);
}

View file

@ -6,6 +6,6 @@ trait DealsWithMigrations
{
protected function getMigrationPaths()
{
return [config('tenancy.migrations_directory', database_path('migrations/tenant'))];
return [\config('tenancy.migrations_directory', \database_path('migrations/tenant'))];
}
}

View file

@ -8,7 +8,7 @@ trait HasATenantsOption
{
protected function getOptions()
{
return array_merge([
return \array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null]
], parent::getOptions());
}