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 $data = []; |
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
|
|
|
$hash = $this->getHash($class, $id); |
40
|
|
|
$data = $this->data[$hash] ?? []; |
41
|
|
|
if ($data) { |
42
|
|
|
$model = $this->instantiate($class); |
43
|
|
|
$model->setPrimaryKey($id); |
44
|
|
|
if ($columns) { |
|
|
|
|
45
|
|
|
$data = array_intersect_key($data, array_flip($columns)); |
46
|
|
|
} |
47
|
|
|
$model->fill($data); |
48
|
|
|
return $model; |
49
|
|
|
} |
50
|
|
|
return null; |
51
|
|
|
|
52
|
|
|
// if (!$columns && !$this->hasAllColumns($id, $class)) { |
|
|
|
|
53
|
|
|
// return null; |
54
|
|
|
// } |
55
|
|
|
// $cachedModel = $this->collection->get($this->getHash($class, $id)); |
|
|
|
|
56
|
|
|
// if ($cachedModel != null && $columns) { |
|
|
|
|
57
|
|
|
// $cachedModel = Cast::storable($cachedModel); |
|
|
|
|
58
|
|
|
// $model = $this->instantiate($class); |
|
|
|
|
59
|
|
|
// $model->setPrimaryKey($cachedModel->getPrimaryKey()); |
|
|
|
|
60
|
|
|
// $model->fill(array_intersect_key($cachedModel->toArray(), array_flip($columns))); |
|
|
|
|
61
|
|
|
// return $model; |
62
|
|
|
// } |
63
|
|
|
// return $cachedModel; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection |
67
|
|
|
{ |
68
|
|
|
return new Collection($this->data); // @TODO return data that's related to $class |
69
|
|
|
// @TODO return collection of models, not array |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function insert(Storable $model): void |
73
|
|
|
{ |
74
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
75
|
|
|
$this->data[$hash] = $model->toArray(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function update(Storable $model): void |
79
|
|
|
{ |
80
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
81
|
|
|
$this->data[$hash] = array_merge($this->data[$hash] ?? [], $model->toArray()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function delete(Storable $model): void |
85
|
|
|
{ |
86
|
|
|
$hash = $this->getHash(get_class($model), $model->getPrimaryKey()); |
87
|
|
|
unset($this->data[$hash]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function hasAllColumns(string $id, string $class): bool |
91
|
|
|
{ |
92
|
|
|
return $this->allColumns->has($this->getHash($class, $id)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setAllColumns(string $id, string $class): void |
96
|
|
|
{ |
97
|
|
|
$this->allColumns->put($this->getHash($class, $id), true); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.