diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e573a9c..57bf9fa 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -13,12 +13,12 @@ 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.* - testbench: ^6.6 + - laravel: 10.* + testbench: 8.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index ce6cadb..2feea41 100644 --- a/composer.json +++ b/composer.json @@ -23,16 +23,15 @@ } ], "require": { - "php": "^8.0|^8.1", - "illuminate/contracts": "^8.37|^9.0|^10.0" + "php": "^8.0", + "illuminate/contracts": "^10.0" }, "require-dev": { - "brianium/paratest": "^6.2|^7.0.6", - "nunomaduro/collision": "^5.3|^6.1|^7.0", - "orchestra/testbench": "^6.15|^7.0|^8.0", - "phpunit/phpunit": "^9.3|^10.0", + "brianium/paratest": "^6.3", + "orchestra/testbench": "^8.0", + "phpunit/phpunit": "^9.3.3", "spatie/laravel-ray": "^1.9", - "vimeo/psalm": "^4.4|^5.6" + "vimeo/psalm": "^4.4" }, "autoload": { "psr-4": { 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..51957cf 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 ( + ) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')), + ); + } + + public function api(): Attribute + { + /** + * @psalm-suppress UndefinedFunction + */ + return new Attribute( + 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(); + + return "{$this->frontend}/{$route}".(count($query) ? '?'.http_build_query($query) : ''); + } } diff --git a/src/helpers.php b/src/helpers.php index 5d9c134..22323da 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -11,3 +11,10 @@ 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); + } +}