From a6e7cdb37da916af798097dd339182031c1c90e8 Mon Sep 17 00:00:00 2001 From: Eder Soares Date: Mon, 9 Sep 2024 09:57:58 -0300 Subject: [PATCH] Adds cache to proxy type --- src/FrontendProxyController.php | 14 +++++++++++++- src/Frontier.php | 8 ++++++++ tests/FrontendProxyControllerTest.php | 26 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/FrontendProxyController.php b/src/FrontendProxyController.php index 41a9775..0cca8c2 100644 --- a/src/FrontendProxyController.php +++ b/src/FrontendProxyController.php @@ -16,8 +16,16 @@ public function __construct(protected Request $request) public function __invoke($uri, $config): Response { + $method = $this->request->getMethod(); $accept = $this->request->header('accept', '*/*'); $url = trim($config['url'], '/') . '/' . trim($uri, '/'); + $cacheKey = str($config['url'])->replace(['/', '.'], '-')->value(); + + $path = storage_path("framework/views/frontier-$method-$cacheKey"); + + if ($method === 'GET' && $config['cache'] && file_exists($path)) { + return new Response(file_get_contents($path)); + } if ($config['rewrite']) { $url = str_replace( @@ -31,7 +39,7 @@ public function __invoke($uri, $config): Response 'Accept' => $accept, ]); - $response = match ($this->request->getMethod()) { + $response = match ($method) { 'GET' => $http->get($url), 'HEAD' => $http->head($url), 'POST' => $http->post($url, $this->request->all()), @@ -51,6 +59,10 @@ public function __invoke($uri, $config): Response ); } + if ($config['cache']) { + file_put_contents($path, $content); + } + return new Response($content, headers: [ 'content-type' => $contextType, ]); diff --git a/src/Frontier.php b/src/Frontier.php index 205f0a0..089e0b6 100644 --- a/src/Frontier.php +++ b/src/Frontier.php @@ -45,9 +45,16 @@ private static function proxy(array $config): void $methods = []; $replaces = []; $rewrite = []; + $cache = false; $proxyAll = true; foreach ($segments as $segment) { + $cache = false; + + if ($segment === 'cache') { + $cache = true; + } + if ($segment === 'exact') { $proxyAll = false; } @@ -99,6 +106,7 @@ private static function proxy(array $config): void 'replaces' => $replaces, 'rewrite' => $rewrite, 'methods' => $methods, + 'cache' => $cache, ], ]); } diff --git a/tests/FrontendProxyControllerTest.php b/tests/FrontendProxyControllerTest.php index 6ed835c..5009e5b 100644 --- a/tests/FrontendProxyControllerTest.php +++ b/tests/FrontendProxyControllerTest.php @@ -20,6 +20,7 @@ '/all::replace(Replace,is amazing!)', '/web', '/all-methods::methods(get,head,options,post,put,patch,delete)', + '/with-cache::cache', ], ])); @@ -163,3 +164,28 @@ Http::assertSent(fn (Request $request) => $request->method() === 'DELETE'); }); + +test('proxy and do cache', function () { + $file = storage_path('framework/views/frontier-GET-frontier-test-with-cache'); + $text = 'Frontier by HTTP'; + + Http::fake([ + 'frontier.test/*' => Http::response($text), + ]); + + $this->assertFileDoesNotExist($file); + + $this->get('/with-cache') + ->assertStatus(200) + ->assertSeeText($text); + + $this->assertFileExists($file); + $this->assertStringEqualsFile($file, $text); + + $this->get('/with-cache') + ->assertStatus(200) + ->assertSeeText($text); + + // Remove cache + $this->artisan('view:clear'); +});