Skip to content

Commit

Permalink
Refactor more old code and get tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed May 13, 2020
1 parent c5377a1 commit c32f229
Show file tree
Hide file tree
Showing 72 changed files with 426 additions and 532 deletions.
98 changes: 86 additions & 12 deletions assets/TenancyServiceProvider.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

namespace App\Providers;

use Closure;
use Stancl\Tenancy\Contracts\Tenant;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Events\DatabaseCreated;
use Stancl\Tenancy\Events\DatabaseDeleted;
use Stancl\Tenancy\Events\DatabaseMigrated;
use Stancl\Tenancy\Events\DatabaseRolledBack;
use Stancl\Tenancy\Events\DatabaseSeeded;
use Stancl\Tenancy\Events\Listeners\JobPipeline;
use Stancl\Tenancy\Events\DomainCreated;
use Stancl\Tenancy\Events\DomainDeleted;
use Stancl\Tenancy\Events\DomainSaved;
use Stancl\Tenancy\Events\DomainUpdated;
use Stancl\Tenancy\Events\RevertedToCentralContext;
use Stancl\Tenancy\Events\TenancyBootstrapped;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\JobPipeline;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Events\TenantSaved;
use Stancl\Tenancy\Events\TenantUpdated;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Jobs\DeleteDatabase;
use Stancl\Tenancy\Jobs\MigrateDatabase;
use Stancl\Tenancy\Jobs\SeedDatabase;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tenancy;

class TenancyServiceProvider extends ServiceProvider
{
Expand All @@ -25,35 +41,82 @@ public function events()
TenantCreated::class => [
JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class, // triggers DatabaseMigrated event
MigrateDatabase::class,
SeedDatabase::class,

// Your own jobs to prepare the tenant.
// Provision API keys, create S3 buckets, anything you want!

])->send(function (TenantCreated $event) {
return $event->tenant;
})->queue(true),
})->queue(false), // `false` by default, but you probably want to make this `true` for production.
],
DatabaseCreated::class => [],
DatabaseMigrated::class => [],
DatabaseSeeded::class => [],
TenantSaved::class => [],
TenantUpdated::class => [],
TenantDeleted::class => [
JobPipeline::make([
DeleteDatabase::class,
])->send(function (TenantDeleted $event) {
return $event->tenant;
})->queue(true),
// DeleteStorage::class,
})->queue(false), // `false` by default, but you probably want to make this `true` for production.
],

DomainCreated::class => [],
DomainSaved::class => [],
DomainUpdated::class => [],
DomainDeleted::class => [],


DatabaseCreated::class => [],
DatabaseMigrated::class => [],
DatabaseSeeded::class => [],
DatabaseRolledBack::class => [],
DatabaseDeleted::class => [],

TenancyInitialized::class => [
BootstrapTenancy::class,
],
TenancyEnded::class => [
RevertToCentralContext::class,
],

TenancyBootstrapped::class => [],
RevertedToCentralContext::class => [],
];
}
}

public function register()
{
//
// Make sure Tenancy is stateful.
$this->app->singleton(Tenancy::class);

// Make sure features are bootstrapped as soon as Tenancy is instantiated.
$this->app->extend(Tenancy::class, function (Tenancy $tenancy) {
foreach ($this->app['config']['tenancy.features'] as $feature) {
$this->app[$feature]->bootstrap($tenancy);
}

return $tenancy;
});

// Make it possible to inject the current tenant by typehinting the Tenant contract.
$this->app->bind(Tenant::class, function ($app) {
return $app[Tenancy::class]->tenant;
});

// Make sure bootstrappers are stateful (singletons).
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
$this->app->singleton($bootstrapper);
}

// Bind the class in the tenancy.id_generator config to the UniqueIdentifierGenerator abstract.
$this->app->bind(UniqueIdentifierGenerator::class, $this->app['config']['tenancy.id_generator']);
}

public function boot()
{
$this->bootEvents();
$this->mapRoutes();

//
}
Expand All @@ -70,4 +133,15 @@ protected function bootEvents()
}
}
}
}

protected function mapRoutes()
{
$this->app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
Route::middleware(['web'])
->namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
}
});
}
}
27 changes: 1 addition & 26 deletions assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@
'localhost',
],


'storage' => [
'data_column' => 'data',
'custom_columns' => [
// 'plan',
],

/**
* Here you can enable the Cached Tenant Lookup.
*
* You can specify what cache store should be used to cache the tenant resolution.
* Set to string with a specific cache store name, or to null to disable cache.
*/
'cache_store' => null, // env('CACHE_DRIVER')
'cache_ttl' => 3600, // seconds
],

/**
* Controller namespace used by routes in routes/tenant.php.
*/
Expand Down Expand Up @@ -76,7 +59,6 @@
*/
'prefix' => 'tenant',
'suffix' => '',
// todo get rid of this stuff, just set the closure instead
],

/**
Expand Down Expand Up @@ -194,18 +176,11 @@
* See the documentation page for each class to
* understand which ones you want to enable.
*/
'features' => [ // todo test features
// Stancl\Tenancy\Features\Timestamps::class, // https://tenancy.samuelstancl.me/docs/v2/features/timestamps/
'features' => [
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-config/
// Stancl\Tenancy\Features\TelescopeTags::class, // https://tenancy.samuelstancl.me/docs/v2/telescope/
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-redirect/
],

/**
* The URL to which users will be redirected when they try to acceess a central route on a tenant domain.
*/
'home_url' => '/app', // todo move this to static

'migration_parameters' => [
'--force' => true, // Set this to true to be able to run migrations in production
'--path' => [database_path('migrations/tenant')],
Expand Down
15 changes: 11 additions & 4 deletions assets/tenant_routes.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider
| with the tenancy and web middleware groups. Good luck!
| with the namespace configured in your tenancy config.
|
| Feel free to customize them however you want. Good luck!
|
*/

Route::get('/app', function () {
return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
});
Route::group([
'middleware' => InitializeTenancyByDomain::class,
'prefix' => '/app',
], function () {
Route::get('/', function () {
return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
});
});
3 changes: 2 additions & 1 deletion src/Commands/Rollback.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Migrations\Migrator;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\DatabaseManager;
use Stancl\Tenancy\Events\DatabaseRolledBack;
use Stancl\Tenancy\Traits\DealsWithMigrations;
use Stancl\Tenancy\Traits\HasATenantsOption;

Expand Down Expand Up @@ -62,7 +63,7 @@ public function handle()
// Rollback
parent::handle();

// todo DatabaseRolledBack event
event(new DatabaseRolledBack($tenant));
});
}
}
2 changes: 1 addition & 1 deletion src/Contracts/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Stancl\Tenancy\Contracts;

use Stancl\Tenancy\Facades\Tenancy;
use Stancl\Tenancy\Tenancy;

/** Additional features, like Telescope tags and tenant redirects. */
interface Feature
Expand Down
21 changes: 0 additions & 21 deletions src/Contracts/Future/CanDeleteKeys.php

This file was deleted.

24 changes: 0 additions & 24 deletions src/Contracts/Future/CanFindByAnyKey.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Contracts/Future/CanSetConnection.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/Contracts/TenancyBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
*/
interface TenancyBootstrapper
{
// todo rename methods
public function start(Tenant $tenant);
public function bootstrap(Tenant $tenant);

public function end();
public function revert();
}
8 changes: 8 additions & 0 deletions src/Contracts/TenantDatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function databaseExists(string $name): bool;
* @return array
*/
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;

/**
* Set the DB connection that should be used by the tenant database manager.
*
* @param string $connection
* @return void
*/
public function setConnection(string $connection): void;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stancl\Tenancy\Database\Models\Concerns;
namespace Stancl\Tenancy\Database\Concerns;

trait CentralConnection
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stancl\Tenancy\Database\Models\Concerns;
namespace Stancl\Tenancy\Database\Concerns;

use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;

Expand All @@ -14,4 +14,9 @@ public static function bootGeneratesIds()
}
});
}

public function getIncrementing()
{
return ! app()->bound(UniqueIdentifierGenerator::class);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

// todo move namespace one dir above
namespace Stancl\Tenancy\Database\Models\Concerns;
namespace Stancl\Tenancy\Database\Concerns;

// todo rename
trait HasADataColumn
{
public static $priorityListeners = [];
Expand Down Expand Up @@ -53,11 +51,17 @@ public static function bootHasADataColumn()
$model->dataEncodingStatus = 'decoded';
};

static::registerPriorityListener('retrieved', $decode);
static::registerPriorityListener('retrieved', function ($model) use ($decode) {
// We always decode after model retrieval.
$model->dataEncodingStatus = 'encoded';

$decode($model);
});

static::registerPriorityListener('saving', $encode);
static::registerPriorityListener('creating', $encode);
static::registerPriorityListener('updating', $encode);

static::registerPriorityListener('saved', $decode);
static::registerPriorityListener('created', $decode);
static::registerPriorityListener('updated', $decode);
Expand Down
Loading

0 comments on commit c32f229

Please sign in to comment.