-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHasLazyRelations.php
More file actions
115 lines (92 loc) · 3.41 KB
/
HasLazyRelations.php
File metadata and controls
115 lines (92 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Michalsn\CodeIgniterNestedModel\Traits;
use CodeIgniter\Autoloader\FileLocatorInterface;
use CodeIgniter\Model;
use Michalsn\CodeIgniterNestedModel\Enums\RelationTypes;
trait HasLazyRelations
{
/**
* @var array<string, bool>
*/
private array $handledRelations = [];
private ?Model $relationModel = null;
private bool $relationModelResolved = false;
public function __get(string $key)
{
if (array_key_exists($key, $this->attributes)) {
return parent::__get($key);
}
$model = $this->getRelationModel();
if ($model !== null && method_exists($model, $key)) {
if (! isset($this->handledRelations[$key]) && ! array_key_exists($key, $this->attributes)) {
$this->handleRelation($key, $model);
$this->handledRelations[$key] = true;
}
return $this->attributes[$key] ?? null;
}
return parent::__get($key);
}
/**
* Load relation for the property.
*/
private function handleRelation(string $name, Model $model)
{
$relation = $model->{$name}();
if (in_array($relation->type, [RelationTypes::hasOne, RelationTypes::belongsTo], true)) {
$this->attributes[$name] = $relation->filterResult(
$relation
->applyRelation([$this->attributes[$relation->primaryKey]], $relation->foreignKey)
->model
->first(),
'object',
);
} else {
$this->attributes[$name] = $relation->filterResults(
$relation->applyRelation([$this->attributes[$relation->primaryKey]], $relation->foreignKey)
->model
->findAll(),
'object',
);
}
return $this->attributes[$name];
}
/**
* Resolve the matching model once for the lifetime of the entity instance.
*/
private function getRelationModel(): ?Model
{
if ($this->relationModelResolved) {
return $this->relationModel;
}
$className = $this->findModelClass();
$this->relationModel = $className === null ? null : model($className);
$this->relationModelResolved = true;
return $this->relationModel;
}
/**
* Search for the proper model.
*
* First search in the same folder in the models.
*/
private function findModelClass(): ?string
{
/** @var FileLocatorInterface $locator */
$locator = service('locator');
$className = static::class;
$namespace = substr($className, strpos($className, 'Entities'));
$baseNamespace = substr($namespace, 0, strrpos($namespace, '\\'));
$baseClassName = substr(strrchr($className, '\\'), 1);
$modelPath = str_replace('Entities', 'Models', $baseNamespace) . '\\' . $baseClassName . 'Model';
$files = $locator->search(str_replace('\\', '/', $modelPath) . '.php');
if ($files === []) {
// Fallback to search in the base Models directory
$modelPath = 'Models\\' . $baseClassName . 'Model';
$files = $locator->search(str_replace('\\', '/', $modelPath) . '.php');
}
if ($files === []) {
return null;
}
return $locator->findQualifiedNameFromPath($files[0]);
}
}