From 9d78ca345aa9972cd36842995b8903767ab7559c Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Fri, 25 Feb 2022 14:14:44 -0600 Subject: [PATCH 1/7] Override withoutGlobalScopes --- src/CacheKey.php | 12 +++++++++++- src/Traits/BuilderCaching.php | 12 ++++++++++++ src/Traits/Caching.php | 4 +++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index ef9aaf2..5aaeeaa 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -16,17 +16,23 @@ class CacheKey protected $macroKey; protected $model; protected $query; + protected $withoutScopes = []; + protected $withoutAllScopes = false; public function __construct( array $eagerLoad, $model, $query, - $macroKey + $macroKey, + $withoutScopes, + $withoutAllScopes ) { $this->eagerLoad = $eagerLoad; $this->macroKey = $macroKey; $this->model = $model; $this->query = $query; + $this->withoutScopes = $withoutScopes; + $this->withoutAllScopes = $withoutAllScopes; } public function make( @@ -57,6 +63,10 @@ protected function getBindingsSlug() : string return ''; } + if ($this->withoutAllScopes || ($this->withoutScopes != null && count($this->withoutScopes) == 0)) { + return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutScopes)->getBindings()); + } + return Arr::query($this->model->query()->getBindings()); } diff --git a/src/Traits/BuilderCaching.php b/src/Traits/BuilderCaching.php index 7ef35c1..d5b595c 100644 --- a/src/Traits/BuilderCaching.php +++ b/src/Traits/BuilderCaching.php @@ -21,4 +21,16 @@ public function truncate() return parent::truncate(); } + + public function withoutGlobalScopes(array $scopes = null) + { + $this->scopesAreApplied = true; + $this->withoutScopes = $scopes; + + if ($scopes == null || ($scopes != null && count($scopes) == 0)) { + $this->withoutAllScopes = true; + } + + return parent::withoutGlobalScopes($scopes); + } } diff --git a/src/Traits/Caching.php b/src/Traits/Caching.php index e061b83..41ff2b6 100644 --- a/src/Traits/Caching.php +++ b/src/Traits/Caching.php @@ -16,6 +16,8 @@ trait Caching protected $isCachable = true; protected $scopesAreApplied = false; protected $macroKey = ""; + protected $withoutScopes = null; + protected $withoutAllScopes = false; public function __call($method, $parameters) { @@ -166,7 +168,7 @@ protected function makeCacheKey( $query = $this->query->getQuery(); } - return (new CacheKey($eagerLoad, $model, $query, $this->macroKey)) + return (new CacheKey($eagerLoad, $model, $query, $this->macroKey, $this->withoutScopes, $this->withoutAllScopes)) ->make($columns, $idColumn, $keyDifferentiator); } From a28c0bf2e7fc65cb2fb3b0827e1ff5b9843e7b5b Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Mon, 28 Feb 2022 11:30:41 -0600 Subject: [PATCH 2/7] Move withoutGlobalScopes --- src/Traits/BuilderCaching.php | 12 ------------ src/Traits/Caching.php | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Traits/BuilderCaching.php b/src/Traits/BuilderCaching.php index d5b595c..7ef35c1 100644 --- a/src/Traits/BuilderCaching.php +++ b/src/Traits/BuilderCaching.php @@ -21,16 +21,4 @@ public function truncate() return parent::truncate(); } - - public function withoutGlobalScopes(array $scopes = null) - { - $this->scopesAreApplied = true; - $this->withoutScopes = $scopes; - - if ($scopes == null || ($scopes != null && count($scopes) == 0)) { - $this->withoutAllScopes = true; - } - - return parent::withoutGlobalScopes($scopes); - } } diff --git a/src/Traits/Caching.php b/src/Traits/Caching.php index 41ff2b6..5e7800a 100644 --- a/src/Traits/Caching.php +++ b/src/Traits/Caching.php @@ -43,6 +43,31 @@ public function applyScopes() return parent::applyScopes(); } + public function withoutGlobalScope($scope) + { + $this->scopesAreApplied = true; + if ($this->withoutScopes == null) { + $this->withoutScopes = []; + } + + array_push($this->withoutScopes[], $scope); + $this->withoutAllScopes = false; + + return parent::withoutGlobalScope($scope); + } + + public function withoutGlobalScopes(array $scopes = null) + { + $this->scopesAreApplied = true; + $this->withoutScopes = $scopes; + + if ($scopes == null || ($scopes != null && count($scopes) == 0)) { + $this->withoutAllScopes = true; + } + + return parent::withoutGlobalScopes($scopes); + } + protected function applyScopesToInstance() { if (! property_exists($this, "scopes") From c6e87852218360d204fa8b26241d5e863ddcc9f3 Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Tue, 1 Mar 2022 13:26:57 -0600 Subject: [PATCH 3/7] Move withoutGlobalScopes to BuilderCaching --- src/Traits/BuilderCaching.php | 23 +++++++++++++++++++++++ src/Traits/Caching.php | 28 ++-------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/Traits/BuilderCaching.php b/src/Traits/BuilderCaching.php index 7ef35c1..f78d147 100644 --- a/src/Traits/BuilderCaching.php +++ b/src/Traits/BuilderCaching.php @@ -21,4 +21,27 @@ public function truncate() return parent::truncate(); } + + public function withoutGlobalScope($scope) + { + if ($this->withoutScopes == null) { + $this->withoutScopes = []; + } + + array_push($this->withoutScopes, $scope); + + return parent::withoutGlobalScope($scope); + } + + public function withoutGlobalScopes(array $scopes = null) + { + $this->withoutScopes = $scopes; + + if ($scopes == null || ($scopes != null && count($scopes) == 0)) { + $this->withoutAllScopes = true; + } + + return parent::withoutGlobalScopes($scopes); + } + } diff --git a/src/Traits/Caching.php b/src/Traits/Caching.php index 5e7800a..dad66e5 100644 --- a/src/Traits/Caching.php +++ b/src/Traits/Caching.php @@ -43,41 +43,17 @@ public function applyScopes() return parent::applyScopes(); } - public function withoutGlobalScope($scope) - { - $this->scopesAreApplied = true; - if ($this->withoutScopes == null) { - $this->withoutScopes = []; - } - - array_push($this->withoutScopes[], $scope); - $this->withoutAllScopes = false; - - return parent::withoutGlobalScope($scope); - } - - public function withoutGlobalScopes(array $scopes = null) - { - $this->scopesAreApplied = true; - $this->withoutScopes = $scopes; - - if ($scopes == null || ($scopes != null && count($scopes) == 0)) { - $this->withoutAllScopes = true; - } - - return parent::withoutGlobalScopes($scopes); - } - protected function applyScopesToInstance() { if (! property_exists($this, "scopes") || $this->scopesAreApplied + || $this->withoutAllScopes ) { return; } foreach ($this->scopes as $identifier => $scope) { - if (! isset($this->scopes[$identifier])) { + if (! isset($this->scopes[$identifier]) || isset($this->withoutScopes[$identifier])) { continue; } From 832a3daa6b8419172d14fa77cf2673dc9b11f32a Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Thu, 3 Mar 2022 08:48:12 -0600 Subject: [PATCH 4/7] testWithoutGlobalScopes --- src/CacheKey.php | 2 +- src/Traits/Caching.php | 2 +- tests/Integration/CachedBuilder/ScopeTest.php | 26 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index 5aaeeaa..cb3d2d4 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -24,7 +24,7 @@ public function __construct( $model, $query, $macroKey, - $withoutScopes, + array $withoutScopes, $withoutAllScopes ) { $this->eagerLoad = $eagerLoad; diff --git a/src/Traits/Caching.php b/src/Traits/Caching.php index dad66e5..a5a9403 100644 --- a/src/Traits/Caching.php +++ b/src/Traits/Caching.php @@ -16,7 +16,7 @@ trait Caching protected $isCachable = true; protected $scopesAreApplied = false; protected $macroKey = ""; - protected $withoutScopes = null; + protected $withoutScopes = []; protected $withoutAllScopes = false; public function __call($method, $parameters) diff --git a/tests/Integration/CachedBuilder/ScopeTest.php b/tests/Integration/CachedBuilder/ScopeTest.php index 30096f3..9796b72 100644 --- a/tests/Integration/CachedBuilder/ScopeTest.php +++ b/tests/Integration/CachedBuilder/ScopeTest.php @@ -156,6 +156,32 @@ public function testGlobalScopesWhenSwitchingContextUsingGetMethod() $this->assertEquals("B", $authorsB->first()); } + public function testWithoutGlobalScopes() + { + factory(Author::class, 200)->create(); + $user = factory(User::class)->create(["name" => "Andrew Junior"]); + $this->actingAs($user); + $authorsA = (new AuthorBeginsWithScoped) + ->withoutGlobalScopes() + ->get() + ->map(function ($author) { + return (new Str)->substr($author->name, 0, 1); + }) + ->unique(); + $user = factory(User::class)->create(["name" => "Barry Barry Barry"]); + $this->actingAs($user); + $authorsB = (new AuthorBeginsWithScoped) + ->withoutGlobalScopes() + ->get() + ->map(function ($author) { + return (new Str)->substr($author->name, 0, 1); + }) + ->unique(); + + $this->assertGreaterThan(1, count($authorsA)); + $this->assertGreaterThan(1, count($authorsB)); + } + public function testLocalScopesInRelationship() { $first = "A"; From 1f524a6d02cb59eb02f39e5002e713f1128b1412 Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Thu, 3 Mar 2022 09:50:55 -0600 Subject: [PATCH 5/7] Add unit test for testWithoutGlobalScope --- src/CacheKey.php | 6 +++- src/Traits/BuilderCaching.php | 8 ++---- tests/Integration/CachedBuilder/ScopeTest.php | 28 ++++++++++++++++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index cb3d2d4..a6b7e86 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -63,7 +63,11 @@ protected function getBindingsSlug() : string return ''; } - if ($this->withoutAllScopes || ($this->withoutScopes != null && count($this->withoutScopes) == 0)) { + if ($this->withoutAllScopes) { + return Arr::query($this->model->query()->withoutGlobalScopes()->getBindings()); + } + + if (count($this->withoutScopes) > 0) { return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutScopes)->getBindings()); } diff --git a/src/Traits/BuilderCaching.php b/src/Traits/BuilderCaching.php index f78d147..4e1459b 100644 --- a/src/Traits/BuilderCaching.php +++ b/src/Traits/BuilderCaching.php @@ -24,10 +24,6 @@ public function truncate() public function withoutGlobalScope($scope) { - if ($this->withoutScopes == null) { - $this->withoutScopes = []; - } - array_push($this->withoutScopes, $scope); return parent::withoutGlobalScope($scope); @@ -35,7 +31,9 @@ public function withoutGlobalScope($scope) public function withoutGlobalScopes(array $scopes = null) { - $this->withoutScopes = $scopes; + if ($scopes != null) { + $this->withoutScopes = $scopes; + } if ($scopes == null || ($scopes != null && count($scopes) == 0)) { $this->withoutAllScopes = true; diff --git a/tests/Integration/CachedBuilder/ScopeTest.php b/tests/Integration/CachedBuilder/ScopeTest.php index 9796b72..9f71d7f 100644 --- a/tests/Integration/CachedBuilder/ScopeTest.php +++ b/tests/Integration/CachedBuilder/ScopeTest.php @@ -171,7 +171,33 @@ public function testWithoutGlobalScopes() $user = factory(User::class)->create(["name" => "Barry Barry Barry"]); $this->actingAs($user); $authorsB = (new AuthorBeginsWithScoped) - ->withoutGlobalScopes() + ->withoutGlobalScopes(['GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith']) + ->get() + ->map(function ($author) { + return (new Str)->substr($author->name, 0, 1); + }) + ->unique(); + + $this->assertGreaterThan(1, count($authorsA)); + $this->assertGreaterThan(1, count($authorsB)); + } + + public function testWithoutGlobalScope() + { + factory(Author::class, 200)->create(); + $user = factory(User::class)->create(["name" => "Andrew Junior"]); + $this->actingAs($user); + $authorsA = (new AuthorBeginsWithScoped) + ->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith') + ->get() + ->map(function ($author) { + return (new Str)->substr($author->name, 0, 1); + }) + ->unique(); + $user = factory(User::class)->create(["name" => "Barry Barry Barry"]); + $this->actingAs($user); + $authorsB = (new AuthorBeginsWithScoped) + ->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith') ->get() ->map(function ($author) { return (new Str)->substr($author->name, 0, 1); From 6089d003e4d013ad34084c132caee6e068dd3f62 Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Thu, 3 Mar 2022 10:21:52 -0600 Subject: [PATCH 6/7] Rename withoutScopes and withoutAllScopes --- src/CacheKey.php | 18 +++++++++--------- src/Traits/BuilderCaching.php | 6 +++--- src/Traits/Caching.php | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/CacheKey.php b/src/CacheKey.php index a6b7e86..13ddfa1 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -16,23 +16,23 @@ class CacheKey protected $macroKey; protected $model; protected $query; - protected $withoutScopes = []; - protected $withoutAllScopes = false; + protected $withoutGlobalScopes = []; + protected $withoutAllGlobalScopes = false; public function __construct( array $eagerLoad, $model, $query, $macroKey, - array $withoutScopes, - $withoutAllScopes + array $withoutGlobalScopes, + $withoutAllGlobalScopes ) { $this->eagerLoad = $eagerLoad; $this->macroKey = $macroKey; $this->model = $model; $this->query = $query; - $this->withoutScopes = $withoutScopes; - $this->withoutAllScopes = $withoutAllScopes; + $this->withoutGlobalScopes = $withoutGlobalScopes; + $this->withoutAllGlobalScopes = $withoutAllGlobalScopes; } public function make( @@ -63,12 +63,12 @@ protected function getBindingsSlug() : string return ''; } - if ($this->withoutAllScopes) { + if ($this->withoutAllGlobalScopes) { return Arr::query($this->model->query()->withoutGlobalScopes()->getBindings()); } - if (count($this->withoutScopes) > 0) { - return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutScopes)->getBindings()); + if (count($this->withoutGlobalScopes) > 0) { + return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutGlobalScopes)->getBindings()); } return Arr::query($this->model->query()->getBindings()); diff --git a/src/Traits/BuilderCaching.php b/src/Traits/BuilderCaching.php index 4e1459b..44dd106 100644 --- a/src/Traits/BuilderCaching.php +++ b/src/Traits/BuilderCaching.php @@ -24,7 +24,7 @@ public function truncate() public function withoutGlobalScope($scope) { - array_push($this->withoutScopes, $scope); + array_push($this->withoutGlobalScopes, $scope); return parent::withoutGlobalScope($scope); } @@ -32,11 +32,11 @@ public function withoutGlobalScope($scope) public function withoutGlobalScopes(array $scopes = null) { if ($scopes != null) { - $this->withoutScopes = $scopes; + $this->withoutGlobalScopes = $scopes; } if ($scopes == null || ($scopes != null && count($scopes) == 0)) { - $this->withoutAllScopes = true; + $this->withoutAllGlobalScopes = true; } return parent::withoutGlobalScopes($scopes); diff --git a/src/Traits/Caching.php b/src/Traits/Caching.php index a5a9403..a615cd6 100644 --- a/src/Traits/Caching.php +++ b/src/Traits/Caching.php @@ -16,8 +16,8 @@ trait Caching protected $isCachable = true; protected $scopesAreApplied = false; protected $macroKey = ""; - protected $withoutScopes = []; - protected $withoutAllScopes = false; + protected $withoutGlobalScopes = []; + protected $withoutAllGlobalScopes = false; public function __call($method, $parameters) { @@ -47,13 +47,13 @@ protected function applyScopesToInstance() { if (! property_exists($this, "scopes") || $this->scopesAreApplied - || $this->withoutAllScopes + || $this->withoutAllGlobalScopes ) { return; } foreach ($this->scopes as $identifier => $scope) { - if (! isset($this->scopes[$identifier]) || isset($this->withoutScopes[$identifier])) { + if (! isset($this->scopes[$identifier]) || isset($this->withoutGlobalScopes[$identifier])) { continue; } @@ -169,7 +169,7 @@ protected function makeCacheKey( $query = $this->query->getQuery(); } - return (new CacheKey($eagerLoad, $model, $query, $this->macroKey, $this->withoutScopes, $this->withoutAllScopes)) + return (new CacheKey($eagerLoad, $model, $query, $this->macroKey, $this->withoutGlobalScopes, $this->withoutAllGlobalScopes)) ->make($columns, $idColumn, $keyDifferentiator); } From c14f5e9c0442f3fc1f280e1be6b6b101b26b1e20 Mon Sep 17 00:00:00 2001 From: Bill Harding Date: Thu, 3 Mar 2022 10:41:06 -0600 Subject: [PATCH 7/7] Add unit test testGlobalScopesAreNotCachedWhenUsingWithoutGlobalScopes --- tests/Integration/CachedBuilder/ScopeTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Integration/CachedBuilder/ScopeTest.php b/tests/Integration/CachedBuilder/ScopeTest.php index 9f71d7f..fb4ba9d 100644 --- a/tests/Integration/CachedBuilder/ScopeTest.php +++ b/tests/Integration/CachedBuilder/ScopeTest.php @@ -156,6 +156,31 @@ public function testGlobalScopesWhenSwitchingContextUsingGetMethod() $this->assertEquals("B", $authorsB->first()); } + public function testGlobalScopesAreNotCachedWhenUsingWithoutGlobalScopes() + { + $user = factory(User::class)->create(["name" => "Abernathy Kings"]); + $this->actingAs($user); + $author = factory(UncachedAuthor::class, 1) + ->create(['name' => 'Alois']) + ->first(); + $authors = (new AuthorBeginsWithScoped) + ->withoutGlobalScopes() + ->get(); + $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped"); + $tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped"]; + + $cachedResults = $this->cache() + ->tags($tags) + ->get($key)['value']; + $liveResults = (new UncachedAuthor) + ->nameStartsWith("A") + ->get(); + + $this->assertTrue($authors->contains($author)); + $this->assertTrue($cachedResults->contains($author)); + $this->assertTrue($liveResults->contains($author)); + } + public function testWithoutGlobalScopes() { factory(Author::class, 200)->create();