2.x filesystem tenancy, events

This commit is contained in:
Samuel Štancl 2019-09-22 15:26:03 +02:00
parent 71287b36cb
commit 3a131a3202
2 changed files with 15 additions and 13 deletions

View file

@ -17,9 +17,9 @@ The following events are available:
### Tenant-specific database connection example {#tenant-specific-database-connection-example} ### Tenant-specific database connection example {#tenant-specific-database-connection-example}
You can hook into these events using `Tenancy::<eventName>`: You can hook into these events using `Tenancy::eventListener(<eventName>, function () {})`:
```php ```php
\Tenancy::boostrapping(function ($tenantManager) { \Tenancy::eventListener('bootstrapping', function ($tenantManager) {
if ($tenantManager->tenant['id'] === 'someID') { if ($tenantManager->tenant['id'] === 'someID') {
config(['database.connections.someDatabaseConnection' => $tenantManager->tenant['databaseConnection']]); config(['database.connections.someDatabaseConnection' => $tenantManager->tenant['databaseConnection']]);
$tenantManager->database->useConnection('someDatabaseConnection'); $tenantManager->database->useConnection('someDatabaseConnection');
@ -41,7 +41,7 @@ The following actions can be prevented:
Another common use case for events is tenant-specific config: Another common use case for events is tenant-specific config:
```php ```php
\Tenancy::bootstrapped(function ($tenantManager) { \Tenancy::eventListener('bootstrapped', function ($tenantManager) {
config(['some.api.key' => $tenantManager->tenant['api_key']); config(['some.api.key' => $tenantManager->tenant['api_key']);
}); });
``` ```

View file

@ -13,7 +13,7 @@ The `storage_path()` will be suffixed with a directory named `config('tenancy.fi
The root of each disk listed in `tenancy.filesystem.disks` will be suffixed with `config('tenancy.filesystem.suffix_base') . $id`. The root of each disk listed in `tenancy.filesystem.disks` will be suffixed with `config('tenancy.filesystem.suffix_base') . $id`.
**However, this alone would cause unwanted behavior.** It would work for S3 and similar disks, but for local disks, this would result in `/path_to_your_application/storage/app/tenant1e22e620-1cb8-11e9-93b6-8d1b78ac0bcd/`. That's not what we want. We want `/path_to_your_application/storage/tenant1e22e620-1cb8-11e9-93b6-8d1b78ac0bcd/app/`. **However, this alone would cause unwanted behavior.** It would work for S3 and similar disks, but for local disks, this would result in `/path_to_your_application/storage/app/tenant1e22e620-1cb8-11e9-93b6-8d1b78ac0bcd/`. That's not what we want. We want `/path_to_your_application/storage/tenant1e22e620-1cb8-11e9-93b6-8d1b78ac0bcd/app/`. Why? Because `storage_path()` returns `/path_to_your_application/storage/tenant1e22e620-1cb8-11e9-93b6-8d1b78ac0bcd/`, so `storage_path('app') means appending `app` to that.
That's what the `root_override` section is for. `%storage_path%` gets replaced by `storage_path()` *after* tenancy has been initialized. The roots of disks listed in the `root_override` section of the config will be replaced accordingly. All other disks will be simply suffixed with `tenancy.filesystem.suffix_base` + the tenant id. That's what the `root_override` section is for. `%storage_path%` gets replaced by `storage_path()` *after* tenancy has been initialized. The roots of disks listed in the `root_override` section of the config will be replaced accordingly. All other disks will be simply suffixed with `tenancy.filesystem.suffix_base` + the tenant id.
@ -25,18 +25,20 @@ If you write to these directories, you will need to create them after you create
Logs will be saved to `storage/logs` regardless of any changes to `storage_path()`. Logs will be saved to `storage/logs` regardless of any changes to `storage_path()`.
One thing that you **will** have to change if you use storage similarly to the example on the image is your use of the helper function `asset()` (that is, if you use it). ## Assets {#assets}
You need to make this change to your code: The `asset()` helper has two different paths of execution:
```diff - If `config('app.asset_url')` has been set, it will simply append `tenant$id` to the end of the configured asset URL. This is useful if you use Laravel Vapor. Vapor sets the asset URL to something like `https://abcdefghijkl.cloudfrount.net/123-456-789`. That is the root for your assets. This package will append that with something like `tenant1e22e620-1cb8-11e9-93b6-8d1b78ac0bcd`.
- asset("storage/images/products/$product_id.png"); - If `config('app.asset_url')` is null, as it is by default, the helper will return a URL (`/tenancy/assets/...`) to a controller provided by this package. That controller returns a file response from `storage_path("app/public/$path")`. This means that you need to store your assets in the public directory.
+ tenant_asset("images/products/$product_id.png");
```
Note that all (public) tenant assets have to be in the `app/public/` subdirectory of the tenant's storage directory, as shown in the image above. > Note: In 1.x, the `asset()` helper was not tenant-aware, and there was a `tenant_asset()` helper that followed the second option in the list above (a link to a controller). For backwards compatibility, that helper remains intact.
This is what the backend of `tenant_asset()` returns: > If you have some non-tenant-specific assets, you may use the `global_asset()` helper.
Note that all tenant assets have to be in the `app/public/` subdirectory of the tenant's storage directory, as shown in the image above.
This is what the backend of `tenant_asset()` (and `asset()` when no asset URL is configured) returns:
```php ```php
// TenantAssetsController // TenantAssetsController
return response()->file(storage_path('app/public/' . $path)); return response()->file(storage_path('app/public/' . $path));
@ -49,4 +51,4 @@ Storage::disk('public')->put($filename, $data);
Storage::disk('local')->put("public/$filename", $data); Storage::disk('local')->put("public/$filename", $data);
``` ```
If you want to store something globally, simply create a new disk and *don't* add it to the `tenancy.filesystem.disks` config. If you want to store something globally, simply create a new disk and *don't* add it to the `tenancy.filesystem.disks` config.