Skip to content

Commit 7c4b047

Browse files
authored
Merge pull request #424 from gitwilliam/add-support-for-withoutGlobalScopes
Add support for withoutGlobalScopes
2 parents 01be319 + c14f5e9 commit 7c4b047

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

src/CacheKey.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ class CacheKey
1616
protected $macroKey;
1717
protected $model;
1818
protected $query;
19+
protected $withoutGlobalScopes = [];
20+
protected $withoutAllGlobalScopes = false;
1921

2022
public function __construct(
2123
array $eagerLoad,
2224
$model,
2325
$query,
24-
$macroKey
26+
$macroKey,
27+
array $withoutGlobalScopes,
28+
$withoutAllGlobalScopes
2529
) {
2630
$this->eagerLoad = $eagerLoad;
2731
$this->macroKey = $macroKey;
2832
$this->model = $model;
2933
$this->query = $query;
34+
$this->withoutGlobalScopes = $withoutGlobalScopes;
35+
$this->withoutAllGlobalScopes = $withoutAllGlobalScopes;
3036
}
3137

3238
public function make(
@@ -57,6 +63,14 @@ protected function getBindingsSlug() : string
5763
return '';
5864
}
5965

66+
if ($this->withoutAllGlobalScopes) {
67+
return Arr::query($this->model->query()->withoutGlobalScopes()->getBindings());
68+
}
69+
70+
if (count($this->withoutGlobalScopes) > 0) {
71+
return Arr::query($this->model->query()->withoutGlobalScopes($this->withoutGlobalScopes)->getBindings());
72+
}
73+
6074
return Arr::query($this->model->query()->getBindings());
6175
}
6276

src/Traits/BuilderCaching.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,25 @@ public function truncate()
2121

2222
return parent::truncate();
2323
}
24+
25+
public function withoutGlobalScope($scope)
26+
{
27+
array_push($this->withoutGlobalScopes, $scope);
28+
29+
return parent::withoutGlobalScope($scope);
30+
}
31+
32+
public function withoutGlobalScopes(array $scopes = null)
33+
{
34+
if ($scopes != null) {
35+
$this->withoutGlobalScopes = $scopes;
36+
}
37+
38+
if ($scopes == null || ($scopes != null && count($scopes) == 0)) {
39+
$this->withoutAllGlobalScopes = true;
40+
}
41+
42+
return parent::withoutGlobalScopes($scopes);
43+
}
44+
2445
}

src/Traits/Caching.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ trait Caching
1616
protected $isCachable = true;
1717
protected $scopesAreApplied = false;
1818
protected $macroKey = "";
19+
protected $withoutGlobalScopes = [];
20+
protected $withoutAllGlobalScopes = false;
1921

2022
public function __call($method, $parameters)
2123
{
@@ -45,12 +47,13 @@ protected function applyScopesToInstance()
4547
{
4648
if (! property_exists($this, "scopes")
4749
|| $this->scopesAreApplied
50+
|| $this->withoutAllGlobalScopes
4851
) {
4952
return;
5053
}
5154

5255
foreach ($this->scopes as $identifier => $scope) {
53-
if (! isset($this->scopes[$identifier])) {
56+
if (! isset($this->scopes[$identifier]) || isset($this->withoutGlobalScopes[$identifier])) {
5457
continue;
5558
}
5659

@@ -166,7 +169,7 @@ protected function makeCacheKey(
166169
$query = $this->query->getQuery();
167170
}
168171

169-
return (new CacheKey($eagerLoad, $model, $query, $this->macroKey))
172+
return (new CacheKey($eagerLoad, $model, $query, $this->macroKey, $this->withoutGlobalScopes, $this->withoutAllGlobalScopes))
170173
->make($columns, $idColumn, $keyDifferentiator);
171174
}
172175

tests/Integration/CachedBuilder/ScopeTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,83 @@ public function testGlobalScopesWhenSwitchingContextUsingGetMethod()
156156
$this->assertEquals("B", $authorsB->first());
157157
}
158158

159+
public function testGlobalScopesAreNotCachedWhenUsingWithoutGlobalScopes()
160+
{
161+
$user = factory(User::class)->create(["name" => "Abernathy Kings"]);
162+
$this->actingAs($user);
163+
$author = factory(UncachedAuthor::class, 1)
164+
->create(['name' => 'Alois'])
165+
->first();
166+
$authors = (new AuthorBeginsWithScoped)
167+
->withoutGlobalScopes()
168+
->get();
169+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped");
170+
$tags = ["genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthorbeginswithscoped"];
171+
172+
$cachedResults = $this->cache()
173+
->tags($tags)
174+
->get($key)['value'];
175+
$liveResults = (new UncachedAuthor)
176+
->nameStartsWith("A")
177+
->get();
178+
179+
$this->assertTrue($authors->contains($author));
180+
$this->assertTrue($cachedResults->contains($author));
181+
$this->assertTrue($liveResults->contains($author));
182+
}
183+
184+
public function testWithoutGlobalScopes()
185+
{
186+
factory(Author::class, 200)->create();
187+
$user = factory(User::class)->create(["name" => "Andrew Junior"]);
188+
$this->actingAs($user);
189+
$authorsA = (new AuthorBeginsWithScoped)
190+
->withoutGlobalScopes()
191+
->get()
192+
->map(function ($author) {
193+
return (new Str)->substr($author->name, 0, 1);
194+
})
195+
->unique();
196+
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
197+
$this->actingAs($user);
198+
$authorsB = (new AuthorBeginsWithScoped)
199+
->withoutGlobalScopes(['GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith'])
200+
->get()
201+
->map(function ($author) {
202+
return (new Str)->substr($author->name, 0, 1);
203+
})
204+
->unique();
205+
206+
$this->assertGreaterThan(1, count($authorsA));
207+
$this->assertGreaterThan(1, count($authorsB));
208+
}
209+
210+
public function testWithoutGlobalScope()
211+
{
212+
factory(Author::class, 200)->create();
213+
$user = factory(User::class)->create(["name" => "Andrew Junior"]);
214+
$this->actingAs($user);
215+
$authorsA = (new AuthorBeginsWithScoped)
216+
->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith')
217+
->get()
218+
->map(function ($author) {
219+
return (new Str)->substr($author->name, 0, 1);
220+
})
221+
->unique();
222+
$user = factory(User::class)->create(["name" => "Barry Barry Barry"]);
223+
$this->actingAs($user);
224+
$authorsB = (new AuthorBeginsWithScoped)
225+
->withoutGlobalScope('GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith')
226+
->get()
227+
->map(function ($author) {
228+
return (new Str)->substr($author->name, 0, 1);
229+
})
230+
->unique();
231+
232+
$this->assertGreaterThan(1, count($authorsA));
233+
$this->assertGreaterThan(1, count($authorsB));
234+
}
235+
159236
public function testLocalScopesInRelationship()
160237
{
161238
$first = "A";

0 commit comments

Comments
 (0)