Skip to content

Commit

Permalink
Merge pull request #22 from BinarCode/2.2.1
Browse files Browse the repository at this point in the history
2.3.0
  • Loading branch information
binaryk authored Jul 10, 2023
2 parents 37c97dc + ca963e3 commit a00429a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
11 changes: 9 additions & 2 deletions config/tenantable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use BinarCode\Tenantable\Models\Tenant;

return [
/**
* The name of the table in the database.
Expand All @@ -11,10 +13,10 @@
/**
* The base model for tenant.
*/
'model' => 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'),

Expand All @@ -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.
*
Expand Down
32 changes: 31 additions & 1 deletion src/Models/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function makeCurrentMaster()

public function isActive(): bool
{
return boolval($this->active);
return (bool) $this->active;
}

public function forget(): Tenantable
Expand All @@ -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) : '');
}
}
7 changes: 7 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a00429a

Please sign in to comment.