Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(overrides): Refactor service overrides to allow for more complexity #89

Merged
merged 48 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
b79425f
chore: Add new overrides config file
ollieread Jan 11, 2025
b6e120d
feat(tenancies): Introduce tenancy option config
ollieread Jan 11, 2025
b0cd9b4
chore: Add overrides tenancy option with sensible default
ollieread Jan 11, 2025
084c9e8
chore: Add generics to setup and cleanup on ServiceOverride
ollieread Jan 11, 2025
801ee12
refactor: Add a constructor to the ServiceOverride interface
ollieread Jan 11, 2025
0542302
chore: Add config publishing for new overrides config file
ollieread Jan 11, 2025
8b76886
feat(overrides): Add new service override manager with base functiona…
ollieread Jan 11, 2025
a449b36
chore: Add override.all tenancy option
ollieread Jan 11, 2025
5561834
chore: Remove services config from sprout config
ollieread Jan 11, 2025
75c399a
chore: Add manager to option to override filesystem manager to overri…
ollieread Jan 11, 2025
4973ed3
chore: Updated compatibility check on session and cookie resolvers
ollieread Jan 11, 2025
afe56af
refactor: Update service overrides to use new approach
ollieread Jan 11, 2025
0e253be
chore: Remove HandlesServiceOverrides and DeferrableServiceOverride
ollieread Jan 11, 2025
b29bc79
chore: Tidy up service override manager
ollieread Jan 11, 2025
f889526
chore: Remove old unused tests
ollieread Jan 11, 2025
89c4fdc
chore: Default tenancy to have all overrides
ollieread Jan 11, 2025
1a95763
fix: Initialise tenancy options with an empty array to prevent errors
ollieread Jan 11, 2025
3805dbd
fix: Change overrides config file location because Laravel no longer …
ollieread Jan 11, 2025
07ac441
fix: Add missing negation that was breaking everything
ollieread Jan 11, 2025
0fdd54c
chore: Updates for new config location
ollieread Jan 11, 2025
85fe0c0
tests: Update tests for changes
ollieread Jan 11, 2025
98d621f
chore: Migrate the core sprout config to sprout/core
ollieread Jan 11, 2025
e18651b
fix: Update to understand new config structure
ollieread Jan 11, 2025
3fb191a
chore: Renaming and tidying up of service override components
ollieread Jan 11, 2025
78ae84c
test(overrides): Added tests for the new service override manager
ollieread Jan 11, 2025
e1b8415
test(resolvers): Add compatibility exception cases to cookie and sess…
ollieread Jan 12, 2025
dc781de
test(overrides): Fix skipped tests for auth and session service overr…
ollieread Jan 12, 2025
90d1951
chore: Ignore service override exceptions from code coverage
ollieread Jan 12, 2025
b84b386
feat: Add method for checking whether a tenancy has had their service…
ollieread Jan 12, 2025
4be33e1
fix: Fix two issues with the cache override
ollieread Jan 12, 2025
ee1d04e
fix: Fix two issues with the filesystem override
ollieread Jan 12, 2025
3ac2257
test(overrides): Add cache override test
ollieread Jan 12, 2025
479c530
chore: Add tenant aware contract and default trait implementation
ollieread Jan 22, 2025
a39c053
chore: Add functionality to refresh tenant-aware dependencies when th…
ollieread Jan 22, 2025
1623606
refactor: Update session handler overrides to be persistent and tenan…
ollieread Jan 22, 2025
20d5bcc
chore: Make override filesystem manager aware of whether it override …
ollieread Jan 22, 2025
b1ea583
chore: Single for tenancy generic with phpstan and tenant-aware imple…
ollieread Jan 22, 2025
7ad7dbf
test(overrides): Completed the tests of the session service override
ollieread Jan 23, 2025
1ae9a62
test(overrides): A few QOL fixes for the session override tests
ollieread Jan 23, 2025
0a3f0bb
test(overrides): Fix database session handler test to account for upd…
ollieread Jan 23, 2025
6df3056
chore(overrides): Fix static analysis issue with overridden DatabaseS…
ollieread Jan 23, 2025
f376150
test(overrides): Further tests for auth and session overrides
ollieread Jan 24, 2025
e60762c
test(overrides): Add more tests for auth and adjust existing ones
ollieread Jan 24, 2025
291c153
chore: Remove unneeded phpstan error silencing
ollieread Jan 24, 2025
b3dda9c
test(overrides): Added a test for the filesystem manager
ollieread Jan 25, 2025
2561469
test(overrides): Complete the service override tests
ollieread Jan 25, 2025
6919c7a
chore: Remove Laravel pint config
ollieread Jan 25, 2025
72ecd65
chore: Ignore ServiceOverrideManager::hasTenancyBeenSetup for code co…
ollieread Jan 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -78,7 +79,7 @@
"providers": [
"Sprout\\SproutServiceProvider"
],
"facades":[
"facades" : [
"Sprout\\Facades\\Sprout"
]
}
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