|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Isswp101\Persimmon\Collection\Collection; |
|
6
|
|
|
use Isswp101\Persimmon\Collection\ICollection; |
|
7
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
|
8
|
|
|
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException; |
|
9
|
|
|
use Isswp101\Persimmon\Helpers\Cast; |
|
10
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
|
11
|
|
|
|
|
12
|
|
|
class RuntimeCacheRepository implements ICacheRepository |
|
13
|
|
|
{ |
|
14
|
|
|
private $collection; |
|
15
|
|
|
private $allColumns; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->collection = new Collection(); |
|
20
|
|
|
$this->allColumns = new Collection(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
private function getHash(string $class, string $id): string |
|
24
|
|
|
{ |
|
25
|
|
|
return $class . '@' . $id; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function instantiate(string $class): Storable |
|
29
|
|
|
{ |
|
30
|
|
|
$instance = new $class; |
|
31
|
|
|
if (!$instance instanceof Storable) { |
|
32
|
|
|
throw new ClassTypeErrorException(Storable::class); |
|
33
|
|
|
} |
|
34
|
|
|
return $instance; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function find(string $id, string $class, array $columns = []): ?Storable |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$columns && !$this->hasAllColumns($id, $class)) { |
|
|
|
|
|
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
$cachedModel = $this->collection->get($this->getHash($class, $id)); |
|
43
|
|
|
if ($cachedModel != null && $columns) { |
|
|
|
|
|
|
44
|
|
|
$cachedModel = Cast::storable($cachedModel); |
|
45
|
|
|
$model = $this->instantiate($class); |
|
46
|
|
|
$model->setPrimaryKey($cachedModel->getPrimaryKey()); |
|
47
|
|
|
$model->fill(array_intersect_key($cachedModel->toArray(), array_flip($columns))); |
|
48
|
|
|
return $model; |
|
49
|
|
|
} |
|
50
|
|
|
return $cachedModel; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->collection; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function insert(Storable $model): void |
|
59
|
|
|
{ |
|
60
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
|
61
|
|
|
$this->collection->put($hash, $model); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function update(Storable $model): void |
|
65
|
|
|
{ |
|
66
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
|
67
|
|
|
if (!$this->collection->has($hash)) { |
|
68
|
|
|
$this->collection->put($hash, $model); |
|
69
|
|
|
} else { |
|
70
|
|
|
$cachedModel = $this->collection->get($hash); |
|
71
|
|
|
foreach ($model->toArray() as $key => $value) { |
|
72
|
|
|
$cachedModel->{$key} = $value; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function delete(Storable $model): void |
|
78
|
|
|
{ |
|
79
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
|
80
|
|
|
$this->collection->forget($hash); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function hasAllColumns(string $id, string $class): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->allColumns->has($this->getHash($class, $id)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function setAllColumns(string $id, string $class): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->allColumns->put($this->getHash($class, $id), true); |
|
91
|
|
|
} |
|
92
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.