Skip to content

Commit

Permalink
rename namespace and publish stub model
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi Yu committed Apr 21, 2023
1 parent aac833d commit b970b13
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 35 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Tenantify

<a href="https://packagist.org/packages/wuhsien/tenantify"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/wuhsien/tenantify"></a>
<a href="https://packagist.org/packages/wuhsien/tenantify"><img alt="Latest Version" src="https://img.shields.io/packagist/v/wuhsien/tenantify"></a>
<a href="https://packagist.org/packages/wuhsien/tenantify"><img alt="License" src="https://img.shields.io/packagist/l/wuhsien/tenantify"></a>
<a href="https://packagist.org/packages/takeshiyu/tenantify"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/takeshiyu/tenantify"></a>
<a href="https://packagist.org/packages/takeshiyu/tenantify"><img alt="Latest Version" src="https://img.shields.io/packagist/v/takeshiyu/tenantify"></a>
<a href="https://packagist.org/packages/takeshiyu/tenantify"><img alt="License" src="https://img.shields.io/packagist/l/takeshiyu/tenantify"></a>

**Tenantify** is a Laravel package designed to make implementing a multi-tenancy architecture easy and efficient. With **Tenantify**, you can quickly set up your application to support multiple tenants using a **single database**, with each tenant being identified by a **unique subdomain**.

Expand All @@ -20,13 +20,13 @@ To install **Tenantify**, follow these simple steps:
1. Install the package via Composer:

```bash
composer require wuhsien/tenantify
composer require takeshiyu/tenantify
```

2. Publish the configuration file:

```bash
php artisan vendor:publish --provider="Wuhsien\Tenantify\TenantifyServiceProvider" --tag="config"
php artisan vendor:publish --provider="TakeshiYu\Tenantify\TenantifyServiceProvider"
```

### Configuration
Expand All @@ -44,10 +44,10 @@ return [

### Custom Model

If you want to use your custom model and use it for route binding, Make sure that your custom model use the `Wuhsien\Tenantify\Concerns\Tenantable` trait:
If you want to use your custom model and use it for route binding, Make sure that your custom model use the `TakeshiYu\Tenantify\Concerns\Tenantable` trait:

```php
use Wuhsien\Tenantify\Concerns\Tenantable;
use TakeshiYu\Tenantify\Concerns\Tenantable;

class YourCustomModel extends Model
{
Expand All @@ -57,18 +57,18 @@ class YourCustomModel extends Model

### Query Scopes

To scope your queries correctly, apply the `Wuhsien\Tenantify\Concerns\HasTenancy` trait on your models:
To scope your queries correctly, apply the `TakeshiYu\Tenantify\Concerns\HasTenancy` trait on your models:

```php
use Wuhsien\Tenantify\Concerns\HasTenancy;
use TakeshiYu\Tenantify\Concerns\HasTenancy;

class YourModel extends Model
{
use HasTenancy;
}
```

### Route Macro
### Usage

In `routes/web.php` file, define your tenant-specific routes using the `tenancy` macro:

Expand All @@ -78,12 +78,18 @@ Route::tenancy(function () {
});
```

or, assign `TakeshiYu\Tenantify\Middleware\ResolveTenant` middleware to your routes or groups:

```php
Route::get('/', fn () => 'ok')->middleware('tenantify.resolve');
```

### Current Tenant

There are several methods available to work with current tenant:

```php
use Wuhsien\Tenantify\Tenancy;
use TakeshiYu\Tenantify\Tenancy;

Tenancy::tenant(); // returns current tenant instance
Tenancy::id(); // returns current tenant id
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "wuhsien/tenantify",
"name": "takeshiyu/tenantify",
"description": "Tenantify is a Laravel package designed to make implementing a multi-tenancy architecture easy and efficient. With Tenantify, you can quickly set up your application to support multiple tenants using a single database, with each tenant being identified by a unique subdomain.",
"keywords": ["php", "laravel", "subdomain", "multi-tenancy", "tenancy", "tenantify"],
"type": "library",
Expand All @@ -23,12 +23,12 @@
},
"autoload": {
"psr-4": {
"Wuhsien\\Tenantify\\": "src/"
"TakeshiYu\\Tenantify\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"TakeshiYu\\Tenantify\\Tests\\": "tests/"
}
},
"config": {
Expand All @@ -44,7 +44,7 @@
"@php vendor/bin/testbench package:discover --ansi"
],
"test:lint": "pint --test",
"test:unit": "pest --colors=always",
"test:unit": "pest --profile --coverage --colors=always",
"test": [
"@test:lint",
"@test:unit"
Expand All @@ -53,7 +53,7 @@
"extra": {
"laravel": {
"providers": [
"Wuhsien\\Tenantify\\TenantifyServiceProvider"
"TakeshiYu\\Tenantify\\TenantifyServiceProvider"
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/HasTenancy.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Wuhsien\Tenantify\Concerns;
namespace TakeshiYu\Tenantify\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Tenantable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Wuhsien\Tenantify\Concerns;
namespace TakeshiYu\Tenantify\Concerns;

use Illuminate\Support\Facades\Config;

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/TenancyNotInitializedException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Wuhsien\Tenantify\Exceptions;
namespace TakeshiYu\Tenantify\Exceptions;

use Exception;

Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/ResolveTenant.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Wuhsien\Tenantify\Middleware;
namespace TakeshiYu\Tenantify\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;
use Wuhsien\Tenantify\Tenancy;
use TakeshiYu\Tenantify\Tenancy;

class ResolveTenant
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tenancy.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Wuhsien\Tenantify;
namespace TakeshiYu\Tenantify;

use Illuminate\Support\Facades\Facade;

Expand Down
4 changes: 2 additions & 2 deletions src/TenancyManager.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Wuhsien\Tenantify;
namespace TakeshiYu\Tenantify;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Wuhsien\Tenantify\Exceptions\TenancyNotInitializedException;
use TakeshiYu\Tenantify\Exceptions\TenancyNotInitializedException;

class TenancyManager
{
Expand Down
8 changes: 6 additions & 2 deletions src/TenantifyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace Wuhsien\Tenantify;
namespace TakeshiYu\Tenantify;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Wuhsien\Tenantify\Middleware\ResolveTenant;
use TakeshiYu\Tenantify\Middleware\ResolveTenant;

class TenantifyServiceProvider extends ServiceProvider
{
Expand All @@ -33,6 +33,10 @@ public function boot(): void
__DIR__.'/../config/tenantify.php' => config_path('tenantify.php'),
], 'config');

$this->publishes([
__DIR__.'/../stubs/Tenant.php' => app_path('Models/Tenant.php'),
], 'tenantify-support');

$this->registerRouteMacro();
}

Expand Down
19 changes: 19 additions & 0 deletions stubs/Tenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use TakeshiYu\Tenantify\Concerns\Tenantable;

class Tenant extends Model
{
use HasFactory, Tenantable;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'tenants';
}
18 changes: 14 additions & 4 deletions tests/Feature/ResolveTenantTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

use Tests\Models\TenantTest;
use Wuhsien\Tenantify\Exceptions\TenancyNotInitializedException;
use Wuhsien\Tenantify\Tenancy;
use TakeshiYu\Tenantify\Exceptions\TenancyNotInitializedException;
use TakeshiYu\Tenantify\Tenancy;
use TakeshiYu\Tenantify\Tests\Models\TenantTest;

beforeEach(function () {
Config::set('tenantify.tenant_model', TenantTest::class);
Expand All @@ -13,7 +13,7 @@
Tenancy::id();
})->throws(TenancyNotInitializedException::class);

test('Load the correct tenant data based on the subdomain', function () {
test('Load the correct tenant data with tenancy macro', function () {
$tenant = TenantTest::create(['slug' => 'foo']);

$response = $this->get('http://'.$tenant->slug.'.tenantify.test');
Expand All @@ -22,3 +22,13 @@
$this->assertEquals($tenant->id, Tenancy::id());
$this->assertEquals($tenant->slug, Tenancy::slug());
});

test('Load the correct tenant data without tenancy macro', function () {
$tenant = TenantTest::create(['slug' => 'foo']);

$response = $this->get('http://'.$tenant->slug.'.tenantify.test/home');
$response->assertSee('ok');

$this->assertEquals($tenant->id, Tenancy::id());
$this->assertEquals($tenant->slug, Tenancy::slug());
});
4 changes: 2 additions & 2 deletions tests/Models/TenantTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Tests\Models;
namespace TakeshiYu\Tenantify\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Wuhsien\Tenantify\Concerns\Tenantable;
use TakeshiYu\Tenantify\Concerns\Tenantable;

class TenantTest extends Model
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
|
*/

uses(Tests\TenantifyTestCase::class)->in('Feature');
uses(TakeshiYu\Tenantify\Tests\TenantifyTestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions tests/TenantifyTestCase.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Tests;
namespace TakeshiYu\Tenantify\Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase;
use Wuhsien\Tenantify\TenantifyServiceProvider;
use TakeshiYu\Tenantify\TenantifyServiceProvider;

class TenantifyTestCase extends TestCase
{
Expand Down Expand Up @@ -41,6 +41,8 @@ protected function defineDatabaseMigrations()
*/
protected function defineRoutes($router)
{
$router->get('/home', fn () => 'ok')->middleware('tenantify.resolve');

$router->tenancy(fn ($router) => $router->get('/', fn () => 'ok'));
}
}

0 comments on commit b970b13

Please sign in to comment.