Skip to content

Commit

Permalink
Formatting menu helper and caching current page
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersai committed Jan 20, 2024
1 parent b701ae4 commit 1250739
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/Traits/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public function listing(

return $this;
}

public function format(string $format, array ...$values): static
{
$this->content .= sprintf($format, ...$values);

return $this;
}
}
16 changes: 8 additions & 8 deletions src/Traits/WithPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

trait WithPagination
{
private static $currentPage;
private static $cache = [];

private function lastPage(): int
{
Expand All @@ -17,17 +17,17 @@ private function lastPage(): int

public function currentPage(): int
{
if (isset(self::$currentPage)) {
return self::$currentPage;
$name = get_called_class();

if (isset(static::$cache[$name])) {
return static::$cache[$name];
}

/** @var Record */ $record = App::make(Record::class);
$pageId = Str::of(get_called_class())->replace('\\', '')->snake()->append('_page')->value();
$pageId = Str::of($name)->replace('\\', '')->snake()->append('_page')->value();
$page = $record->get($pageId, 1);

self::$currentPage = $page;

return $page;
return static::$cache[$name] = $page;
}

public function isFirstPage(): int
Expand Down Expand Up @@ -56,6 +56,6 @@ abstract public function perPage(): int;

public function __destruct()
{
self::$currentPage = null;
static::$cache = [];
}
}

0 comments on commit 1250739

Please sign in to comment.