Skip to content

Commit

Permalink
[5.x] Cache the isApiRoute results for the request (#9638)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
JohnathonKoster and jasonvarga authored Mar 13, 2024
1 parent 8fdaaff commit 7bb2bfd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/Listeners/ClearState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Statamic\Listeners;

use Statamic\Statamic;
use Statamic\View\State\StateManager;

class ClearState
{
public function handle()
{
Statamic::clearApiRouteCache();
StateManager::resetState();
}
}
4 changes: 2 additions & 2 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class EventServiceProvider extends ServiceProvider
\Statamic\Entries\AddSiteColumnToBlueprint::class,
],
\Statamic\Events\ResponseCreated::class => [
\Statamic\View\State\ClearState::class,
\Statamic\Listeners\ClearState::class,
],
\Illuminate\Foundation\Http\Events\RequestHandled::class => [
\Statamic\View\State\ClearState::class,
\Statamic\Listeners\ClearState::class,
],
];

Expand Down
14 changes: 12 additions & 2 deletions src/Statamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Statamic
protected static $jsonVariables = [];
protected static $bootedCallbacks = [];
protected static $afterInstalledCallbacks = [];
private static $isApiRouteCache;

public static function version()
{
Expand Down Expand Up @@ -211,13 +212,22 @@ public static function cpRoute($route, $params = [])
return $route;
}

public static function clearApiRouteCache()
{
self::$isApiRouteCache = null;
}

public static function isApiRoute()
{
if (self::$isApiRouteCache !== null) {
return self::$isApiRouteCache;
}

if (! config('statamic.api.enabled') || ! static::pro()) {
return false;
return self::$isApiRouteCache = false;
}

return starts_with(request()->path(), config('statamic.api.route'));
return self::$isApiRouteCache = starts_with(request()->path(), config('statamic.api.route'));
}

public static function apiRoute($route, $params = [])
Expand Down
11 changes: 0 additions & 11 deletions src/View/State/ClearState.php

This file was deleted.

0 comments on commit 7bb2bfd

Please sign in to comment.