Skip to content

Commit

Permalink
chore: Add core Sprout settings repository (#79)
Browse files Browse the repository at this point in the history
* chore: Add core Sprout settings repository

* refactor: Replace cookie value overrides with usage of settings repository

* refactor: Implement settings repository for overrides

* refactor: Add helper methods for settings

* fix: Fix database override setting
  • Loading branch information
ollieread authored Dec 16, 2024
1 parent 88ced30 commit 1b00334
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 111 deletions.
71 changes: 0 additions & 71 deletions src/Concerns/OverridesCookieSettings.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/Http/Resolvers/PathIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Sprout\Overrides\CookieOverride;
use Sprout\Overrides\SessionOverride;
use Sprout\Support\BaseIdentityResolver;
use Sprout\Support\Settings;
use function Sprout\settings;

/**
* Path Identity Resolver
Expand Down Expand Up @@ -178,8 +180,7 @@ public function setup(Tenancy $tenancy, ?Tenant $tenant): void
$this->parameterSetup($tenancy, $tenant);

if ($tenant !== null) {
CookieOverride::setPath($this->getTenantRoutePrefix($tenancy));
SessionOverride::setPath($this->getTenantRoutePrefix($tenancy));
settings()->setUrlPath($this->getTenantRoutePrefix($tenancy));
}
}
}
5 changes: 3 additions & 2 deletions src/Http/Resolvers/SubdomainIdentityResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Sprout\Overrides\CookieOverride;
use Sprout\Overrides\SessionOverride;
use Sprout\Support\BaseIdentityResolver;
use Sprout\Support\Settings;
use function Sprout\settings;

/**
* The Subdomain Identity Resolver
Expand Down Expand Up @@ -172,8 +174,7 @@ public function setup(Tenancy $tenancy, ?Tenant $tenant): void
$this->parameterSetup($tenancy, $tenant);

if ($tenant !== null) {
CookieOverride::setDomain($this->getTenantRouteDomain($tenancy));
SessionOverride::setDomain($this->getTenantRouteDomain($tenancy));
settings()->setUrlDomain($this->getTenantRouteDomain($tenancy));
}
}
}
12 changes: 5 additions & 7 deletions src/Overrides/CookieOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
namespace Sprout\Overrides;

use Illuminate\Cookie\CookieJar;
use Sprout\Concerns\OverridesCookieSettings;
use Sprout\Contracts\DeferrableServiceOverride;
use Sprout\Contracts\ServiceOverride;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use function Sprout\settings;

/**
* Cookie Override
Expand All @@ -20,8 +20,6 @@
*/
final class CookieOverride implements ServiceOverride, DeferrableServiceOverride
{
use OverridesCookieSettings;

/**
* Get the service to watch for before overriding
*
Expand All @@ -47,10 +45,10 @@ public static function service(): string
public function setup(Tenancy $tenancy, Tenant $tenant): void
{
// Collect the values
$path = self::$settings['path'] ?? config('session.path') ?? '/';
$domain = self::$settings['domain'] ?? config('session.domain');
$secure = self::$settings['secure'] ?? config('session.secure', false);
$sameSite = self::$settings['same_site'] ?? config('session.same_site');
$path = settings()->getUrlPath(config('session.path') ?? '/'); // @phpstan-ignore-line
$domain = settings()->getUrlDomain(config('session.domain')); // @phpstan-ignore-line
$secure = settings()->shouldCookieBeSecure(config('session.secure', false)); // @phpstan-ignore-line
$sameSite = settings()->shouldCookeBeSameSite(config('session.same_site')); // @phpstan-ignore-line

/**
* This is here to make PHPStan quiet down
Expand Down
53 changes: 29 additions & 24 deletions src/Overrides/SessionOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Session\DatabaseSessionHandler as OriginalDatabaseSessionHandler;
use Illuminate\Session\FileSessionHandler;
use Illuminate\Session\SessionManager;
use Sprout\Concerns\OverridesCookieSettings;
use Illuminate\Support\Arr;
use Sprout\Contracts\BootableServiceOverride;
use Sprout\Contracts\DeferrableServiceOverride;
use Sprout\Contracts\Tenancy;
Expand All @@ -19,6 +19,8 @@
use Sprout\Exceptions\TenantMissing;
use Sprout\Overrides\Session\TenantAwareDatabaseSessionHandler;
use Sprout\Sprout;
use Sprout\Support\Settings;
use function Sprout\settings;
use function Sprout\sprout;

/**
Expand All @@ -31,23 +33,6 @@
*/
final class SessionOverride implements BootableServiceOverride, DeferrableServiceOverride
{
use OverridesCookieSettings;

/**
* @var bool
*/
private static bool $overrideDatabase = true;

/**
* Prevent this override from overriding the database driver
*
* @return void
*/
public static function doNotOverrideDatabase(): void
{
self::$overrideDatabase = false;
}

/**
* Get the service to watch for before overriding
*
Expand Down Expand Up @@ -80,7 +65,7 @@ public function boot(Application $app, Sprout $sprout): void
$sessionManager->extend('file', $fileCreator);
$sessionManager->extend('native', $fileCreator);

if (self::$overrideDatabase) {
if (settings()->shouldNotOverrideTheDatabase(false) === false) {
$sessionManager->extend('database', self::createDatabaseDriver());
}
}
Expand All @@ -99,13 +84,33 @@ public function boot(Application $app, Sprout $sprout): void
*/
public function setup(Tenancy $tenancy, Tenant $tenant): void
{
$settings = self::$settings;

/** @var \Illuminate\Contracts\Config\Repository $config */
$config = config();
$config = config();
$settings = settings();

if (! $settings->has('original.session')) {
/** @var array<string, mixed> $original */
$original = $config->get('session');
$settings->set(
'original.session',
Arr::only($original, ['path', 'domain', 'secure', 'same_site'])
);
}

if ($settings->has(Settings::URL_PATH)) {
$config->set('session.path', $settings->getUrlPath());
}

if ($settings->has(Settings::URL_DOMAIN)) {
$config->set('session.domain', $settings->getUrlDomain());
}

if ($settings->has(Settings::COOKIE_SECURE)) {
$config->set('session.secure', $settings->shouldCookieBeSecure());
}

foreach ($settings as $setting => $value) {
$config->set('session.' . $setting, $value);
if ($settings->has(Settings::COOKIE_SAME_SITE)) {
$config->set('session.same_site', $settings->shouldCookeBeSameSite());
}

$config->set('session.cookie', $this->getCookieName($tenancy, $tenant));
Expand Down
37 changes: 34 additions & 3 deletions src/Sprout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Sprout\Managers\ProviderManager;
use Sprout\Managers\TenancyManager;
use Sprout\Support\ResolutionHook;
use Sprout\Support\SettingsRepository;

/**
* Sprout
Expand Down Expand Up @@ -38,14 +39,21 @@ final class Sprout
*/
private bool $withinContext = false;

/**
* @var \Sprout\Support\SettingsRepository
*/
private SettingsRepository $settings;

/**
* Create a new instance
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Sprout\Support\SettingsRepository $settings
*/
public function __construct(Application $app)
public function __construct(Application $app, SettingsRepository $settings)
{
$this->app = $app;
$this->app = $app;
$this->settings = $settings;
}

/**
Expand All @@ -63,6 +71,29 @@ public function config(string $key, mixed $default = null): mixed
return $this->app->make('config')->get('sprout.' . $key, $default);
}

/**
* Get a config item from the sprout config
*
* @param string $key
* @param mixed|null $default
*
* @return mixed
*/
public function setting(string $key, mixed $default = null): mixed
{
return $this->settings->get($key, $default);
}

/**
* Get the Sprout settings repository
*
* @return \Sprout\Support\SettingsRepository
*/
public function settings(): SettingsRepository
{
return $this->settings;
}

/**
* Set the current tenancy
*
Expand Down Expand Up @@ -222,7 +253,7 @@ public function withinContext(): bool
*
* If no tenancy name is provided, this method will use the current tenancy
* or the default one.
*
*
* If no resolver name is provided, this method will use the resolver
* currently linked with the tenancy, or the default one.
*
Expand Down
6 changes: 5 additions & 1 deletion src/SproutServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sprout\Managers\ProviderManager;
use Sprout\Managers\TenancyManager;
use Sprout\Support\ResolutionHook;
use Sprout\Support\SettingsRepository;

/**
* Sprout Service Provider
Expand All @@ -36,10 +37,13 @@ public function register(): void

private function registerSprout(): void
{
$this->sprout = new Sprout($this->app);
$this->sprout = new Sprout($this->app, new SettingsRepository());

$this->app->singleton(Sprout::class, fn () => $this->sprout);
$this->app->alias(Sprout::class, 'sprout');

// Bind the settings repository too
$this->app->bind(SettingsRepository::class, fn () => $this->sprout->settings());
}

private function registerManagers(): void
Expand Down
21 changes: 21 additions & 0 deletions src/Support/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Sprout\Support;

final class Settings
{
public const URL = 'url';

public const URL_PATH = self::URL . '.path';

public const URL_DOMAIN = self::URL . '.domain';

public const COOKIE = 'cookie';

public const COOKIE_SECURE = self::COOKIE . '.secure';

public const COOKIE_SAME_SITE = self::COOKIE . '.same_site';

public const NO_DATABASE_OVERRIDE = 'no-database-override';
}
Loading

0 comments on commit 1b00334

Please sign in to comment.