From d63e15ec2ea37e5a67c1772278565a95b6daf7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 15:36:31 +0200 Subject: [PATCH 01/16] Add issue templates --- .github/ISSUE_TEMPLATE/---bug-report.md | 22 +++++++++++++++++++ .../ISSUE_TEMPLATE/---feature-suggestion.md | 14 ++++++++++++ .github/ISSUE_TEMPLATE/---support-question.md | 10 +++++++++ .github/ISSUE_TEMPLATE/bug_report.md | 22 +++++++++++++++++++ .github/ISSUE_TEMPLATE/feature-suggestion.md | 14 ++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 20 +++++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/---bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/---feature-suggestion.md create mode 100644 .github/ISSUE_TEMPLATE/---support-question.md create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature-suggestion.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/---bug-report.md b/.github/ISSUE_TEMPLATE/---bug-report.md new file mode 100644 index 00000000..ce301e06 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---bug-report.md @@ -0,0 +1,22 @@ +--- +name: "\U0001F41B Bug Report" +about: Report unexpected behavior with stancl/tenancy. +title: '' +labels: bug +assignees: stancl + +--- + +#### Describe the bug + + +#### Steps to reproduce + + +#### Expected behavior +A clear and concise description of what you expected to happen. + +#### Your setup + - Laravel version [e.g. 5.8] + - stancl/tenancy version [e.g. 22] + - Storage driver [e.g. Redis] diff --git a/.github/ISSUE_TEMPLATE/---feature-suggestion.md b/.github/ISSUE_TEMPLATE/---feature-suggestion.md new file mode 100644 index 00000000..537b20f6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---feature-suggestion.md @@ -0,0 +1,14 @@ +--- +name: "\U0001F4A1 Feature Suggestion" +about: Suggest an idea for stancl/tenancy. +title: '' +labels: feature +assignees: stancl + +--- + +#### Description + + +#### Why this should be added + diff --git a/.github/ISSUE_TEMPLATE/---support-question.md b/.github/ISSUE_TEMPLATE/---support-question.md new file mode 100644 index 00000000..76723c61 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---support-question.md @@ -0,0 +1,10 @@ +--- +name: "❤️ Support Question" +about: Ask for help with implementing stancl/tenancy. +title: '' +labels: support +assignees: stancl + +--- + + diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..34a0ed84 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,22 @@ +--- +name: Bug report +about: Report unexpected behavior with stancl/tenancy. +title: '' +labels: bug +assignees: stancl + +--- + +#### Describe the bug + + +#### Steps to reproduce + + +#### Expected behavior +A clear and concise description of what you expected to happen. + +#### Your setup + - Laravel version [e.g. 5.8] + - stancl/tenancy version [e.g. 22] + - Storage driver [e.g. Redis] diff --git a/.github/ISSUE_TEMPLATE/feature-suggestion.md b/.github/ISSUE_TEMPLATE/feature-suggestion.md new file mode 100644 index 00000000..233e06a2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature-suggestion.md @@ -0,0 +1,14 @@ +--- +name: Feature suggestion +about: Suggest an idea for stancl/tenancy. +title: '' +labels: feature +assignees: stancl + +--- + +#### Description + + +#### Why this should be added + diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..7d9fa972 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: feature +assignees: stancl + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. From 0f1da9e15b1dd76e3fa4b3bc6f26ac50357d59d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 15:39:07 +0200 Subject: [PATCH 02/16] update issue templates --- .../ISSUE_TEMPLATE/---feature-suggestion.md | 14 ------------ .../{---bug-report.md => bug-report.md} | 0 .github/ISSUE_TEMPLATE/bug_report.md | 22 ------------------- .github/ISSUE_TEMPLATE/feature-suggestion.md | 2 +- .github/ISSUE_TEMPLATE/feature_request.md | 20 ----------------- ...upport-question.md => support-question.md} | 0 6 files changed, 1 insertion(+), 57 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/---feature-suggestion.md rename .github/ISSUE_TEMPLATE/{---bug-report.md => bug-report.md} (100%) delete mode 100644 .github/ISSUE_TEMPLATE/bug_report.md delete mode 100644 .github/ISSUE_TEMPLATE/feature_request.md rename .github/ISSUE_TEMPLATE/{---support-question.md => support-question.md} (100%) diff --git a/.github/ISSUE_TEMPLATE/---feature-suggestion.md b/.github/ISSUE_TEMPLATE/---feature-suggestion.md deleted file mode 100644 index 537b20f6..00000000 --- a/.github/ISSUE_TEMPLATE/---feature-suggestion.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -name: "\U0001F4A1 Feature Suggestion" -about: Suggest an idea for stancl/tenancy. -title: '' -labels: feature -assignees: stancl - ---- - -#### Description - - -#### Why this should be added - diff --git a/.github/ISSUE_TEMPLATE/---bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md similarity index 100% rename from .github/ISSUE_TEMPLATE/---bug-report.md rename to .github/ISSUE_TEMPLATE/bug-report.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 34a0ed84..00000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -name: Bug report -about: Report unexpected behavior with stancl/tenancy. -title: '' -labels: bug -assignees: stancl - ---- - -#### Describe the bug - - -#### Steps to reproduce - - -#### Expected behavior -A clear and concise description of what you expected to happen. - -#### Your setup - - Laravel version [e.g. 5.8] - - stancl/tenancy version [e.g. 22] - - Storage driver [e.g. Redis] diff --git a/.github/ISSUE_TEMPLATE/feature-suggestion.md b/.github/ISSUE_TEMPLATE/feature-suggestion.md index 233e06a2..537b20f6 100644 --- a/.github/ISSUE_TEMPLATE/feature-suggestion.md +++ b/.github/ISSUE_TEMPLATE/feature-suggestion.md @@ -1,5 +1,5 @@ --- -name: Feature suggestion +name: "\U0001F4A1 Feature Suggestion" about: Suggest an idea for stancl/tenancy. title: '' labels: feature diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index 7d9fa972..00000000 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project -title: '' -labels: feature -assignees: stancl - ---- - -**Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Additional context** -Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/---support-question.md b/.github/ISSUE_TEMPLATE/support-question.md similarity index 100% rename from .github/ISSUE_TEMPLATE/---support-question.md rename to .github/ISSUE_TEMPLATE/support-question.md From 5ba4eec417f06e3cd48133ae9aba22d082ea00cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 15:41:02 +0200 Subject: [PATCH 03/16] Update issue templates --- .github/ISSUE_TEMPLATE/---documentation-issue.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/---documentation-issue.md diff --git a/.github/ISSUE_TEMPLATE/---documentation-issue.md b/.github/ISSUE_TEMPLATE/---documentation-issue.md new file mode 100644 index 00000000..7fc7ec35 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/---documentation-issue.md @@ -0,0 +1,10 @@ +--- +name: "\U0001F4DA Documentation Issue" +about: Suggest how the documentation could be improved. +title: '' +labels: documentation +assignees: stancl + +--- + + From e7716f94a0e6a31c4384725354615dceb25994d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 15:41:40 +0200 Subject: [PATCH 04/16] Rename ---documentation-issue.md to documentation-issue.md --- .../{---documentation-issue.md => documentation-issue.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/ISSUE_TEMPLATE/{---documentation-issue.md => documentation-issue.md} (100%) diff --git a/.github/ISSUE_TEMPLATE/---documentation-issue.md b/.github/ISSUE_TEMPLATE/documentation-issue.md similarity index 100% rename from .github/ISSUE_TEMPLATE/---documentation-issue.md rename to .github/ISSUE_TEMPLATE/documentation-issue.md From 2fe464a1f9e8fc0006d79e9b3f3c96f7f21ffb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 16:01:39 +0200 Subject: [PATCH 05/16] Use only --- src/Tenant.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tenant.php b/src/Tenant.php index 2eb8039f..03fbeb71 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -77,7 +77,7 @@ class Tenant extends Model public function get(string $key) { - return $this->$key ?? $this->getFromData($key) ?? null; + return $this->attributes[$key] ?? $this->getFromData($key) ?? null; } /** @todo In v2, this should return an associative array. */ From 1216addd1fc06a67ce3cc64234cd06cbcbbf5dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 17:55:40 +0200 Subject: [PATCH 06/16] Improve get() --- src/TenantManager.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 37c9e716..1409a36f 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -273,6 +273,10 @@ final class TenantManager { $uuid = $uuid ?: $this->tenant['uuid']; + if (isset($this->tenant['uuid']) && $uuid === $this->tenant['uuid'] && isset($this->tenant[$key])) { + return $this->tenant[$key]; + } + if (\is_array($key)) { return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); } @@ -372,6 +376,6 @@ final class TenantManager return $this->tenant; } - return $this->tenant[(string) $attribute]; + return $this->get((string) $attribute); } } From fd79e0aec32892551c0da4c9a5c06c6bd448a104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:04:54 +0200 Subject: [PATCH 07/16] Fix illegal offset --- src/TenantManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 1409a36f..5c84bd89 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -273,7 +273,7 @@ final class TenantManager { $uuid = $uuid ?: $this->tenant['uuid']; - if (isset($this->tenant['uuid']) && $uuid === $this->tenant['uuid'] && isset($this->tenant[$key])) { + if (array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && array_key_exists($key, $this->tenant)) { return $this->tenant[$key]; } From 3c9ead6aacf628233a5d456db422c3c62a5f43ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:10:34 +0200 Subject: [PATCH 08/16] default value --- src/TenantManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 5c84bd89..b07c93b1 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -37,7 +37,7 @@ final class TenantManager * * @var array */ - public $tenant; + public $tenant = []; public function __construct(Application $app, StorageDriver $storage, DatabaseManager $database) { From bbafc9d8e2a6656b13b35a9a0834f5e1eea0640f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:20:34 +0200 Subject: [PATCH 09/16] is_array check --- src/TenantManager.php | 3 ++- tests/TenantStorageTest.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index b07c93b1..8da49762 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -273,7 +273,8 @@ final class TenantManager { $uuid = $uuid ?: $this->tenant['uuid']; - if (array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && array_key_exists($key, $this->tenant)) { + if (array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && + array_key_exists($key, $this->tenant) && ! is_array($key)) { return $this->tenant[$key]; } diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 8904ed18..7999f37c 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -43,6 +43,7 @@ class TenantStorageTest extends TestCase $data = array_combine($keys, $vals); tenancy()->put($data); + dd(tenant()->get($keys)); $this->assertSame($vals, tenant()->get($keys)); } From 417330decd246e80462b0f4e4e9bead7ea1f608f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:21:53 +0200 Subject: [PATCH 10/16] Add native_function_invocation --- .styleci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.styleci.yml b/.styleci.yml index 2a6c92f2..a1b99be9 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,4 +1,6 @@ preset: laravel +enabled: +- native_function_invocation disabled: - concat_without_spaces - ternary_operator_spaces From 84890cdd1e239394567d3c609023c26b4dce497b Mon Sep 17 00:00:00 2001 From: stancl Date: Fri, 16 Aug 2019 16:21:59 +0000 Subject: [PATCH 11/16] Apply fixes from StyleCI --- src/CacheManager.php | 2 +- src/Commands/Install.php | 6 ++-- src/Commands/Run.php | 8 ++--- src/DatabaseManager.php | 4 +-- src/StorageDrivers/RedisStorageDriver.php | 14 ++++---- src/Tenant.php | 14 ++++---- .../SQLiteDatabaseManager.php | 4 +-- src/TenantManager.php | 32 +++++++++---------- src/TenantRouteServiceProvider.php | 2 +- src/Traits/BootstrapsTenancy.php | 2 +- src/Traits/HasATenantsOption.php | 2 +- src/Traits/TenantManagerEvents.php | 2 +- src/helpers.php | 6 ++-- tests/BootstrapsTenancyTest.php | 2 +- tests/CommandsTest.php | 10 +++--- tests/ReidentificationTest.php | 2 +- tests/TenantAssetTest.php | 6 ++-- tests/TenantDatabaseManagerTest.php | 4 +-- tests/TenantStorageTest.php | 14 ++++---- tests/TestCase.php | 10 +++--- 20 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/CacheManager.php b/src/CacheManager.php index d3b31ea2..52952fe7 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -18,7 +18,7 @@ class CacheManager extends BaseCacheManager $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); diff --git a/src/Commands/Install.php b/src/Commands/Install.php index de1a81b2..a2f162e5 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -34,14 +34,14 @@ class Install extends Command ]); $this->info('✔️ Created config/tenancy.php'); - file_put_contents(app_path('Http/Kernel.php'), str_replace( + \file_put_contents(app_path('Http/Kernel.php'), \str_replace( 'protected $middlewarePriority = [', "protected \$middlewarePriority = [\n \Stancl\Tenancy\Middleware\InitializeTenancy::class,", - file_get_contents(app_path('Http/Kernel.php')) + \file_get_contents(app_path('Http/Kernel.php')) )); $this->info('✔️ Set middleware priority'); - file_put_contents(base_path('routes/tenant.php'), + \file_put_contents(base_path('routes/tenant.php'), " 'bar', 'abc' => 'xyz=zzz'] - $arguments = array_reduce($this->option('argument'), $callback(), []); + $arguments = \array_reduce($this->option('argument'), $callback(), []); // Turns ['foo=bar', 'abc=xyz=zzz'] into ['--foo' => 'bar', '--abc' => 'xyz=zzz'] - $options = array_reduce($this->option('option'), $callback('--'), []); + $options = \array_reduce($this->option('option'), $callback('--'), []); // Run command - $this->call($this->argument('commandname'), array_merge($arguments, $options)); + $this->call($this->argument('commandname'), \array_merge($arguments, $options)); tenancy()->end(); }); diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 68e0cde0..b8152194 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -50,7 +50,7 @@ final class DatabaseManager $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."); } @@ -76,7 +76,7 @@ final class DatabaseManager $databaseManagers = config('tenancy.database_managers'); - if (! array_key_exists($driver, $databaseManagers)) { + if (! \array_key_exists($driver, $databaseManagers)) { throw new \Exception("Database could not be deleted: no database manager for driver $driver is registered."); } diff --git a/src/StorageDrivers/RedisStorageDriver.php b/src/StorageDrivers/RedisStorageDriver.php index 63c087af..9866018c 100644 --- a/src/StorageDrivers/RedisStorageDriver.php +++ b/src/StorageDrivers/RedisStorageDriver.php @@ -37,7 +37,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 @@ -48,7 +48,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"); } @@ -63,7 +63,7 @@ class RedisStorageDriver implements StorageDriver public function deleteTenant(string $id): bool { try { - $domain = json_decode($this->getTenantById($id)['domain']); + $domain = \json_decode($this->getTenantById($id)['domain']); } catch (\Throwable $th) { throw new \Exception("No tenant with UUID $id exists."); } @@ -75,7 +75,7 @@ class RedisStorageDriver implements StorageDriver public function getAllTenants(array $uuids = []): array { - $hashes = array_map(function ($hash) { + $hashes = \array_map(function ($hash) { return "tenants:{$hash}"; }, $uuids); @@ -91,13 +91,13 @@ class RedisStorageDriver implements StorageDriver $all_keys = $this->redis->scan(null, 'MATCH', $redis_prefix . 'tenants:*')[1]; } - $hashes = array_map(function ($key) use ($redis_prefix) { + $hashes = \array_map(function ($key) use ($redis_prefix) { // Left strip $redis_prefix from $key - return substr($key, strlen($redis_prefix)); + return \substr($key, \strlen($redis_prefix)); }, $all_keys); } - return array_map(function ($tenant) { + return \array_map(function ($tenant) { return $this->redis->hgetall($tenant); }, $hashes); } diff --git a/src/Tenant.php b/src/Tenant.php index 03fbeb71..db9997d0 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -54,14 +54,14 @@ class Tenant extends Model public static function decodeData($tenant) { $tenant = $tenant instanceof self ? (array) $tenant->attributes : $tenant; - $decoded = json_decode($tenant[$dataColumn = static::dataColumn()], true); + $decoded = \json_decode($tenant[$dataColumn = static::dataColumn()], true); foreach ($decoded as $key => $value) { $tenant[$key] = $value; } // If $tenant[$dataColumn] has been overriden by a value, don't delete the key. - if (! array_key_exists($dataColumn, $decoded)) { + if (! \array_key_exists($dataColumn, $decoded)) { unset($tenant[$dataColumn]); } @@ -70,7 +70,7 @@ class Tenant extends Model public function getFromData(string $key) { - $this->dataArray = $this->dataArray ?? json_decode($this->{$this->dataColumn()}, true); + $this->dataArray = $this->dataArray ?? \json_decode($this->{$this->dataColumn()}, true); return $this->dataArray[$key] ?? null; } @@ -83,18 +83,18 @@ class Tenant extends Model /** @todo In v2, this should return an associative array. */ public function getMany(array $keys): array { - return array_map([$this, 'get'], $keys); + return \array_map([$this, 'get'], $keys); } public function put(string $key, $value) { - if (array_key_exists($key, $this->customColumns())) { + if (\array_key_exists($key, $this->customColumns())) { $this->update([$key => $value]); } else { - $obj = json_decode($this->{$this->dataColumn()}); + $obj = \json_decode($this->{$this->dataColumn()}); $obj->$key = $value; - $this->update([$this->dataColumn() => json_encode($obj)]); + $this->update([$this->dataColumn() => \json_encode($obj)]); } return $value; diff --git a/src/TenantDatabaseManagers/SQLiteDatabaseManager.php b/src/TenantDatabaseManagers/SQLiteDatabaseManager.php index ca6d027a..cdfc094b 100644 --- a/src/TenantDatabaseManagers/SQLiteDatabaseManager.php +++ b/src/TenantDatabaseManagers/SQLiteDatabaseManager.php @@ -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; } diff --git a/src/TenantManager.php b/src/TenantManager.php index 8da49762..6e27ee1a 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -64,7 +64,7 @@ final class TenantManager $tenant = $this->storage->identifyTenant($domain); - if (! $tenant || ! array_key_exists('uuid', $tenant) || ! $tenant['uuid']) { + if (! $tenant || ! \array_key_exists('uuid', $tenant) || ! $tenant['uuid']) { throw new \Exception("Tenant could not be identified on domain {$domain}."); } @@ -94,7 +94,7 @@ final class TenantManager if ($data) { $this->put($data, null, $tenant['uuid']); - $tenant = array_merge($tenant, $data); + $tenant = \array_merge($tenant, $data); } $this->database->create($this->getDatabaseName($tenant)); @@ -175,7 +175,7 @@ final class TenantManager $uuid = $this->getIdByDomain($domain); - if (is_null($uuid)) { + if (\is_null($uuid)) { throw new \Exception("Tenant with domain $domain could not be identified."); } @@ -240,7 +240,7 @@ final class TenantManager $tenants = $this->storage->getAllTenants($uuids); if ($this->useJson()) { - $tenants = array_map(function ($tenant_array) { + $tenants = \array_map(function ($tenant_array) { return $this->jsonDecodeArrayValues($tenant_array); }, $tenants); } @@ -273,8 +273,8 @@ final class TenantManager { $uuid = $uuid ?: $this->tenant['uuid']; - if (array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && - array_key_exists($key, $this->tenant) && ! is_array($key)) { + if (\array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && + \array_key_exists($key, $this->tenant) && ! \is_array($key)) { return $this->tenant[$key]; } @@ -282,7 +282,7 @@ final class TenantManager 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); } /** @@ -295,10 +295,10 @@ final class TenantManager */ public function put($key, $value = null, string $uuid = null) { - if (in_array($key, ['uuid', 'domain'], true) || ( - is_array($key) && ( - in_array('uuid', array_keys($key), true) || - in_array('domain', array_keys($key), true) + if (\in_array($key, ['uuid', 'domain'], true) || ( + \is_array($key) && ( + \in_array('uuid', \array_keys($key), true) || + \in_array('domain', \array_keys($key), true) ) )) { throw new CannotChangeUuidOrDomainException; @@ -319,7 +319,7 @@ final class TenantManager } if (! \is_null($value)) { - return $target[$key] = json_decode($this->storage->put($uuid, $key, json_encode($value)), true); + return $target[$key] = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true); } if (! \is_array($key)) { @@ -328,7 +328,7 @@ final class TenantManager 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)); @@ -349,8 +349,8 @@ final 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; @@ -358,7 +358,7 @@ final class TenantManager public function useJson() { - if (property_exists($this->storage, 'useJson') && $this->storage->useJson === false) { + if (\property_exists($this->storage, 'useJson') && $this->storage->useJson === false) { return false; } diff --git a/src/TenantRouteServiceProvider.php b/src/TenantRouteServiceProvider.php index 4ea77402..e6551756 100644 --- a/src/TenantRouteServiceProvider.php +++ b/src/TenantRouteServiceProvider.php @@ -10,7 +10,7 @@ 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'))) { + && \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')); diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index b7188968..5e45c3bc 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -138,7 +138,7 @@ trait BootstrapsTenancy foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { $old['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix(); - if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { + if ($root = \str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { Storage::disk($disk)->getAdapter()->setPathPrefix($root); } else { $root = $this->app['config']["filesystems.disks.{$disk}.root"]; diff --git a/src/Traits/HasATenantsOption.php b/src/Traits/HasATenantsOption.php index 1dc055af..1c37cfe1 100644 --- a/src/Traits/HasATenantsOption.php +++ b/src/Traits/HasATenantsOption.php @@ -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()); } diff --git a/src/Traits/TenantManagerEvents.php b/src/Traits/TenantManagerEvents.php index 416ebd72..38e303a0 100644 --- a/src/Traits/TenantManagerEvents.php +++ b/src/Traits/TenantManagerEvents.php @@ -78,7 +78,7 @@ trait TenantManagerEvents */ public function event(string $name): Collection { - return array_reduce($this->listeners[$name], function ($prevents, $listener) { + return \array_reduce($this->listeners[$name], function ($prevents, $listener) { return $prevents->merge($listener($this) ?? []); }, collect([])); } diff --git a/src/helpers.php b/src/helpers.php index 0041f131..29cb3411 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,7 +2,7 @@ use Stancl\Tenancy\TenantManager; -if (! function_exists('tenancy')) { +if (! \function_exists('tenancy')) { function tenancy($key = null) { if ($key) { @@ -13,14 +13,14 @@ if (! function_exists('tenancy')) { } } -if (! function_exists('tenant')) { +if (! \function_exists('tenant')) { function tenant($key = null) { return tenancy($key); } } -if (! function_exists('tenant_asset')) { +if (! \function_exists('tenant_asset')) { function tenant_asset($asset) { return route('stancl.tenancy.asset', ['asset' => $asset]); diff --git a/tests/BootstrapsTenancyTest.php b/tests/BootstrapsTenancyTest.php index fc0d761f..1f21b858 100644 --- a/tests/BootstrapsTenancyTest.php +++ b/tests/BootstrapsTenancyTest.php @@ -86,7 +86,7 @@ class BootstrapsTenancyTest extends TestCase $current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix(); if ($override = config("tenancy.filesystem.root_override.{$disk}")) { - $correct_path_prefix = str_replace('%storage_path%', storage_path(), $override); + $correct_path_prefix = \str_replace('%storage_path%', storage_path(), $override); } else { if ($base = $old_storage_facade_roots[$disk]) { $correct_path_prefix = $base . "/$suffix/"; diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 6d2a8e41..6b161738 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -117,14 +117,14 @@ class CommandsTest extends TestCase /** @test */ public function install_command_works() { - if (! is_dir($dir = app_path('Http'))) { - mkdir($dir, 0777, true); + if (! \is_dir($dir = app_path('Http'))) { + \mkdir($dir, 0777, true); } - if (! is_dir($dir = base_path('routes'))) { - mkdir($dir, 0777, true); + if (! \is_dir($dir = base_path('routes'))) { + \mkdir($dir, 0777, true); } - file_put_contents(app_path('Http/Kernel.php'), "getAdapter()->getPathPrefix(); if ($override = config("tenancy.filesystem.root_override.{$disk}")) { - $correct_path_prefix = str_replace('%storage_path%', storage_path(), $override); + $correct_path_prefix = \str_replace('%storage_path%', storage_path(), $override); } else { if ($base = $originals[$disk]) { $correct_path_prefix = $base . "/$suffix/"; diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 01f1fd28..b43d43c6 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -16,9 +16,9 @@ class TenantAssetTest extends TestCase $this->get(tenant_asset($filename))->assertSuccessful(); $this->assertFileExists($path); - $f = fopen($path, 'r'); - $content = fread($f, filesize($path)); - fclose($f); + $f = \fopen($path, 'r'); + $content = \fread($f, \filesize($path)); + \fclose($f); $this->assertSame('bar', $content); } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 9f6e911b..533c6eee 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -87,7 +87,7 @@ class TenantDatabaseManagerTest extends TestCase config()->set('database.default', 'pgsql'); - $db_name = strtolower('testdatabase' . $this->randomString(10)); + $db_name = \strtolower('testdatabase' . $this->randomString(10)); $this->assertTrue(app(DatabaseManager::class)->create($db_name, 'pgsql')); $this->assertNotEmpty(DB::select("SELECT datname FROM pg_database WHERE datname = '$db_name'")); @@ -104,7 +104,7 @@ class TenantDatabaseManagerTest extends TestCase config()->set('database.default', 'pgsql'); - $db_name = strtolower('testdatabase' . $this->randomString(10)); + $db_name = \strtolower('testdatabase' . $this->randomString(10)); $databaseManagers = config('tenancy.database_managers'); $job = new QueuedTenantDatabaseCreator(app($databaseManagers['pgsql']), $db_name); diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 7999f37c..79001bdd 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -40,7 +40,7 @@ class TenantStorageTest extends TestCase { $keys = ['foo', 'abc']; $vals = ['bar', 'xyz']; - $data = array_combine($keys, $vals); + $data = \array_combine($keys, $vals); tenancy()->put($data); dd(tenant()->get($keys)); @@ -76,13 +76,13 @@ class TenantStorageTest extends TestCase $keys = ['foo', 'abc']; $vals = ['bar', 'xyz']; - $data = array_combine($keys, $vals); + $data = \array_combine($keys, $vals); tenancy()->put($data, null, $uuid); $this->assertSame($vals, tenancy()->get($keys, $uuid)); $this->assertNotSame($vals, tenancy()->get($keys)); - $this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset + $this->assertFalse(\array_intersect($data, tenant()->tenant) == $data); // assert array not subset } /** @test */ @@ -130,15 +130,15 @@ class TenantStorageTest extends TestCase public function data_is_stored_with_correct_data_types() { tenancy()->put('someBool', false); - $this->assertSame('boolean', gettype(tenancy()->get('someBool'))); + $this->assertSame('boolean', \gettype(tenancy()->get('someBool'))); tenancy()->put('someInt', 5); - $this->assertSame('integer', gettype(tenancy()->get('someInt'))); + $this->assertSame('integer', \gettype(tenancy()->get('someInt'))); tenancy()->put('someDouble', 11.40); - $this->assertSame('double', gettype(tenancy()->get('someDouble'))); + $this->assertSame('double', \gettype(tenancy()->get('someDouble'))); tenancy()->put('string', 'foo'); - $this->assertSame('string', gettype(tenancy()->get('string'))); + $this->assertSame('string', \gettype(tenancy()->get('string'))); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 9cafb202..b4f39982 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -24,7 +24,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase Redis::connection('cache')->flushdb(); $this->loadMigrationsFrom([ - '--path' => realpath(__DIR__ . '/../assets/migrations'), + '--path' => \realpath(__DIR__ . '/../assets/migrations'), '--database' => 'central', ]); config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom @@ -63,11 +63,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase */ protected function getEnvironmentSetUp($app) { - if (file_exists(__DIR__ . '/../.env')) { + if (\file_exists(__DIR__ . '/../.env')) { \Dotenv\Dotenv::create(__DIR__ . '/..')->load(); } - fclose(fopen(database_path('central.sqlite'), 'w')); + \fclose(\fopen(database_path('central.sqlite'), 'w')); $app['config']->set([ 'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'), @@ -160,7 +160,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase public function randomString(int $length = 10) { - return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length); + return \substr(\str_shuffle(\str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', \ceil($length / \strlen($x)))), 1, $length); } public function isContainerized() @@ -170,6 +170,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase public function assertArrayIsSubset($subset, $array, string $message = ''): void { - parent::assertTrue(array_intersect($subset, $array) == $subset, $message); + parent::assertTrue(\array_intersect($subset, $array) == $subset, $message); } } From 89c7289b66f29fc8bbc63457133896f372d380a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:26:18 +0200 Subject: [PATCH 12/16] Switch order of conditions --- src/TenantManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TenantManager.php b/src/TenantManager.php index 8da49762..fdd077f8 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -274,7 +274,7 @@ final class TenantManager $uuid = $uuid ?: $this->tenant['uuid']; if (array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && - array_key_exists($key, $this->tenant) && ! is_array($key)) { + ! is_array($key) && array_key_exists($key, $this->tenant)) { return $this->tenant[$key]; } From 6944a46d4c25ddc4f9b1d0a6dad181097f4d77c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:41:49 +0200 Subject: [PATCH 13/16] remove dd --- tests/TenantStorageTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 79001bdd..806e19b0 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -43,7 +43,6 @@ class TenantStorageTest extends TestCase $data = \array_combine($keys, $vals); tenancy()->put($data); - dd(tenant()->get($keys)); $this->assertSame($vals, tenant()->get($keys)); } From 609569635525782fb642ba533f195fbf5438f3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 16 Aug 2019 18:54:25 +0200 Subject: [PATCH 14/16] Fix invoke test --- src/TenantManager.php | 1 + tests/TenantManagerTest.php | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/TenantManager.php b/src/TenantManager.php index beeb641d..5c10d80c 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -370,6 +370,7 @@ final class TenantManager * * @param string $attribute * @return mixed + * @todo Deprecate this in v2. */ public function __invoke($attribute) { diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 453730ea..112e66b5 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -24,6 +24,9 @@ class TenantManagerTest extends TestCase /** @test */ public function invoke_works() { + tenant()->create('foo.localhost'); + tenancy()->init('foo.localhost'); + $this->assertSame(tenant('uuid'), tenant()('uuid')); } From 6028e70b98a810533a81227dd0bfdfd62ba56f29 Mon Sep 17 00:00:00 2001 From: stancl Date: Fri, 16 Aug 2019 16:54:34 +0000 Subject: [PATCH 15/16] Apply fixes from StyleCI --- tests/TenantManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 112e66b5..73bcf0fe 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -26,7 +26,7 @@ class TenantManagerTest extends TestCase { tenant()->create('foo.localhost'); tenancy()->init('foo.localhost'); - + $this->assertSame(tenant('uuid'), tenant()('uuid')); } From 7bcad4718664944c974c1cbfbdb323f95b9af1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 17 Aug 2019 10:03:41 +0200 Subject: [PATCH 16/16] Updated comment --- src/DatabaseManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index b8152194..df5806ed 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -94,7 +94,7 @@ final class DatabaseManager public function createTenantConnection(string $database_name) { - // Create the `tenancy` database connection. + // Create the `tenant` database connection. $based_on = config('tenancy.database.based_on') ?: config('database.default'); config()->set([ 'database.connections.tenant' => config('database.connections.' . $based_on),