From 8da31fa7b61d773de09c318562b2bcc4b5527f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 17 Aug 2019 14:19:31 +0200 Subject: [PATCH] event system --- navigation.php | 1 + source/docs/event-system.md | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 source/docs/event-system.md diff --git a/navigation.php b/navigation.php index da72f58e..58561211 100644 --- a/navigation.php +++ b/navigation.php @@ -27,6 +27,7 @@ return [ 'Custom Database Names' => 'docs/custom-database-names', 'Tenancy Initialization' => 'docs/tenancy-initialization', 'Filesystem Tenancy' => 'docs/filesystem-tenancy', + 'Event System' => 'docs/event-system', 'Writing Storage Drivers' => 'docs/writing-storage-drivers', 'Development' => 'docs/development', ], diff --git a/source/docs/event-system.md b/source/docs/event-system.md new file mode 100644 index 00000000..a6e0a675 --- /dev/null +++ b/source/docs/event-system.md @@ -0,0 +1,36 @@ +--- +title: The Event System +description: The Event System | stancl/tenancy — A Laravel multi-database tenancy package that respects your code.. +extends: _layouts.documentation +section: content +--- + +# The Event System + +You can use event hooks to change the behavior of the tenancy boostrapping and tenancy ending processes. + +The following events are available: +- `boostrapping` +- `boostrapped` +- `ending` +- `ended` + +You can hook into these events using `Tenancy::`: +```php +Tenancy::boostrapping(function ($tenantManager) { + if ($tenantManager->tenant['uuid'] === 'someUUID') { + config(['database.connections.someDatabaseConnection' => $tenantManager->tenant['databaseConnection']]); + $tenantManager->database->useConnection('someDatabaseConnection'); + + return ['database']; + } +}); +``` + +The example above checks whether the current tenant has an uuid of `someUUID`. If yes, it creates a new database connection based on data stored in the tenant's storage. Then it changes the default database connection. Finally, it returns an array of the events that this callback prevents. + +The following actions can be prevented: +- database connection switch: `database` +- Redis prefix: `redis` +- CacheManager switch: `cache` +- Filesystem changes: `filesystem` \ No newline at end of file