Skip to content

Commit

Permalink
refactor(overrides): Refactor service overrides to allow for more com…
Browse files Browse the repository at this point in the history
…plexity (#89)

* chore: Add new overrides config file

* feat(tenancies): Introduce tenancy option config

* chore: Add overrides tenancy option with sensible default

* chore: Add generics to setup and cleanup on ServiceOverride

* refactor: Add a constructor to the ServiceOverride interface

* chore: Add config publishing for new overrides config file

* feat(overrides): Add new service override manager with base functionality

* chore: Add override.all tenancy option

* chore: Remove services config from sprout config

* chore: Add manager to option to override filesystem manager to overrides config

* chore: Updated compatibility check on session and cookie resolvers

* refactor: Update service overrides to use new approach

* chore: Remove HandlesServiceOverrides and DeferrableServiceOverride

* chore: Tidy up service override manager

* chore: Remove old unused tests

* chore: Default tenancy to have all overrides

* fix: Initialise tenancy options with an empty array to prevent errors

* fix: Change overrides config file location because Laravel no longer supports nested directories

* fix: Add missing negation that was breaking everything

* chore: Updates for new config location

* tests: Update tests for changes

* chore: Migrate the core sprout config to sprout/core

* fix: Update to understand new config structure

* chore: Renaming and tidying up of service override components

* test(overrides): Added tests for the new service override manager

* test(resolvers): Add compatibility exception cases to cookie and session identity resolver tests

* test(overrides): Fix skipped tests for auth and session service overrides

* chore: Ignore service override exceptions from code coverage

* feat: Add method for checking whether a tenancy has had their service overrides setup

* fix: Fix two issues with the cache override

The sprout driver for the cache manager wasn't added if the cache manager had already been resolved, and because the driver creation method was bound to the manager, it wasn't able to access $this->drivers

* fix: Fix two issues with the filesystem override

The sprout driver for the cache manager wasn't added if the cache manager had already been resolved, and because the driver creation method was bound to the manager, it wasn't able to access $this->drivers

* test(overrides): Add cache override test

* chore: Add tenant aware contract and default trait implementation

* chore: Add functionality to refresh tenant-aware dependencies when the tenancy and/or tenant changes

* refactor: Update session handler overrides to be persistent and tenant-aware

* chore: Make override filesystem manager aware of whether it override an existing original

* chore: Single for tenancy generic with phpstan and tenant-aware implementation

* test(overrides): Completed the tests of the session service override

* test(overrides): A few QOL fixes for the session override tests

* test(overrides): Fix database session handler test to account for updated Laravel core

* chore(overrides): Fix static analysis issue with overridden DatabaseSessionHandler::performInsert

* test(overrides): Further tests for auth and session overrides

* test(overrides): Add more tests for auth and adjust existing ones

* chore: Remove unneeded phpstan error silencing

* test(overrides): Added a test for the filesystem manager

* test(overrides): Complete the service override tests

* chore: Remove Laravel pint config

* chore: Ignore ServiceOverrideManager::hasTenancyBeenSetup for code coverage
  • Loading branch information
ollieread authored Jan 25, 2025
1 parent a2445af commit 4b399db
Show file tree
Hide file tree
Showing 88 changed files with 6,194 additions and 4,492 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
/wiki/
/build/
.phpunit.result.cache
/tests/_Original
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"phpunit/phpunit" : "^11.0.1",
"orchestra/testbench": "^9.4",
"larastan/larastan" : "^2.9",
"infection/infection": "^0.29.8"
"infection/infection": "^0.29.8",
"brianium/paratest" : "^7.7"
},
"license" : "MIT",
"autoload" : {
Expand Down
14 changes: 10 additions & 4 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
"@default" : true,
"ProtectedVisibility": {
"ignore": [
"Sprout\\Concerns\\FindsIdentityRouteParameters::initialiseRouteParameter",
"Sprout\\Concerns\\FindsIdentityRouteParameters::getParameterPatternMapping",
"Sprout\\Concerns\\FindsIdentityRouteParameters::applyParameterMapping",
"Sprout\\Support\\BaseFactory::callCustomCreator"
"Sprout\\Concerns\\FindsIdentityRouteParameters::*",
"Sprout\\Support\\BaseFactory::callCustomCreator",
"Sprout\\Overrides\\Auth\\SproutAuthCacheTokenRepository::getTenantedPrefix",
"Sprout\\Overrides\\Auth\\SproutAuthDatabaseTokenRepository::*",
"Sprout\\Overrides\\Cache\\SproutCacheDriverCreator::*"
]
},
"PublicVisibility": {
"ignore": [
"Sprout\\Concerns\\FindsIdentityRouteParameters::*"
]
},
"UnwrapUcFirst" : {
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
testdox="true"
>
<testsuites>
<testsuite name="Original">
<directory>tests/_Original</directory>
</testsuite>
<testsuite name="Feature">
<directory>./tests/Feature</directory>
</testsuite>
Expand Down
11 changes: 0 additions & 11 deletions pint.json

This file was deleted.

33 changes: 2 additions & 31 deletions resources/config/sprout.php → resources/config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,8 @@
\Sprout\Listeners\CleanupServiceOverrides::class,
// Sets up service overrides for the current tenancy
\Sprout\Listeners\SetupServiceOverrides::class,
],

/*
|--------------------------------------------------------------------------
| Service Overrides
|--------------------------------------------------------------------------
|
| This is an array of service override classes.
| These classes will be instantiated and automatically run when relevant.
|
*/

'services' => [
// This will override the storage by introducing a 'sprout' driver
// that wraps any other storage drive in a tenant resource subdirectory.
Services::STORAGE => \Sprout\Overrides\StorageOverride::class,
// This will hydrate tenants when running jobs, based on the current
// context.
Services::JOB => \Sprout\Overrides\JobOverride::class,
// This will override the cache by introducing a 'sprout' driver
// that adds a prefix to cache stores for the current tenant.
Services::CACHE => \Sprout\Overrides\CacheOverride::class,
// This is a simple override that removes all currently resolved
// guards to prevent user auth leaking.
Services::AUTH => \Sprout\Overrides\AuthOverride::class,
// This will override the cookie settings so that all created cookies
// are specific to the tenant.
Services::COOKIE => \Sprout\Overrides\CookieOverride::class,
// This will override the session by introducing a 'sprout' driver
// that wraps any other session store.
Services::SESSION => \Sprout\Overrides\SessionOverride::class,
// Refresh anything that's tenant-aware
\Sprout\Listeners\RefreshTenantAwareDependencies::class,
],

];
1 change: 1 addition & 0 deletions resources/config/multitenancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'options' => [
TenancyOptions::hydrateTenantRelation(),
TenancyOptions::throwIfNotRelated(),
TenancyOptions::allOverrides(),
],
],

Expand Down
50 changes: 50 additions & 0 deletions resources/config/overrides.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
|--------------------------------------------------------------------------
| Service Overrides
|--------------------------------------------------------------------------
|
| This config file provides the config for the different service overrides
| registered by Sprout.
| Service overrides are registered against a "service", which is an arbitrary
| string value, used to prevent multiple overrides for a single service.
|
| All services overrides should have a "driver" which should contain an FQN
| for a class that implements the ServiceOverride interface.
| Any other config options will depend on the individual service override
| driver.
|
*/

return [

'filesystem' => [
'driver' => \Sprout\Overrides\FilesystemOverride::class,
// This config option defines whether the filesystem override will
// override the filesystem manager with a Sprout version.
// The default value is 'true'
'manager' => true,
],

'job' => [
'driver' => \Sprout\Overrides\JobOverride::class,
],

'cache' => [
'driver' => \Sprout\Overrides\CacheOverride::class,
],

'auth' => [
'driver' => \Sprout\Overrides\AuthOverride::class,
],

'cookie' => [
'driver' => \Sprout\Overrides\CookieOverride::class,
],

'session' => [
'driver' => \Sprout\Overrides\SessionOverride::class,
'database' => false,
],
];
102 changes: 102 additions & 0 deletions src/Concerns/AwareOfTenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);

namespace Sprout\Concerns;

use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;

/**
* @phpstan-require-implements \Sprout\Contracts\TenantAware
*/
trait AwareOfTenant
{
/**
* @var \Sprout\Contracts\Tenant|null
*/
private ?Tenant $tenant;

/**
* @var \Sprout\Contracts\Tenancy<*>|null
*/
private ?Tenancy $tenancy;

/**
* Should the tenancy and tenant be refreshed when they change?
*
* @return bool
*/
public function shouldBeRefreshed(): bool
{
return true; // @codeCoverageIgnore
}

/**
* Get the tenant if there is one
*
* @return \Sprout\Contracts\Tenant|null
*/
public function getTenant(): ?Tenant
{
return $this->tenant ?? null;
}

/**
* Check if there is a tenant
*
* @return bool
*/
public function hasTenant(): bool
{
return $this->getTenant() !== null;
}

/**
* Set the tenant
*
* @param \Sprout\Contracts\Tenant|null $tenant
*
* @return static
*/
public function setTenant(?Tenant $tenant): static
{
$this->tenant = $tenant;

return $this;
}

/**
* Get the tenancy if there is one
*
* @return \Sprout\Contracts\Tenancy<*>|null
*/
public function getTenancy(): ?Tenancy
{
return $this->tenancy ?? null;
}

/**
* Check if there is a tenancy
*
* @return bool
*/
public function hasTenancy(): bool
{
return $this->getTenancy() !== null;
}

/**
* Set the tenancy
*
* @template TenantClass of \Sprout\Contracts\Tenant
* @param \Sprout\Contracts\Tenancy<TenantClass>|null $tenancy
*
* @return static
*/
public function setTenancy(?Tenancy $tenancy): static
{
$this->tenancy = $tenancy;

return $this;
}
}
Loading

0 comments on commit 4b399db

Please sign in to comment.