Skip to content

Commit 094cd6c

Browse files
authored
Merge pull request #24 from BinarCode/2.4.1
2.4.1
2 parents dc7b041 + 05c4b03 commit 094cd6c

File tree

9 files changed

+62
-15
lines changed

9 files changed

+62
-15
lines changed

.github/workflows/run-tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
fail-fast: true
1414
matrix:
1515
os: [ubuntu-latest, windows-latest]
16-
php: [8.0]
17-
laravel: [8.*]
16+
php: [8.1]
17+
laravel: [10.*]
1818
stability: [prefer-lowest, prefer-stable]
1919
include:
20-
- laravel: 8.*
21-
testbench: ^6.6
20+
- laravel: 10.*
21+
testbench: 8.*
2222

2323
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
2424

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.phpunit.cache
12
build
23
composer.lock
34
vendor

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
}
2424
],
2525
"require": {
26-
"php": "^8.0",
27-
"illuminate/contracts": "^8.37"
26+
"php": "^8.1",
27+
"illuminate/contracts": "^10.0"
2828
},
2929
"require-dev": {
30-
"brianium/paratest": "^6.2",
31-
"nunomaduro/collision": "^5.3",
32-
"orchestra/testbench": "^6.15",
33-
"phpunit/phpunit": "^9.3",
30+
"brianium/paratest": "^6.3",
31+
"orchestra/testbench": "^8.0",
32+
"phpunit/phpunit": "^9.3.3",
3433
"spatie/laravel-ray": "^1.9",
3534
"vimeo/psalm": "^4.4"
3635
},

config/tenantable.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use BinarCode\Tenantable\Models\Tenant;
4+
35
return [
46
/**
57
* The name of the table in the database.
@@ -13,15 +15,15 @@
1315
*
1416
* If the entry does have null tenant_id, it will be considered as a global entry.
1517
*/
16-
'allow_nullable_tenant' => env('ALLOW_NULLABLE_TENANT', true),
18+
'allow_nullable_tenant' => env('ALLOW_NULLABLE_TENANT', false),
1719

1820
/**
1921
* The base model for tenant.
2022
*/
21-
'model' => BinarCode\Tenantable\Models\TenantContract::class,
23+
'model' => Tenant::class,
2224

2325
/*
24-
* Domain for the maine application
26+
* Domain for the main application
2527
*/
2628
'master_domain' => env('MASTER_DOMAIN', 'sample.test'),
2729

@@ -30,6 +32,11 @@
3032
*/
3133
'master_fqdn' => env('MASTER_FQDN', 'admin.sample.test'),
3234

35+
/**
36+
* The HTTP protocol.
37+
*/
38+
'protocol' => env('PROTOCOL', 'https'),
39+
3340
/*
3441
* The connection name to reach the a tenant database.
3542
*

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
33
<coverage>
44
<include>
55
<directory suffix=".php">src/</directory>

psalm.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0"?>
22
<psalm
33
errorLevel="4"
4+
findUnusedCode="false"
5+
findUnusedBaselineEntry="true"
46
findUnusedVariablesAndParams="true"
57
resolveFromConfigFile="true"
68
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

src/Models/Tenant.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use BinarCode\Tenantable\Models\Concerns\UsesMasterConnection;
1111
use BinarCode\Tenantable\Tenant\Contracts\DatabaseConfig;
1212
use BinarCode\Tenantable\Tenant\Contracts\Tenantable;
13+
use Illuminate\Database\Eloquent\Casts\Attribute;
1314
use Illuminate\Database\Eloquent\Model;
1415
use Illuminate\Support\Facades\DB;
1516
use Illuminate\Support\Str;
@@ -75,7 +76,7 @@ public function makeCurrentMaster()
7576

7677
public function isActive(): bool
7778
{
78-
return boolval($this->active);
79+
return (bool) $this->active;
7980
}
8081

8182
public function forget(): Tenantable
@@ -95,4 +96,33 @@ public function databaseConfig(): DatabaseConfig
9596
{
9697
return \BinarCode\Tenantable\Tenant\DatabaseConfig::make($this);
9798
}
99+
100+
public function frontend(): Attribute
101+
{
102+
/**
103+
* @psalm-suppress UndefinedFunction
104+
*/
105+
return new Attribute(
106+
get: fn (
107+
) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')),
108+
);
109+
}
110+
111+
public function api(): Attribute
112+
{
113+
/**
114+
* @psalm-suppress UndefinedFunction
115+
*/
116+
return new Attribute(
117+
get: fn (
118+
) => config('tenantable.protocol')."://{$this->subdomain}.".withoutProtocol(config('tenantable.master_domain')).'/api',
119+
);
120+
}
121+
122+
public function frontendRoute(string $route, array $query = []): string
123+
{
124+
$route = str($route)->whenStartsWith('/', fn ($str) => $str->replaceFirst('/', ''))->toString();
125+
126+
return "{$this->frontend}/{$route}".(count($query) ? '?'.http_build_query($query) : '');
127+
}
98128
}

src/Tenant/PendingNew.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BinarCode\Tenantable\Models\Tenant;
77
use Illuminate\Contracts\Support\Arrayable;
88

9+
/** @psalm-suppress MissingTemplateParam */
910
class PendingNew implements Arrayable
1011
{
1112
use Make;

src/helpers.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ function fromTenant($property = null)
1111
{
1212
return data_get(app(Tenantable::class), $property);
1313
}
14+
15+
if (! function_exists('withoutProtocol')) {
16+
function withoutProtocol(string $fqdn = ''): string
17+
{
18+
return str_replace(['https://', 'http://', 'www.'], ['https://' => '', 'http://' => '', 'www.' => ''], $fqdn);
19+
}
20+
}

0 commit comments

Comments
 (0)