Skip to content

Commit 458ef47

Browse files
committed
Remove session stuff for global filters
1 parent 61fe910 commit 458ef47

11 files changed

Lines changed: 60 additions & 70 deletions

File tree

src/Filters/GlobalFilters/GlobalFilters.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@
66
use Code16\Sharp\Filters\Filter;
77
use Code16\Sharp\Filters\GlobalRequiredFilter;
88
use Illuminate\Contracts\Support\Arrayable;
9+
use Illuminate\Support\Collection;
910

1011
final class GlobalFilters implements Arrayable
1112
{
1213
use HasFilters;
1314

1415
public static string $defaultKey = 'root';
1516
public static string $valuesUrlSeparator = '~';
17+
private ?Collection $globalFilters = null;
1618

1719
public function getFilters(): array
1820
{
19-
return collect(sharp()->config()->get('global_filters'))
20-
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize())
21-
->all();
21+
if ($this->globalFilters === null) {
22+
$this->globalFilters = collect(sharp()->config()->get('global_filters'))
23+
->filter(fn (GlobalRequiredFilter $filter) => $filter->authorize())
24+
->values();
25+
}
26+
27+
return $this->globalFilters->all();
2228
}
2329

2430
public function isEnabled(): bool
@@ -35,15 +41,11 @@ public function toArray(): array
3541
'filterValues' => [
3642
'default' => $this->filterContainer()->getFilterHandlers()
3743
->flatten()
38-
->mapWithKeys(function (Filter $handler) {
39-
return [$handler->getKey() => $handler->defaultValue()];
40-
})
44+
->mapWithKeys(fn (Filter $handler) => [$handler->getKey() => $handler->defaultValue()])
4145
->toArray(),
4246
'current' => $this->filterContainer()->getFilterHandlers()
4347
->flatten()
44-
->mapWithKeys(function (Filter $handler) {
45-
return [$handler->getKey() => $handler->currentValue()];
46-
})
48+
->mapWithKeys(fn (Filter $handler) => [$handler->getKey() => $handler->currentValue()])
4749
->toArray(),
4850
'valuated' => [], // not needed here
4951
],

src/Filters/GlobalRequiredFilter.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66

77
abstract class GlobalRequiredFilter extends SelectRequiredFilter
88
{
9+
private mixed $currentValue = null;
10+
911
final public function currentValue(): mixed
1012
{
11-
$value = session()->get($this->getSessionKey());
12-
13-
if ($value === null) {
14-
if (($value = $this->defaultValue()) !== null) {
15-
session()->put($this->getSessionKey(), $value);
16-
}
17-
}
18-
19-
return $value;
13+
return $this->currentValue !== null
14+
? $this->currentValue
15+
: $this->defaultValue();
2016
}
2117

22-
final public function setCurrentValue(mixed $value): void
18+
final public function setCurrentValue(mixed $value, bool $throwIfInvalid = true): void
2319
{
2420
if ($value === null) {
25-
session()->forget($this->getSessionKey());
21+
$this->currentValue = null;
2622

2723
return;
2824
}
@@ -31,16 +27,11 @@ final public function setCurrentValue(mixed $value): void
3127
->where('id', $value)
3228
->first();
3329

34-
if (! $formattedValue) {
30+
if ($throwIfInvalid && ! $formattedValue) {
3531
throw new SharpInvalidFilterValueException('['.$value.'] is not a valid value for this filter.');
3632
}
3733

38-
session()->put($this->getSessionKey(), $formattedValue['id']);
39-
}
40-
41-
private function getSessionKey(): string
42-
{
43-
return '_sharp_retained_global_filter_'.$this->getKey();
34+
$this->currentValue = $formattedValue['id'] ?? null;
4435
}
4536

4637
public function authorize(): bool

src/Http/Context/SharpBreadcrumb.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ protected function getSegmentsFromRequest(): Collection
314314

315315
return collect(explode('/', parse_url($urlToParse)['path']))
316316
->filter(fn (string $segment) => strlen(trim($segment)))
317+
// Have to skip /sharp/{filterKey}
317318
->skip(2)
318319
->values();
319320
}

src/Http/Context/SharpContext.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class SharpContext
1212
private Collection $cachedListInstances;
1313
private SharpBreadcrumb $breadcrumb;
1414

15+
public function __construct(private GlobalFilters $globalFiltersHandler) {}
16+
1517
public function globalFilterValue(string $handlerClassOrKey): array|string|null
1618
{
17-
$handler = class_exists($handlerClassOrKey)
18-
? app($handlerClassOrKey)
19-
: app(GlobalFilters::class)->findFilter($handlerClassOrKey);
19+
$handler = $this->globalFiltersHandler->findFilter($handlerClassOrKey);
2020

2121
abort_if(! $handler instanceof GlobalRequiredFilter, 404);
2222

@@ -25,13 +25,8 @@ public function globalFilterValue(string $handlerClassOrKey): array|string|null
2525

2626
public function globalFilterUrlSegmentValue(): string
2727
{
28-
return collect(sharp()->config()->get('global_filters'))
29-
->map(fn ($globalFilterClassOrInstance) => is_string($globalFilterClassOrInstance)
30-
? app($globalFilterClassOrInstance)
31-
: $globalFilterClassOrInstance
32-
)
28+
return collect($this->globalFiltersHandler->getFilters())
3329
->map(fn (GlobalRequiredFilter $globalFilter) => $globalFilter->currentValue())
34-
->filter()
3530
->implode(GlobalFilters::$valuesUrlSeparator) ?: GlobalFilters::$defaultKey;
3631
}
3732

src/Http/Controllers/GlobalFilterController.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Code16\Sharp\Http\Controllers;
44

5-
use Code16\Sharp\Exceptions\SharpInvalidFilterValueException;
65
use Code16\Sharp\Filters\GlobalFilters\GlobalFilters;
76
use Code16\Sharp\Filters\GlobalRequiredFilter;
87
use Illuminate\Http\RedirectResponse;
@@ -15,12 +14,10 @@ public function update(string $filterKey, GlobalFilters $globalFilters): Redirec
1514

1615
abort_if(! $handler instanceof GlobalRequiredFilter, 404);
1716

18-
try {
19-
$handler->setCurrentValue(request('value'));
20-
} catch (SharpInvalidFilterValueException) {
21-
// Reset global filter to its previous value instead of showing an error
22-
}
17+
$handler->setCurrentValue(request('value'), throwIfInvalid: false);
2318

24-
return redirect()->route('code16.sharp.home');
19+
return redirect()->route('code16.sharp.home', [
20+
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
21+
]);
2522
}
2623
}

src/Http/Controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class HomeController extends SharpProtectedController
1010
{
11-
public function index()
11+
public function index(string $filterKey)
1212
{
1313
if ($firstEntityUrl = $this->getFirstConfiguredEntityUrl()) {
1414
return redirect()->to($firstEntityUrl);

src/Http/Middleware/HandleGlobalFilters.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,26 @@
1010

1111
class HandleGlobalFilters
1212
{
13+
public function __construct(private GlobalFilters $globalFiltersHandler) {}
14+
1315
public function handle(Request $request, Closure $next)
1416
{
15-
URL::defaults(['filterKey' => sharp()->context()->globalFilterUrlSegmentValue()]);
16-
1717
if ($request->isMethod('GET') && ($filterKey = $request->route('filterKey'))) {
1818
$filterKeys = explode(GlobalFilters::$valuesUrlSeparator, $filterKey);
19-
$configuredGlobalFilters = collect(sharp()->config()->get('global_filters'))
20-
->map(fn ($globalFilterClassOrInstance) => is_string($globalFilterClassOrInstance)
21-
? app($globalFilterClassOrInstance)
22-
: $globalFilterClassOrInstance
23-
);
2419

25-
if (count($configuredGlobalFilters) != 0 && count($filterKeys) != count($configuredGlobalFilters)) {
20+
if ($this->globalFiltersHandler->isEnabled()
21+
&& count($filterKeys) != count($this->globalFiltersHandler->getFilters())) {
2622
return redirect()->route('code16.sharp.home');
2723
}
2824

29-
$configuredGlobalFilters
30-
->each(fn (GlobalRequiredFilter $globalFilter, $index) => $globalFilter
25+
collect($this->globalFiltersHandler->getFilters())
26+
->each(fn (GlobalRequiredFilter $globalFilter, int $index) => $globalFilter
3127
->setCurrentValue($filterKeys[$index])
3228
);
3329
}
3430

31+
URL::defaults(['filterKey' => sharp()->context()->globalFilterUrlSegmentValue()]);
32+
3533
return $next($request);
3634
}
3735
}

src/Utils/SharpUtil.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88

99
class SharpUtil
1010
{
11-
protected SharpContext $context;
12-
13-
public function __construct()
14-
{
15-
$this->context = new SharpContext();
16-
}
11+
public function __construct(protected SharpContext $context) {}
1712

1813
public function version(): string
1914
{

src/routes/web.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
'middleware' => ['sharp_common', 'sharp_web'],
1919
], function () {
2020
// Redirect GET routes without filterKey
21+
Route::get('/', fn () => redirect(
22+
route('code16.sharp.home', [
23+
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
24+
])
25+
));
2126
Route::get('s-dashboard/{dashboardKey}', fn ($entityKey) => redirect(
2227
route('code16.sharp.dashboard', [
2328
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
@@ -37,7 +42,7 @@
3742
])
3843
));
3944

40-
Route::get('/', [HomeController::class, 'index'])
45+
Route::get('/home/{filterKey}', [HomeController::class, 'index'])
4146
->name('code16.sharp.home');
4247

4348
Route::get('{filterKey}/s-dashboard/{dashboardKey}', [DashboardController::class, 'show'])

tests/Http/Auth/ImpersonateTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public function getUsers(): array
3030
}
3131
});
3232

33-
$this->get(route('code16.sharp.home'))
34-
->assertRedirect(route('code16.sharp.impersonate'));
33+
$this
34+
->followingRedirects()
35+
->get('/sharp')
36+
->assertInertia(fn (Assert $page) => $page->component('Auth/Impersonate'));
3537
});
3638

3739
it('redirects to impersonation page if authenticated as non admin user', function () {
@@ -52,9 +54,10 @@ public function getUsers(): array
5254
});
5355

5456
$this
57+
->followingRedirects()
5558
->actingAs(User::create(['id' => 1, 'name' => 'Marie Curie']))
56-
->get(route('code16.sharp.home'))
57-
->assertRedirect(route('code16.sharp.impersonate'));
59+
->get('/sharp')
60+
->assertInertia(fn (Assert $page) => $page->component('Auth/Impersonate'));
5861
});
5962

6063
it('displays impersonatable users from a custom handler', function () {

0 commit comments

Comments
 (0)