-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupervisor.php
More file actions
185 lines (152 loc) · 4.62 KB
/
Supervisor.php
File metadata and controls
185 lines (152 loc) · 4.62 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace SyncEngine\Model\Trait;
use SyncEngine\Exception\InvalidParameterException;
use SyncEngine\Model\Abstract\AbstractModel;
use SyncEngine\Model\Abstract\EntityModel;
use SyncEngine\Model\Abstract\ServiceModel;
use SyncEngine\Model\BlueprintModel;
use SyncEngine\Model\Interface\Persistable;
use SyncEngine\Model\ModuleModel;
trait Supervisor
{
use Config {
initConfig as _initConfig;
getConfigDependencies as _getConfigDependencies;
}
protected ?AbstractModel $supervisor;
protected function initConfig(): void
{
$this->_initConfig();
$this->initSupervisor();
}
public function getConfigDependencies( array|bool $recursive = false ): array
{
$supervisor = $this->getSupervisor();
if ( $supervisor ) {
return $supervisor->getConfigDependencies( $recursive );
}
return $this->_getConfigDependencies( $recursive );
}
public function getIcon(): string
{
$icon = '';
if ( is_callable( $this, 'getIcon' ) ) {
$icon = $this->getIcon();
}
if ( ! $icon && $this instanceof Persistable && is_callable( [ $this->getEntity(), 'getIcon' ] ) ) {
$icon = $this->getEntity()->getIcon();
}
if ( ! $icon && is_callable( [ $this->getSupervisor(), 'getIcon' ] ) ) {
$icon = $this->getSupervisor()->getIcon();
}
return $icon ?? '';
}
public function getSupervisor(): ?AbstractModel
{
if ( ! isset( $this->supervisor ) ) {
$this->supervisor = null;
if ( ! $this instanceof Persistable || ! is_callable( [ $this->getEntity(), 'getSupervisor' ] ) ) {
return $this->supervisor;
}
$supervisor = $this->getEntity()->getSupervisor();
if ( $supervisor ) {
$this->setSupervisor( $supervisor );
}
}
return $this->supervisor;
}
public function exportSupervisor(): ?string
{
if ( $this instanceof Persistable ) {
// Return raw database value.
return $this->getEntity()->getSupervisor();
}
$supervisor = $this->getSupervisor();
if ( $supervisor instanceof ServiceModel ) {
return $supervisor->getClassLocator();
}
return $supervisor ? AbstractModel::getModelName( $supervisor::class ) : null;
}
public function setSupervisor( AbstractModel|string|null $model ): void
{
if ( is_string( $model ) ) {
$param = $model;
$parts = explode( ':', $model );
$model = array_shift( $parts );
$name = implode( ':', $parts );
$modelClass = AbstractModel::getModelClass( $model );
if ( $modelClass && method_exists( $modelClass, 'get' ) ) {
$model = $modelClass::get( $name );
} else {
throw new InvalidParameterException( 'Supervisor type does not exist: ' . $param );
}
}
if ( ! $model ) {
// @todo Properly handle errors on interface.
throw new InvalidParameterException( 'Supervisor not found: ' . $model );
}
if ( ! $this->supportsSupervisor( $model ) ) {
// @todo Properly handle errors on interface.
throw new InvalidParameterException( 'Supervisor not allowed: ' . $model );
}
$this->supervisor = $model;
if ( method_exists( $this->supervisor, 'setSupervisable' ) ) {
$this->supervisor->setSupervisable( $this );
}
if ( $this instanceof Persistable && is_callable( [ $this->getEntity(), 'setSupervisor' ] ) ) {
$supervisor = $model::getModelName();
if ( $model instanceof ServiceModel ) {
$supervisor .= ':' . $model->getClassLocator();
} elseif ( $model instanceof EntityModel ) {
$supervisor .= ':' . $model->getId();
} else {
$supervisor .= ':' . $model->getRef();
}
$this->getEntity()->setSupervisor( $supervisor );
}
}
public function initSupervisor(): void
{
$supervisor = $this->getSupervisor();
if ( $supervisor instanceof BlueprintModel ) {
$supervisor->setSupervisableConfig();
}
}
public function runSupervisorValidate(): bool
{
if ( $this->supportsSupervisor( BlueprintModel::class ) ) {
$blueprint = $this->getConfig( '_blueprint._class' );
if ( $blueprint ) {
$this->setSupervisor( BlueprintModel::get( $blueprint ) );
}
}
$supervisor = $this->getSupervisor();
if ( $supervisor instanceof BlueprintModel ) {
$supervisor->update( true );
}
return true;
}
public function supportsSupervisor( string|AbstractModel $type ): bool
{
$supported = $this->getSupportedSupervisors();
if ( is_object( $type ) ) {
foreach ( $supported as $supervisor ) {
if ( is_a( $type, $supervisor ) ) {
return true;
}
}
return false;
}
if ( str_ends_with( $type, 'Model' ) ) {
return in_array( $type, $supported );
}
return array_key_exists( strtolower( $type ), $supported );
}
public function getSupportedSupervisors(): array
{
return [
'module' => ModuleModel::class,
'blueprint' => BlueprintModel::class,
];
}
}