forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportManager.php
More file actions
151 lines (138 loc) · 4.09 KB
/
ExportManager.php
File metadata and controls
151 lines (138 loc) · 4.09 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
<?php
namespace ProcessMaker\Managers;
use ProcessMaker\Models\Screen;
use Illuminate\Database\Eloquent\Model;
class ExportManager
{
private $dependencies = [];
/**
* Get the value of dependencies
*/
public function getDependencies()
{
return $this->dependencies;
}
/**
* Set the value of dependencies
*
* @return self
*/
public function setDependencies($dependencies)
{
$this->dependencies = $dependencies;
return $this;
}
public function addDependency(array $dependency)
{
$this->dependencies[] = $dependency;
}
/**
* Get dependencies of a $modelClass type
*
* @param string $modelClass
* @param Model $owner
* @param array $references
*
* @return array
*/
public function getDependenciesOfType($modelClass, $owner, array $references = [])
{
$references = $this->reviewDependenciesOf($owner, $references);
$ids = [];
foreach ($references as $ref) {
list($class, $id) = $ref;
$class === $modelClass ? $ids[] = $id : null;
}
return array_unique($ids);
}
/**
* Review all the dependencies of the $owner
*
* @param Model $owner
* @param array $references
* @param array $reviewed
*
* @return array
*/
private function reviewDependenciesOf(Model $owner, array $references = [], array $reviewed = [])
{
$key = get_class($owner) . ':' . $owner->getKey();
if (in_array($key, $reviewed)) {
return $references;
}
$reviewed[] = $key;
$newReferences = [];
foreach ($this->dependencies as $dependencie) {
if (is_a($owner, $dependencie['owner'])) {
$newReferences = call_user_func($dependencie['referencesToExport'], $owner, $newReferences);
}
}
$newReferences = $this->uniqueDiff($newReferences, $references);
$references = array_merge($references, $newReferences);
// Find recurcively dependencies
foreach ($newReferences as $ref) {
list($class, $id) = $ref;
$nextOwner = $class::find($id);
if ($nextOwner) {
$references = $this->reviewDependenciesOf($nextOwner, $references, $reviewed);
}
}
return $references;
}
/**
* Update references for a given model
*
* @param Model $model
* @param array $newReferences
*
* @return Model
*/
public function updateReferences(array $newReferences)
{
foreach ($newReferences as $class => $model) {
if (is_array($model)) {
foreach ($model as $item) {
$this->updateModelReferences($item, $newReferences);
}
} elseif (is_object($model) && $model instanceof Model) {
$this->updateModelReferences($model, $newReferences);
}
}
}
private function updateModelReferences(Model $model, array $newReferences)
{
foreach ($this->dependencies as $dependencie) {
if (is_a($model, $dependencie['owner']) && isset($dependencie['updateReferences'])) {
call_user_func($dependencie['updateReferences'], $model, $newReferences);
}
}
}
public function addDependencyManager($class)
{
$instance = new $class;
$this->addDependency([
'type' => $instance->type,
'owner' => $instance->owner,
'referencesToExport' => [$instance, 'referencesToExport'],
'updateReferences' => [$instance, 'updateReferences'],
]);
return $this;
}
/**
* Remove duplicated items
*
* @param array $array
*
* @return array
*/
private function uniqueDiff(array $array, array $references)
{
$result = [];
foreach ($array as $item) {
if (!in_array($item, $references) && !in_array($item, $result)) {
$result[] = $item;
}
}
return $result;
}
}