Skip to content

Commit

Permalink
Update Ziggy.php
Browse files Browse the repository at this point in the history
cache creation of data for better performance
  • Loading branch information
msoler75 authored Feb 18, 2024
1 parent 7963d8d commit 6989338
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6989338

Please sign in to comment.