|
1 | 1 | <?php namespace GeneaLabs\LaravelModelCaching;
|
2 | 2 |
|
3 |
| -use Illuminate\Cache\CacheManager; |
4 |
| -use Illuminate\Cache\TaggableStore; |
5 |
| -use Illuminate\Database\Eloquent\Model; |
6 |
| -use Illuminate\Database\Eloquent\Relations\Relation; |
7 |
| -use LogicException; |
| 3 | +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
8 | 4 |
|
9 |
| -abstract class CachedModel extends Model |
| 5 | +class Builder extends EloquentBuilder |
10 | 6 | {
|
11 |
| - protected function getRelationshipFromMethod($method) |
| 7 | + protected function eagerLoadRelation(array $models, $name, Closure $constraints) |
12 | 8 | {
|
13 |
| - $relation = $this->$method(); |
14 |
| - |
15 |
| - if (! $relation instanceof Relation) { |
16 |
| - throw new LogicException(get_class($this).'::'.$method.' must return a relationship instance.'); |
17 |
| - } |
18 |
| - |
19 |
| - $results = $this->cache([$method]) |
20 |
| - ->rememberForever(str_slug(get_called_class()) . "-{$method}", function () use ($relation) { |
21 |
| - return $relation->getResults(); |
| 9 | + $relation = $this->getRelation($name); |
| 10 | + $relation->addEagerConstraints($models); |
| 11 | + $constraints($relation); |
| 12 | + |
| 13 | + $parentName = str_slug(get_class($relation->getParent())); |
| 14 | + $childName = str_slug(get_class($relation->getModel())); |
| 15 | + $results = cache()->tags([$parentName, $childName]) |
| 16 | + ->rememberForever("{$parentName}-{$childName}-relation", function () use ($relation) { |
| 17 | + return $relation->getEager(); |
22 | 18 | });
|
23 | 19 |
|
24 |
| - return tap($results, function ($results) use ($method) { |
25 |
| - $this->setRelation($method, $results); |
26 |
| - }); |
27 |
| - } |
28 |
| - |
29 |
| - public function newEloquentBuilder($query) |
30 |
| - { |
31 |
| - return new Builder($query); |
32 |
| - } |
33 |
| - |
34 |
| - public static function boot() |
35 |
| - { |
36 |
| - parent::boot(); |
37 |
| - |
38 |
| - static::created(function () { |
39 |
| - self::flushCache(); |
40 |
| - }); |
41 |
| - |
42 |
| - static::deleted(function () { |
43 |
| - self::flushCache(); |
44 |
| - }); |
45 |
| - |
46 |
| - static::saved(function () { |
47 |
| - self::flushCache(); |
48 |
| - }); |
49 |
| - |
50 |
| - static::updated(function () { |
51 |
| - self::flushCache(); |
52 |
| - }); |
53 |
| - } |
54 |
| - |
55 |
| - public function cache(array $tags = []) |
56 |
| - { |
57 |
| - $cache = cache(); |
58 |
| - |
59 |
| - if (is_subclass_of(cache()->getStore(), TaggableStore::class)) { |
60 |
| - array_push($tags, str_slug(get_called_class())); |
61 |
| - $cache = $cache->tags($tags); |
62 |
| - } |
63 |
| - |
64 |
| - return $cache; |
65 |
| - } |
66 |
| - |
67 |
| - public static function flushCache() |
68 |
| - { |
69 |
| - cache()->tags([str_slug(get_called_class())]) |
70 |
| - ->flush(); |
| 20 | + return $relation->match( |
| 21 | + $relation->initRelation($models, $name), |
| 22 | + $results, |
| 23 | + $name |
| 24 | + ); |
71 | 25 | }
|
72 | 26 | }
|
0 commit comments