Skip to content

Commit 6cb0629

Browse files
committed
Remove use of helper methods
1 parent a5a4c88 commit 6cb0629

File tree

11 files changed

+86
-20
lines changed

11 files changed

+86
-20
lines changed

CHANGELOG.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,28 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [0.5.3] - 21 May 2019
7+
## [0.5.6] - 2019-06-20
8+
### Removed
9+
- all use of helper methods to allow non-Laravel apps to use this package:
10+
- app()
11+
- config()
12+
- cache()
13+
- db()
14+
- now()
15+
- request()
16+
17+
## [0.5.5] - 2019-05-27
18+
### Fixed
19+
- parsing of soft-deleted-related queries:
20+
- withTrashed()
21+
- onlyTrashed()
22+
- withoutTrashed()
23+
24+
## [0.5.4] - 2019-05-27
25+
### Fixed
26+
- parsing of global scopes. Rewrote how global scopes are analyzed to create appropriate cache key.
27+
28+
## [0.5.3] - 2019-05-21
829
### Fixed
930
- calling `flushCache()` on non-cachable related models.
1031

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
"illuminate/cache": "5.8.*",
1414
"illuminate/config": "5.8.*",
1515
"illuminate/console": "5.8.*",
16+
"illuminate/container": "5.8.*",
1617
"illuminate/database": "5.8.*",
1718
"illuminate/support": "5.8.*",
19+
"illuminate/http": "5.8.*",
1820
"php": "^7.1.3"
1921
},
2022
"require-dev": {

src/Console/Commands/Clear.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
22

33
use Illuminate\Console\Command;
4+
use Illuminate\Container\Container;
45
use Illuminate\Support\Collection;
56

67
class Clear extends Command
@@ -21,8 +22,13 @@ public function handle()
2122

2223
protected function flushEntireCache() : int
2324
{
24-
app('cache')
25-
->store(config('laravel-model-caching.store'))
25+
$config = Container::getInstance()
26+
->make("config")
27+
->get('laravel-model-caching.store');
28+
29+
Container::getInstance()
30+
->make("cache")
31+
->store($config)
2632
->flush();
2733

2834
$this->info("✔︎ Entire model cache has been flushed.");

src/Helper.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3+
use Illuminate\Container\Container;
4+
35
class Helper
46
{
57
public function runDisabled(callable $closure)
68
{
7-
$originalSetting = config('laravel-model-caching.disabled');
8-
config(['laravel-model-caching.disabled' => true]);
9+
$originalSetting = Container::getInstance()
10+
->make("config")
11+
->get('laravel-model-caching.disabled');
12+
13+
Container::getInstance()
14+
->make("config")
15+
->set(['laravel-model-caching.disabled' => true]);
916

1017
$result = $closure();
1118

12-
config(['laravel-model-caching.disabled' => $originalSetting]);
19+
Container::getInstance()
20+
->make("config")
21+
->set(['laravel-model-caching.disabled' => $originalSetting]);
1322

1423
return $result;
1524
}

src/Traits/Buildable.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3+
use Illuminate\Container\Container;
4+
35
/**
46
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
57
*/
@@ -146,7 +148,9 @@ public function paginate(
146148
return parent::paginate($perPage, $columns, $pageName, $page);
147149
}
148150

149-
$page = app('request')->input($pageName)
151+
$page = Container::getInstance()
152+
->make("request")
153+
->input($pageName)
150154
?: $page
151155
?: 1;
152156

src/Traits/CachePrefixing.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3+
use Illuminate\Container\Container;
4+
35
trait CachePrefixing
46
{
57
protected function getCachePrefix() : string
68
{
7-
$cachePrefix = config("laravel-model-caching.cache-prefix", "");
9+
$cachePrefix = Container::getInstance()
10+
->make("config")
11+
->get("laravel-model-caching.cache-prefix", "");
812

913
if ($this->model
1014
&& property_exists($this->model, "cachePrefix")

src/Traits/Caching.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3-
use Carbon\Carbon;
43
use Closure;
54
use GeneaLabs\LaravelModelCaching\CachedBuilder;
65
use GeneaLabs\LaravelModelCaching\CacheKey;
76
use GeneaLabs\LaravelModelCaching\CacheTags;
87
use Illuminate\Cache\TaggableStore;
8+
use Illuminate\Container\Container;
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\Scope;
1111
use Illuminate\Database\Query\Builder;
12+
use Illuminate\Support\Carbon;
1213

1314
trait Caching
1415
{
@@ -62,10 +63,14 @@ protected function applyScopesToInstance()
6263

6364
public function cache(array $tags = [])
6465
{
65-
$cache = app('cache');
66-
67-
if (config('laravel-model-caching.store')) {
68-
$cache = $cache->store(config('laravel-model-caching.store'));
66+
$cache = Container::getInstance()
67+
->make("cache");
68+
$config = Container::getInstance()
69+
->make("config")
70+
->get("laravel-model-caching.store");
71+
72+
if ($config) {
73+
$cache = $cache->store($config);
6974
}
7075

7176
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
@@ -106,7 +111,9 @@ public function flushCache(array $tags = [])
106111

107112
protected function getCachePrefix() : string
108113
{
109-
$cachePrefix = config("laravel-model-caching.cache-prefix", "");
114+
$cachePrefix = Container::getInstance()
115+
->make("config")
116+
->get("laravel-model-caching.cache-prefix", "");
110117

111118
if ($this->model
112119
&& property_exists($this->model, "cachePrefix")
@@ -140,7 +147,9 @@ protected function makeCacheKey(
140147
}
141148

142149
$query = $this->query
143-
?? app('db')->query();
150+
?? Container::getInstance()
151+
->make("db")
152+
->query();
144153

145154
if ($this->query
146155
&& method_exists($this->query, "getQuery")
@@ -160,7 +169,9 @@ protected function makeCacheTags() : array
160169
: $this;
161170
$query = $this->query instanceof Builder
162171
? $this->query
163-
: app('db')->query();
172+
: Container::getInstance()
173+
->make("db")
174+
->query();
164175
$tags = (new CacheTags($eagerLoad, $model, $query))
165176
->make();
166177

@@ -259,8 +270,12 @@ protected function checkCooldownAndFlushAfterPersisting(Model $instance, string
259270

260271
public function isCachable() : bool
261272
{
273+
$isCacheDisabled = Container::getInstance()
274+
->make("config")
275+
->get("laravel-model-caching.disabled");
276+
262277
return $this->isCachable
263-
&& ! config('laravel-model-caching.disabled');
278+
&& ! $isCacheDisabled;
264279
}
265280

266281
protected function setCacheCooldownSavedAtTimestamp(Model $instance)

src/Traits/ModelCaching.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
22

3-
use Carbon\Carbon;
43
use GeneaLabs\LaravelModelCaching\CachedBelongsToMany;
54
use GeneaLabs\LaravelModelCaching\CachedBuilder;
5+
use Illuminate\Container\Container;
66
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
use Illuminate\Support\Carbon;
910

1011
trait ModelCaching
1112
{
@@ -39,7 +40,11 @@ public function __set($key, $value)
3940

4041
public static function all($columns = ['*'])
4142
{
42-
if (config('laravel-model-caching.disabled')) {
43+
$isCacheDisabled = Container::getInstance()
44+
->make("config")
45+
->get("laravel-model-caching.disabled");
46+
47+
if ($isCacheDisabled) {
4348
return parent::all($columns);
4449
}
4550

tests/Integration/CachedModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function testCooldownIsQueriedForCooldownModels()
169169
[$usesCacheCooldown, $expiresAt, $savedAt] = $method->invokeArgs($author, [$author]);
170170

171171
$this->assertEquals($usesCacheCooldown, 1);
172-
$this->assertEquals("Carbon\Carbon", get_class($expiresAt));
172+
$this->assertEquals("Illuminate\Support\Carbon", get_class($expiresAt));
173173
$this->assertNull($savedAt);
174174
}
175175

tests/database/baseline.sqlite

0 Bytes
Binary file not shown.

tests/database/testing.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)