From f619940a60e0855f284ec466b866ae668b869020 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 16:27:26 +0300 Subject: [PATCH 1/9] fix: adding helpers --- config/tenantable.php | 11 +++++++++-- src/Models/Tenant.php | 32 +++++++++++++++++++++++++++++++- src/helpers.php | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/config/tenantable.php b/config/tenantable.php index 02b1bf7..5768b06 100644 --- a/config/tenantable.php +++ b/config/tenantable.php @@ -1,5 +1,7 @@ BinarCode\Tenantable\Models\TenantContract::class, + 'model' => Tenant::class, /* - * Domain for the maine application + * Domain for the main application */ 'master_domain' => env('MASTER_DOMAIN', 'sample.test'), @@ -23,6 +25,11 @@ */ 'master_fqdn' => env('MASTER_FQDN', 'admin.sample.test'), + /** + * The HTTP protocol. + */ + 'protocol' => env('PROTOCOL', 'https'), + /* * The connection name to reach the a tenant database. * diff --git a/src/Models/Tenant.php b/src/Models/Tenant.php index 476bf1c..46c8395 100644 --- a/src/Models/Tenant.php +++ b/src/Models/Tenant.php @@ -10,6 +10,7 @@ use BinarCode\Tenantable\Models\Concerns\UsesMasterConnection; use BinarCode\Tenantable\Tenant\Contracts\DatabaseConfig; use BinarCode\Tenantable\Tenant\Contracts\Tenantable; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; @@ -75,7 +76,7 @@ public function makeCurrentMaster() public function isActive(): bool { - return boolval($this->active); + return (bool) $this->active; } public function forget(): Tenantable @@ -95,4 +96,33 @@ public function databaseConfig(): DatabaseConfig { return \BinarCode\Tenantable\Tenant\DatabaseConfig::make($this); } + + public function frontend(): Attribute + { + /** + * @psalm-suppress UndefinedFunction + */ + return new Attribute( + get: fn ($value + ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')), + ); + } + + public function api(): Attribute + { + /** + * @psalm-suppress UndefinedFunction + */ + return new Attribute( + get: fn ($value + ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')).'/api', + ); + } + + public function frontendRoute(string $route, array $query = []): string + { + $route = str($route)->whenStartsWith('/', fn ($str) => $str->replaceFirst('/', ''))->toString(); + + return "{$this->frontend}/{$route}".(count($query) ? '?'.http_build_query($query) : ''); + } } diff --git a/src/helpers.php b/src/helpers.php index 5d9c134..650a764 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -11,3 +11,20 @@ function fromTenant($property = null) { return data_get(app(Tenantable::class), $property); } + +if (! function_exists('withoutProtocol')) { + function withoutProtocol(string $fqdn = ''): string + { + return str_replace(['https://', 'http://', 'www.'], ['https://' => '', 'http://' => '', 'www.' => ''], $fqdn); + } +} + +if (! function_exists('str')) { + /** + * @return Str + */ + function str() + { + return resolve(Str::class); + } +} From cba2f7ed857e2873d617f01b096970364db7e50f Mon Sep 17 00:00:00 2001 From: binaryk Date: Mon, 10 Jul 2023 13:27:53 +0000 Subject: [PATCH 2/9] Fix styling --- src/Models/Tenant.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Models/Tenant.php b/src/Models/Tenant.php index 46c8395..8caa884 100644 --- a/src/Models/Tenant.php +++ b/src/Models/Tenant.php @@ -103,7 +103,8 @@ public function frontend(): Attribute * @psalm-suppress UndefinedFunction */ return new Attribute( - get: fn ($value + get: fn ( + $value ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')), ); } @@ -114,7 +115,8 @@ public function api(): Attribute * @psalm-suppress UndefinedFunction */ return new Attribute( - get: fn ($value + get: fn ( + $value ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')).'/api', ); } From a0278180bad4488b75df77a2134f84fb26a61697 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 16:30:06 +0300 Subject: [PATCH 3/9] fix: wip --- src/Models/Tenant.php | 10 +++++----- src/helpers.php | 10 ---------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Models/Tenant.php b/src/Models/Tenant.php index 46c8395..c60ad0f 100644 --- a/src/Models/Tenant.php +++ b/src/Models/Tenant.php @@ -38,7 +38,7 @@ public static function current(): ?Tenant { $containerKey = config('tenantable.container_key'); - if (! app()->has($containerKey)) { + if (!app()->has($containerKey)) { return null; } @@ -52,7 +52,7 @@ public static function check(): bool public static function isMaster(): bool { - return ! static::check(); + return !static::check(); } public function makeCurrent(): Tenantable @@ -103,7 +103,7 @@ public function frontend(): Attribute * @psalm-suppress UndefinedFunction */ return new Attribute( - get: fn ($value + get: fn( ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')), ); } @@ -114,14 +114,14 @@ public function api(): Attribute * @psalm-suppress UndefinedFunction */ return new Attribute( - get: fn ($value + get: fn( ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')).'/api', ); } public function frontendRoute(string $route, array $query = []): string { - $route = str($route)->whenStartsWith('/', fn ($str) => $str->replaceFirst('/', ''))->toString(); + $route = str($route)->whenStartsWith('/', fn($str) => $str->replaceFirst('/', ''))->toString(); return "{$this->frontend}/{$route}".(count($query) ? '?'.http_build_query($query) : ''); } diff --git a/src/helpers.php b/src/helpers.php index 650a764..22323da 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -18,13 +18,3 @@ function withoutProtocol(string $fqdn = ''): string return str_replace(['https://', 'http://', 'www.'], ['https://' => '', 'http://' => '', 'www.' => ''], $fqdn); } } - -if (! function_exists('str')) { - /** - * @return Str - */ - function str() - { - return resolve(Str::class); - } -} From 852e393c485b54631c3be105ec98a443a9078914 Mon Sep 17 00:00:00 2001 From: binaryk Date: Mon, 10 Jul 2023 13:30:53 +0000 Subject: [PATCH 4/9] Fix styling --- src/Models/Tenant.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Models/Tenant.php b/src/Models/Tenant.php index c60ad0f..51957cf 100644 --- a/src/Models/Tenant.php +++ b/src/Models/Tenant.php @@ -38,7 +38,7 @@ public static function current(): ?Tenant { $containerKey = config('tenantable.container_key'); - if (!app()->has($containerKey)) { + if (! app()->has($containerKey)) { return null; } @@ -52,7 +52,7 @@ public static function check(): bool public static function isMaster(): bool { - return !static::check(); + return ! static::check(); } public function makeCurrent(): Tenantable @@ -103,7 +103,7 @@ public function frontend(): Attribute * @psalm-suppress UndefinedFunction */ return new Attribute( - get: fn( + get: fn ( ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')), ); } @@ -114,14 +114,14 @@ public function api(): Attribute * @psalm-suppress UndefinedFunction */ return new Attribute( - get: fn( + get: fn ( ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')).'/api', ); } public function frontendRoute(string $route, array $query = []): string { - $route = str($route)->whenStartsWith('/', fn($str) => $str->replaceFirst('/', ''))->toString(); + $route = str($route)->whenStartsWith('/', fn ($str) => $str->replaceFirst('/', ''))->toString(); return "{$this->frontend}/{$route}".(count($query) ? '?'.http_build_query($query) : ''); } From 325389165e1c1671968a2e25c88a4b190a92c339 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 16:34:55 +0300 Subject: [PATCH 5/9] fix: wip --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 58ed339..c9f4c85 100644 --- a/composer.json +++ b/composer.json @@ -23,14 +23,14 @@ } ], "require": { - "php": "^8.0", - "illuminate/contracts": "^8.37" + "php": "^8.1", + "illuminate/contracts": "^8.0|^9.0|^10.0" }, "require-dev": { - "brianium/paratest": "^6.2", - "nunomaduro/collision": "^5.3", + "brianium/paratest": "^6.3", + "nunomaduro/collision": "^5.11", "orchestra/testbench": "^6.15", - "phpunit/phpunit": "^9.3", + "phpunit/phpunit": "^9.3.3", "spatie/laravel-ray": "^1.9", "vimeo/psalm": "^4.4" }, From 7589c868a733e0bfe843b822a14773b94c4ff2e4 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 16:36:21 +0300 Subject: [PATCH 6/9] fix: wip --- .github/workflows/run-tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e573a9c..03e3da8 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -13,11 +13,11 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest] - php: [8.0] - laravel: [8.*] + php: [8.1] + laravel: [10.*] stability: [prefer-lowest, prefer-stable] include: - - laravel: 8.* + - laravel: 10.* testbench: ^6.6 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} From 759bc002fecd5cb232005771bef31313cf84de3c Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 16:38:58 +0300 Subject: [PATCH 7/9] fix: wip --- .github/workflows/run-tests.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 03e3da8..57bf9fa 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -18,7 +18,7 @@ jobs: stability: [prefer-lowest, prefer-stable] include: - laravel: 10.* - testbench: ^6.6 + testbench: 8.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index c9f4c85..147d2fb 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "require-dev": { "brianium/paratest": "^6.3", "nunomaduro/collision": "^5.11", - "orchestra/testbench": "^6.15", + "orchestra/testbench": "^6.0|^7.0|^8.0", "phpunit/phpunit": "^9.3.3", "spatie/laravel-ray": "^1.9", "vimeo/psalm": "^4.4" From a1efa7cc2841f393b16b3a6270d0ea7544284929 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 22:31:14 +0300 Subject: [PATCH 8/9] fix: wip --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 147d2fb..5d67422 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.0", "illuminate/contracts": "^8.0|^9.0|^10.0" }, "require-dev": { From ca963e351cd2b77791b44d375a78533529699409 Mon Sep 17 00:00:00 2001 From: Eduard Lupacescu Date: Mon, 10 Jul 2023 22:38:55 +0300 Subject: [PATCH 9/9] fix: wip --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5d67422..2feea41 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,11 @@ ], "require": { "php": "^8.0", - "illuminate/contracts": "^8.0|^9.0|^10.0" + "illuminate/contracts": "^10.0" }, "require-dev": { "brianium/paratest": "^6.3", - "nunomaduro/collision": "^5.11", - "orchestra/testbench": "^6.0|^7.0|^8.0", + "orchestra/testbench": "^8.0", "phpunit/phpunit": "^9.3.3", "spatie/laravel-ray": "^1.9", "vimeo/psalm": "^4.4"