Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Improved cache rules for parsing cursor_queue_names
Browse files Browse the repository at this point in the history
  • Loading branch information
juampi92 committed Mar 9, 2018
1 parent e3d469b commit 0b61b59
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
36 changes: 22 additions & 14 deletions src/CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class CursorPaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, PaginatorContract
{
private static $queue_names_cache;
/**
* Determine if there are more items in the data source.
*
Expand Down Expand Up @@ -45,6 +44,11 @@ class CursorPaginator extends AbstractPaginator implements Arrayable, ArrayAcces
*/
protected $cursor = null;

/**
* @var array|null
*/
protected $cursor_queue_names = null;

/**
* Create a new paginator instance.
*
Expand Down Expand Up @@ -105,25 +109,29 @@ public static function resolveCurrentCursor(Request $request = null)
}

/**
* @param bool $force (Don't use cache)
*
* @return array
*/
public static function cursorQueryNames($force = false)
public static function cursorQueryNames()
{
if (!$force && isset(static::$queue_names_cache)) {
return static::$queue_names_cache;
}

$ident = config('cursor_pagination.identifier_name');
list($prev, $next) = config('cursor_pagination.navigation_names');

static::$queue_names_cache = [
return [
self::formatNames("{$prev}_$ident"),
self::formatNames("{$next}_$ident"),
];
}

/**
* @return array
*/
public function getCursorQueryNames()
{
if (is_null($this->cursor_queue_names)) {
$this->cursor_queue_names = static::cursorQueryNames();
}

return static::$queue_names_cache;
return $this->cursor_queue_names;
}

protected static function formatNames($name)
Expand All @@ -149,7 +157,7 @@ public function nextCursor()
*/
public function nextPageUrl()
{
list($prev, $next) = self::cursorQueryNames();
list($prev, $next) = $this->getCursorQueryNames();

if ($this->nextCursor()) {
$query = [
Expand Down Expand Up @@ -181,7 +189,7 @@ public function prevCursor()
*/
public function previousPageUrl()
{
list($prev) = self::cursorQueryNames();
list($prev) = $this->getCursorQueryNames();

if ($pre_cursor = $this->prevCursor()) {
return $this->url([
Expand All @@ -197,7 +205,7 @@ public function previousPageUrl()
*/
protected function getRawQuery()
{
list($prev, $next) = self::cursorQueryNames();
list($prev, $next) = $this->getCursorQueryNames();

return collect($this->request->query())
->diffKeys([
Expand Down Expand Up @@ -302,7 +310,7 @@ public function render($view = null, $data = [])
*/
public function toArray()
{
list($prev, $next) = self::cursorQueryNames();
list($prev, $next) = $this->getCursorQueryNames();

return [
'data' => $this->items->toArray(),
Expand Down
5 changes: 2 additions & 3 deletions tests/CursorNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ class CursorNameTest extends TestCase
public function test_camel_case()
{
config(['cursor_pagination.camel_case' => false]);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertContains('_', $prev_name);
$this->assertContains('_', $next_name);

config(['cursor_pagination.camel_case' => true]);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertNotContains('_', $prev_name);
$this->assertNotContains('_', $next_name);

config(['cursor_pagination.camel_case' => false]);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
}
}
2 changes: 1 addition & 1 deletion tests/CursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function test_resolves_next_cursor()

public function test_both_cursor()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$prev_val = 3;
$next_val = 1;
Expand Down
8 changes: 4 additions & 4 deletions tests/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function test_on_query_builder_with_identifier()

public function test_perPage_dynamic_prev()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$p = User::cursorPaginate([$prev_count = 2, $next_count = 4], ['*'], [
'request' => $req = new Request([
Expand All @@ -64,7 +64,7 @@ public function test_perPage_dynamic_prev()

public function test_perPage_dynamic_empty_cursor_defaults_next()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$p = User::cursorPaginate([$prev_count = 2, $next_count = 4], ['*'], [
'request' => $req = new Request(),
Expand All @@ -77,7 +77,7 @@ public function test_perPage_dynamic_empty_cursor_defaults_next()

public function test_perPage_dynamic_next()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$p = User::cursorPaginate([$prev_count = 2, $next_count = 4], ['*'], [
'request' => $req = new Request([
Expand All @@ -92,7 +92,7 @@ public function test_perPage_dynamic_next()

public function test_both_pagination()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$p = User::cursorPaginate(10, ['*'], [
'request' => $req = new Request([
Expand Down
16 changes: 8 additions & 8 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RequestTest extends ModelsTestCase
/** @test */
public function test_a()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$response = $this->get('/test/one/?asd=1');

Expand All @@ -20,7 +20,7 @@ public function test_a()
/** @test */
public function test_resource()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$prev_cur = 1;
$next_cur = 5;

Expand All @@ -46,7 +46,7 @@ public function test_resource()
/** @test */
public function test_prev_nav()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$prev_cur = 1;

$response = $this->get("/test/resource?$prev_name=$prev_cur");
Expand All @@ -70,7 +70,7 @@ public function test_prev_nav()

public function test_more_middle_pagination()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$prev_cur = 10;
$prev_cur_added = $prev_cur - 5;
$next_cur = 0;
Expand Down Expand Up @@ -101,7 +101,7 @@ public function test_more_middle_pagination()

public function test_prev_is_still_present()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$prev_cur = 10;
$next_cur = 2;
$next_cur_after = $next_cur + 5;
Expand Down Expand Up @@ -132,7 +132,7 @@ public function test_prev_is_still_present()
/** @test */
public function test_surroundings_finished()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$prev_cur = 10;
$next_cur = 7;

Expand Down Expand Up @@ -162,7 +162,7 @@ public function test_surroundings_finished()
/** @test */
public function test_inverted_order()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$next_cur = 36;

$response = $this->get("/test/inverse?$next_name=$next_cur");
Expand All @@ -180,7 +180,7 @@ public function test_inverted_order()

public function test_on_query()
{
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();
$next_cur = 36;

$response = $this->get("/test/query_inverse?$next_name=$next_cur");
Expand Down
12 changes: 6 additions & 6 deletions tests/UrlDetectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function test_resolves_urls_on_no_cursor()
$this->assertTrue($p->isFirstPage());
$this->assertEquals($p->prevCursor(), 1);

list($prev_name, $next_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name, $next_name) = CursorPaginator::cursorQueryNames();

$this->assertEquals($p->previousPageUrl(), "$path?$prev_name=$firstId");
$this->assertEquals($p->nextPageUrl(), "$path?$next_name=$lastId");
}

public function test_resolves_no_prev_on_cursor()
{
list(, $next_name) = CursorPaginator::cursorQueryNames(true);
list(, $next_name) = CursorPaginator::cursorQueryNames();

$p = new CursorPaginator($array = [
(object) ['id' => 2],
Expand All @@ -56,7 +56,7 @@ public function test_resolves_no_prev_on_cursor()

public function test_resolves_with_other_query()
{
list(, $next_name) = CursorPaginator::cursorQueryNames(true);
list(, $next_name) = CursorPaginator::cursorQueryNames();

$p = new CursorPaginator($array = [
(object) ['id' => 2],
Expand All @@ -77,7 +77,7 @@ public function test_resolves_with_other_query()

public function test_resolves_prev_intact_if_no_elements()
{
list($prev_name) = CursorPaginator::cursorQueryNames(true);
list($prev_name) = CursorPaginator::cursorQueryNames();

$p = new CursorPaginator($array = [], $perPage = 2, [
'request' => new Request([
Expand All @@ -91,7 +91,7 @@ public function test_resolves_prev_intact_if_no_elements()

public function test_stops_when_no_more_items()
{
list(, $next_name) = CursorPaginator::cursorQueryNames(true);
list(, $next_name) = CursorPaginator::cursorQueryNames();

$p = new CursorPaginator($array = [
(object) ['id' => 98],
Expand All @@ -110,7 +110,7 @@ public function test_stops_when_no_more_items()

public function test_different_identifier()
{
CursorPaginator::cursorQueryNames(true);
CursorPaginator::cursorQueryNames();

$p = new CursorPaginator($array = [
(object) ['id' => 98, '_id' => 1],
Expand Down

0 comments on commit 0b61b59

Please sign in to comment.