Skip to content

Commit e94b530

Browse files
authored
Merge pull request #12 from DirectoryTree/feature-11
Add support for custom developer repositories
2 parents ba0860f + 6cba0c5 commit e94b530

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

src/Model.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use Illuminate\Support\Facades\Date;
2828
use Illuminate\Support\Str;
2929
use Illuminate\Support\Traits\ForwardsCalls;
30-
use InvalidArgumentException;
3130
use JsonException;
3231
use Stringable;
3332

@@ -432,9 +431,9 @@ public static function setRepository(string $repository): void
432431
protected function newRepository(): Repository
433432
{
434433
return match ($repository = static::$repository) {
434+
'redis' => app(RedisRepository::class, [$this->getConnection()]),
435435
'array' => app(ArrayRepository::class),
436-
'redis' => app(RedisRepository::class, ['redis' => $this->getConnection()]),
437-
default => throw new InvalidArgumentException("Repository [{$repository}] is not supported."),
436+
default => app($repository, [$this]),
438437
};
439438
}
440439

tests/ModelRepositoryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use DirectoryTree\ActiveRedis\Tests\Stubs\ModelStubWithCustomRepository;
4+
5+
it('can use custom null repository', function () {
6+
expect(ModelStubWithCustomRepository::create())->toBeinstanceOf(ModelStubWithCustomRepository::class);
7+
expect(ModelStubWithCustomRepository::all())->toBeEmpty();
8+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace DirectoryTree\ActiveRedis\Tests\Stubs;
4+
5+
use DirectoryTree\ActiveRedis\Model;
6+
7+
class ModelStubWithCustomRepository extends Model
8+
{
9+
protected static string $repository = NullRepository::class;
10+
}

tests/Stubs/NullRepository.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace DirectoryTree\ActiveRedis\Tests\Stubs;
4+
5+
use Closure;
6+
use DirectoryTree\ActiveRedis\Repositories\Repository;
7+
use Generator;
8+
9+
class NullRepository implements Repository
10+
{
11+
public function exists(string $hash): bool
12+
{
13+
return false;
14+
}
15+
16+
public function chunk(string $pattern, int $count): Generator
17+
{
18+
yield [];
19+
}
20+
21+
public function setAttribute(string $hash, string $attribute, string $value): void
22+
{
23+
// Do nothing.
24+
}
25+
26+
public function setAttributes(string $hash, array $attributes): void
27+
{
28+
// Do nothing.
29+
}
30+
31+
public function getAttribute(string $hash, string $field): mixed
32+
{
33+
return null;
34+
}
35+
36+
public function getAttributes(string $hash): array
37+
{
38+
return [];
39+
}
40+
41+
public function setExpiry(string $hash, int $seconds): void
42+
{
43+
// Do nothing.
44+
}
45+
46+
public function getExpiry(string $hash): ?int
47+
{
48+
return null;
49+
}
50+
51+
public function deleteAttributes(string $hash, array|string $attributes): void
52+
{
53+
// Do nothing.
54+
}
55+
56+
public function delete(string $hash): void
57+
{
58+
// Do nothing.
59+
}
60+
61+
public function transaction(Closure $operation): void
62+
{
63+
// Do nothing.
64+
}
65+
}

0 commit comments

Comments
 (0)