From 69893389d625492fb53ed9909b8f843fe4c03919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Soler=20/=20Pigmali=C3=B3n=20Tseyor?= Date: Sun, 18 Feb 2024 08:57:45 +0100 Subject: [PATCH] Update Ziggy.php cache creation of data for better performance --- src/Ziggy.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Ziggy.php b/src/Ziggy.php index f2cdae75..c6d9d9af 100644 --- a/src/Ziggy.php +++ b/src/Ziggy.php @@ -26,8 +26,25 @@ public function __construct($group = null, string $url = null) $this->url = rtrim($url ?? url('/'), '/'); - if (! static::$cache) { - static::$cache = $this->nameKeyedRoutes(); + if (!static::$cache) { + // el archivo ziggy se guarda en cache, aquĆ­ se comprueba si debe reconstruirse + $cache_routes = base_path("bootstrap/cache/routes-v7.php"); + $cache_ziggy = base_path("bootstrap/cache/ziggy2.json"); + if ( + !file_exists($cache_ziggy) || + !file_exists($cache_routes) || + filemtime($cache_routes) > filemtime($cache_ziggy) + ) { + static::$cache = $this->nameKeyedRoutes(); + file_put_contents($cache_ziggy, static::$cache->toJson()); + } else { + try { + $ziggy_content = file_get_contents($cache_ziggy); + static::$cache = collect(json_decode($ziggy_content, true)); + } catch (\Exception $e) { + static::$cache = $this->nameKeyedRoutes(); // por si hubiera algun error + } + } } $this->routes = static::$cache;