Skip to content

Commit fa2e106

Browse files
Merge pull request #71 from TheDragonCode/3.x
Added the ability to flexibly configure storage activation
2 parents d16d8ff + 4086dec commit fa2e106

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+375
-90
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,42 @@ $cache->doesntHave();
428428
// Returns `true`
429429
```
430430

431+
You can also define whether to enable or disable the use of cache storage in the settings.
432+
433+
For example:
434+
435+
```php
436+
// config/cache.php
437+
return [
438+
'enabled' => [
439+
// App\Models\Page::class => true,
440+
//
441+
// 'stdClass' => false,
442+
//
443+
// 'foo' => false,
444+
],
445+
];
446+
```
447+
448+
```php
449+
use App\Services\Some;use DragonCode\Cache\Services\Cache;
450+
451+
// as string
452+
$cache = Cache::make()->when('foo');
453+
454+
// as class-string
455+
$cache = Cache::make()->when(Some::class);
456+
$cache = Cache::make()->when(static::class);
457+
$cache = Cache::make()->when(self::class);
458+
459+
// as class
460+
$cache = Cache::make()->when(new Some);
461+
$cache = Cache::make()->when($this);
462+
463+
// as stdClass
464+
$cache = Cache::make()->when((object)['foo' => 'Foo']);
465+
```
466+
431467
## License
432468

433469
This package's licensed under the [MIT License](LICENSE).

config/cache.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
// 'foo' => 300,
1414
// 'bar' => 600,
1515
],
16+
17+
'enabled' => [
18+
// App\Models\Page::class => true,
19+
// App\Models\News::class => true,
20+
// App\Services\Custom::class => true,
21+
//
22+
// 'stdClass' => false,
23+
//
24+
// 'foo' => false,
25+
// 'bar' => false,
26+
],
1627
];

src/Services/Cache.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ class Cache
3030

3131
protected string $key_hash = '';
3232

33-
protected bool $when = true;
33+
protected bool|object|string $when = true;
3434

3535
protected array|string|null $auth = null;
3636

37-
public function when(bool $when = true): Cache
37+
public function when(bool|object|string $when = true): Cache
3838
{
39-
$this->when = $when;
39+
$this->when = match (true) {
40+
is_string($when) => config('cache.enabled.' . $when, true),
41+
is_object($when) => config('cache.enabled.' . get_class($when), true),
42+
default => $when
43+
};
4044

4145
return $this;
4246
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\EnabledConfig;
6+
7+
use Tests\TestCase;
8+
9+
class BoolTest extends TestCase
10+
{
11+
protected bool|object|string $when = true;
12+
13+
public function testEnabled()
14+
{
15+
$this->when = true;
16+
17+
$this->assertFalse($this->cache()->has());
18+
19+
$this->cache()->put($this->value);
20+
21+
$this->assertTrue($this->cache()->has());
22+
}
23+
24+
public function testDisabled()
25+
{
26+
$this->when = false;
27+
28+
$this->assertFalse($this->cache()->has());
29+
30+
$this->cache()->put($this->value);
31+
32+
$this->assertTrue($this->cache()->doesntHave());
33+
}
34+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\EnabledConfig;
6+
7+
use stdClass;
8+
use Tests\Fixtures\Simple\CustomObject;
9+
use Tests\TestCase;
10+
11+
class DisabledTest extends TestCase
12+
{
13+
protected bool|object|string $when = true;
14+
15+
public function testBool()
16+
{
17+
$this->when = false;
18+
19+
$this->assertTrue($this->cache()->doesntHave());
20+
21+
$this->cache()->put($this->value);
22+
23+
$this->assertTrue($this->cache()->doesntHave());
24+
}
25+
26+
public function testString()
27+
{
28+
$this->when = 'foo';
29+
30+
config(['cache.enabled.foo' => false]);
31+
32+
$this->assertTrue($this->cache()->doesntHave());
33+
34+
$this->cache()->put($this->value);
35+
36+
$this->assertTrue($this->cache()->doesntHave());
37+
}
38+
39+
public function testStringObject()
40+
{
41+
$this->when = CustomObject::class;
42+
43+
config(['cache.enabled.' . CustomObject::class => false]);
44+
45+
$this->assertTrue($this->cache()->doesntHave());
46+
47+
$this->cache()->put($this->value);
48+
49+
$this->assertTrue($this->cache()->doesntHave());
50+
}
51+
52+
public function testObject()
53+
{
54+
$this->when = new CustomObject();
55+
56+
config(['cache.enabled.' . CustomObject::class => false]);
57+
58+
$this->assertTrue($this->cache()->doesntHave());
59+
60+
$this->cache()->put($this->value);
61+
62+
$this->assertTrue($this->cache()->doesntHave());
63+
}
64+
65+
public function testStdClass()
66+
{
67+
$this->when = (object) ['foo' => 'bar'];
68+
69+
config(['cache.enabled.' . stdClass::class => false]);
70+
71+
$this->assertTrue($this->cache()->doesntHave());
72+
73+
$this->cache()->put($this->value);
74+
75+
$this->assertTrue($this->cache()->doesntHave());
76+
}
77+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\EnabledConfig;
6+
7+
use stdClass;
8+
use Tests\Fixtures\Simple\CustomObject;
9+
use Tests\TestCase;
10+
11+
class ObjectTest extends TestCase
12+
{
13+
protected bool|object|string $when = true;
14+
15+
public function testExists()
16+
{
17+
$this->when = new CustomObject();
18+
19+
config(['cache.enabled.' . CustomObject::class => true]);
20+
21+
$this->assertFalse($this->cache()->has());
22+
23+
$this->cache()->put($this->value);
24+
25+
$this->assertTrue($this->cache()->has());
26+
}
27+
28+
public function testDoesntExists()
29+
{
30+
$this->when = new CustomObject();
31+
32+
$this->assertFalse($this->cache()->has());
33+
34+
$this->cache()->put($this->value);
35+
36+
$this->assertTrue($this->cache()->has());
37+
}
38+
39+
public function testExistsStdClass()
40+
{
41+
$this->when = (object) ['foo' => 'bar'];
42+
43+
config(['cache.enabled.' . stdClass::class => true]);
44+
45+
$this->assertFalse($this->cache()->has());
46+
47+
$this->cache()->put($this->value);
48+
49+
$this->assertTrue($this->cache()->has());
50+
}
51+
52+
public function testDoesntExistsStdClass()
53+
{
54+
$this->when = (object) ['foo' => 'bar'];
55+
56+
$this->assertFalse($this->cache()->has());
57+
58+
$this->cache()->put($this->value);
59+
60+
$this->assertTrue($this->cache()->has());
61+
}
62+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Cache\EnabledConfig;
6+
7+
use Tests\Fixtures\Simple\CustomObject;
8+
use Tests\TestCase;
9+
10+
class StringTest extends TestCase
11+
{
12+
protected bool|object|string $when = true;
13+
14+
public function testExists()
15+
{
16+
$this->when = 'foo';
17+
18+
config(['cache.enabled.foo' => true]);
19+
20+
$this->assertFalse($this->cache()->has());
21+
22+
$this->cache()->put($this->value);
23+
24+
$this->assertTrue($this->cache()->has());
25+
}
26+
27+
public function testExistsClass()
28+
{
29+
$this->when = CustomObject::class;
30+
31+
config(['cache.enabled.' . CustomObject::class => true]);
32+
33+
$this->assertFalse($this->cache()->has());
34+
35+
$this->cache()->put($this->value);
36+
37+
$this->assertTrue($this->cache()->has());
38+
}
39+
40+
public function testDoesntExists()
41+
{
42+
$this->when = 'bar';
43+
44+
$this->assertFalse($this->cache()->has());
45+
46+
$this->cache()->put($this->value);
47+
48+
$this->assertTrue($this->cache()->has());
49+
}
50+
51+
public function testDoesntExistsClass()
52+
{
53+
$this->when = CustomObject::class;
54+
55+
$this->assertFalse($this->cache()->has());
56+
57+
$this->cache()->put($this->value);
58+
59+
$this->assertTrue($this->cache()->has());
60+
}
61+
}

tests/Cache/NotWhen/Arrayables/Many/Arr/DragonCodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class DragonCodeTest extends Base
1111
{
12-
protected $value = [
12+
protected mixed $value = [
1313
'foo' => 'Foo',
1414
'bar' => 'Bar',
1515
'baz' => [

tests/Cache/NotWhen/Arrayables/Many/Arr/IlluminateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class IlluminateTest extends Base
1111
{
12-
protected $value = [
12+
protected mixed $value = [
1313
'foo' => 'Foo',
1414
'bar' => 'Bar',
1515
'baz' => [

tests/Cache/NotWhen/Arrayables/Many/Arr/MixedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class MixedTest extends Base
1111
{
12-
protected $value = [
12+
protected mixed $value = [
1313
'foo' => 'Foo',
1414
'bar' => 'Bar',
1515
'baz' => [

0 commit comments

Comments
 (0)