mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 12:44:04 +00:00
Improve file structure (#5)
* Add Enums and Overrides folders * Fix code style (php-cs-fixer) --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
1d0ca27bc8
commit
e3b59ae2b5
26 changed files with 33 additions and 32 deletions
38
src/Overrides/CacheManager.php
Normal file
38
src/Overrides/CacheManager.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Overrides;
|
||||
|
||||
use Illuminate\Cache\CacheManager as BaseCacheManager;
|
||||
|
||||
// todo move to Cache namespace?
|
||||
|
||||
class CacheManager extends BaseCacheManager
|
||||
{
|
||||
/**
|
||||
* Add tags and forward the call to the inner cache store.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
$tags = [config('tenancy.cache.tag_base') . tenant()->getTenantKey()];
|
||||
|
||||
if ($method === 'tags') {
|
||||
$count = count($parameters);
|
||||
|
||||
if ($count !== 1) {
|
||||
throw new \Exception("Method tags() takes exactly 1 argument. $count passed.");
|
||||
}
|
||||
|
||||
$names = $parameters[0];
|
||||
$names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/9.x/cache#removing-tagged-cache-items
|
||||
|
||||
return $this->store()->tags(array_merge($tags, $names));
|
||||
}
|
||||
|
||||
return $this->store()->tags($tags)->$method(...$parameters);
|
||||
}
|
||||
}
|
||||
65
src/Overrides/TenancyBroadcastManager.php
Normal file
65
src/Overrides/TenancyBroadcastManager.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Overrides;
|
||||
|
||||
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
|
||||
use Illuminate\Broadcasting\BroadcastManager;
|
||||
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
|
||||
class TenancyBroadcastManager extends BroadcastManager
|
||||
{
|
||||
/**
|
||||
* Names of broadcasters to always recreate using $this->resolve() (even when they're
|
||||
* cached and available in the $broadcasters property).
|
||||
*
|
||||
* The reason for recreating the broadcasters is
|
||||
* to make your app use the correct broadcaster credentials when tenancy is initialized.
|
||||
*/
|
||||
public static array $tenantBroadcasters = ['pusher', 'ably'];
|
||||
|
||||
/**
|
||||
* Override the get method so that the broadcasters in $tenantBroadcasters
|
||||
* always get freshly resolved even when they're cached and available in the $broadcasters property,
|
||||
* and that the resolved broadcaster will override the BroadcasterContract::class singleton.
|
||||
*
|
||||
* If there's a cached broadcaster with the same name as $name,
|
||||
* give its channels to the newly resolved bootstrapper.
|
||||
*/
|
||||
protected function get($name)
|
||||
{
|
||||
if (in_array($name, static::$tenantBroadcasters)) {
|
||||
/** @var Broadcaster|null $originalBroadcaster */
|
||||
$originalBroadcaster = $this->app->make(BroadcasterContract::class);
|
||||
$newBroadcaster = $this->resolve($name);
|
||||
|
||||
// If there is a current broadcaster, give its channels to the newly resolved one
|
||||
// Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract
|
||||
// Which doesn't require the channels property
|
||||
// So passing the channels is only needed for Illuminate\Broadcasting\Broadcasters\Broadcaster instances
|
||||
if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) {
|
||||
$this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);
|
||||
}
|
||||
|
||||
$this->app->singleton(BroadcasterContract::class, fn (Application $app) => $newBroadcaster);
|
||||
|
||||
return $newBroadcaster;
|
||||
}
|
||||
|
||||
return parent::get($name);
|
||||
}
|
||||
|
||||
// Because, unlike the original broadcaster, the newly resolved broadcaster won't have the channels registered using routes/channels.php
|
||||
// Using it for broadcasting won't work, unless we make it have the original broadcaster's channels
|
||||
protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void
|
||||
{
|
||||
// invade() because channels can't be retrieved through any of the broadcaster's public methods
|
||||
$originalBroadcaster = invade($originalBroadcaster);
|
||||
|
||||
foreach ($originalBroadcaster->channels as $channel => $callback) {
|
||||
$newBroadcaster->channel($channel, $callback, $originalBroadcaster->retrieveChannelOptions($channel));
|
||||
}
|
||||
}
|
||||
}
|
||||
119
src/Overrides/TenancyUrlGenerator.php
Normal file
119
src/Overrides/TenancyUrlGenerator.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Overrides;
|
||||
|
||||
use Illuminate\Routing\UrlGenerator;
|
||||
use Illuminate\Support\Arr;
|
||||
use Stancl\Tenancy\PathIdentificationManager;
|
||||
|
||||
/**
|
||||
* This class is used in place of the default UrlGenerator when UrlBindingBootstrapper is enabled.
|
||||
*
|
||||
* TenancyUrlGenerator does two extra things:
|
||||
* 1. Autofill the {tenant} parameter in the tenant context with the current tenant if $passTenantParameterToRoutes is enabled (enabled by default)
|
||||
* 2. Prepend the route name with `tenant.` (or the configured prefix) if $prefixRouteNames is enabled (disabled by default)
|
||||
*
|
||||
* Both of these can be skipped by passing the $bypassParameter (`['central' => true]` by default)
|
||||
*/
|
||||
class TenancyUrlGenerator extends UrlGenerator
|
||||
{
|
||||
/**
|
||||
* Parameter which bypasses the behavior modification of route() and temporarySignedRoute().
|
||||
*
|
||||
* E.g. route('tenant') => app.test/{tenant}/tenant (or app.test/tenant?tenant=tenantKey if the route doesn't accept the tenant parameter)
|
||||
* route('tenant', [$bypassParameter => true]) => app.test/tenant.
|
||||
*/
|
||||
public static string $bypassParameter = 'central';
|
||||
|
||||
/**
|
||||
* Determine if the route names of routes generated using
|
||||
* `route()` or `temporarySignedRoute()` should get prefixed with the tenant route name prefix.
|
||||
*
|
||||
* Set this to true when using path identification.
|
||||
*/
|
||||
public static bool $prefixRouteNames = false;
|
||||
|
||||
/**
|
||||
* Determine if the tenant parameter should get passed
|
||||
* to the links generated by `route()` or `temporarySignedRoute()`.
|
||||
*/
|
||||
public static bool $passTenantParameterToRoutes = true;
|
||||
|
||||
/**
|
||||
* Override the route() method so that the route name gets prefixed
|
||||
* and the tenant parameter gets added when in tenant context.
|
||||
*/
|
||||
public function route($name, $parameters = [], $absolute = true)
|
||||
{
|
||||
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters));
|
||||
|
||||
return parent::route($name, $parameters, $absolute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the temporarySignedRoute() method so that the route name gets prefixed
|
||||
* and the tenant parameter gets added when in tenant context.
|
||||
*/
|
||||
public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true)
|
||||
{
|
||||
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters));
|
||||
|
||||
return parent::temporarySignedRoute($name, $expiration, $parameters, $absolute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return bool indicating if the bypass parameter was in $parameters.
|
||||
*/
|
||||
protected function routeBehaviorModificationBypassed(mixed $parameters): bool
|
||||
{
|
||||
if (isset($parameters[static::$bypassParameter])) {
|
||||
return (bool) $parameters[static::$bypassParameter];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a route name and an array of parameters to return the prefixed route name
|
||||
* and the route parameters with the tenant parameter added.
|
||||
*
|
||||
* To skip these modifications, pass the bypass parameter in route parameters.
|
||||
* Before returning the modified route inputs, the bypass parameter is removed from the parameters.
|
||||
*/
|
||||
protected function prepareRouteInputs(string $name, array $parameters): array
|
||||
{
|
||||
if (! $this->routeBehaviorModificationBypassed($parameters)) {
|
||||
$name = $this->prefixRouteName($name);
|
||||
$parameters = $this->addTenantParameter($parameters);
|
||||
}
|
||||
|
||||
// Remove bypass parameter from the route parameters
|
||||
unset($parameters[static::$bypassParameter]);
|
||||
|
||||
return [$name, $parameters];
|
||||
}
|
||||
|
||||
/**
|
||||
* If $prefixRouteNames is true, prefix the passed route name.
|
||||
*/
|
||||
protected function prefixRouteName(string $name): string
|
||||
{
|
||||
$tenantPrefix = PathIdentificationManager::getTenantRouteNamePrefix();
|
||||
|
||||
if (static::$prefixRouteNames && ! str($name)->startsWith($tenantPrefix)) {
|
||||
$name = str($name)->after($tenantPrefix)->prepend($tenantPrefix)->toString();
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* If `tenant()` isn't null, add tenant paramter to the passed parameters.
|
||||
*/
|
||||
protected function addTenantParameter(array $parameters): array
|
||||
{
|
||||
return tenant() && static::$passTenantParameterToRoutes ? array_merge($parameters, [PathIdentificationManager::getTenantParameterName() => tenant()->getTenantKey()]) : $parameters;
|
||||
}
|
||||
}
|
||||
22
src/Overrides/Vite.php
Normal file
22
src/Overrides/Vite.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Overrides;
|
||||
|
||||
use Illuminate\Foundation\Vite as BaseVite;
|
||||
|
||||
class Vite extends BaseVite
|
||||
{
|
||||
/**
|
||||
* Generate an asset path for the application.
|
||||
*
|
||||
* @param string $path
|
||||
* @param bool|null $secure
|
||||
* @return string
|
||||
*/
|
||||
protected function assetPath($path, $secure = null)
|
||||
{
|
||||
return global_asset($path);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue