Skip to content

Commit

Permalink
Merge branch 'BinarCode:main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkirkosa authored Apr 3, 2024
2 parents 881515b + 00959d1 commit 66b1574
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 18 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/auto-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release

on:
pull_request:
types: [ closed ]
branches:
- "main"

jobs:
release:
name: Create release
if: github.event.pull_request.merged == true
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v2
- uses: ncipollo/release-action@v1
with:
name: ${{ github.event.pull_request.title }}
tag: ${{ github.event.pull_request.title }}
body: ${{ github.event.pull_request.body }}
prerelease: false
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
# uses the default GitHub actions app token
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
coverage: none

Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: run-tests

on:
push:
branches: [master]
branches: [main]
pull_request:
branches: [master]
branches: [main]

jobs:
test:
Expand All @@ -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.1",
"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
18 changes: 16 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 @@ -8,13 +10,20 @@

'related_tenant_column' => env('TENANTABLE_RELATED_TENANT_COLUMN', 'tenant_id'),

/**
* Allow the `tenant_id` to be null.
*
* If the entry does have null tenant_id, it will be considered as a global entry.
*/
'allow_nullable_tenant' => env('ALLOW_NULLABLE_TENANT', false),

/**
* 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 +32,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
36 changes: 35 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 @@ -86,6 +87,10 @@ public function forget(): Tenantable
"database.connections.{$tenantConnectionName}.database" => null,
]);

app()->forgetInstance($this->containerKey());

app()->forgetInstance(Tenantable::class);

DB::purge($tenantConnectionName);

return $this;
Expand All @@ -95,4 +100,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) : '');
}
}
14 changes: 13 additions & 1 deletion src/Models/TenantScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ class TenantScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where($model->qualifyColumn(config('tenantable.related_tenant_column')), tenant()->key());
$column = $model->qualifyColumn(config('tenantable.related_tenant_column'));
$tenantKey = Tenant::check()
? tenant()->key()
: null;

if (config('tenantable.allow_nullable_tenant')) {
$builder->where(function (Builder $query) use ($column, $tenantKey) {
$query->whereNull($column)
->orWhere($column, $tenantKey);
});
} else {
$builder->where($column, $tenantKey);
}
}

public function extend(Builder $builder)
Expand Down
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 66b1574

Please sign in to comment.