forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportProcess.php
More file actions
278 lines (245 loc) · 7.57 KB
/
ExportProcess.php
File metadata and controls
278 lines (245 loc) · 7.57 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
namespace ProcessMaker\Jobs;
use Cache;
use Illuminate\Bus\Queueable;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\Script;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use ProcessMaker\Managers\ExportManager;
use ProcessMaker\Providers\WorkflowServiceProvider;
class ExportProcess implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The process we will export.
*
* @var object
*/
private $process;
/**
* The BPMN definitions of the process.
*
* @var object
*/
private $definitions;
/**
* The path where our final file should be stored, if any.
*
* @var string|null
*/
protected $filePath;
/**
* The final contents of our file.
*
* @var string
*/
private $fileContents;
/**
* The array which will contain all of our packaged objects in array form.
*
* @var array[]
*/
protected $package = [];
/**
* @var ExportManager
*/
public $manager;
/**
* Create a new job instance, set the process, get BPMN definitions, and
* set the file path.
*
* @return void
*/
public function __construct(Process $process, $filePath = null)
{
$this->process = $process;
$this->definitions = $this->process->getDefinitions();
$this->filePath = $filePath;
}
/**
* Parse the BPMN, looking for any task assignments to users and/or groups,
* then remove them along with any referenced users or groups.
*
* @return void
*/
private function removeAssignedEntities()
{
$humanTasks = ['startEvent', 'task', 'userTask', 'manualTask'];
foreach ($humanTasks as $humanTask) {
$tasks = $this->definitions->getElementsByTagName($humanTask);
foreach ($tasks as $task) {
$task->removeAttributeNS(WorkflowServiceProvider::PROCESS_MAKER_NS, 'assignment');
$task->removeAttributeNS(WorkflowServiceProvider::PROCESS_MAKER_NS, 'assignedUsers');
$task->removeAttributeNS(WorkflowServiceProvider::PROCESS_MAKER_NS, 'assignedGroups');
}
}
//remove assignments to call Activity
$callActivity = $this->definitions->getElementsByTagName('callActivity');
foreach ($callActivity as $task) {
$task->removeAttribute('calledElement');
}
$this->process->bpmn = $this->definitions->saveXML();
}
/**
* Package the process itself. Note that we must save BPMN separately
* since it is hidden from our toArray method.
*
* @return void
*/
private function packageProcess()
{
$this->package['process'] = $this->process->append('notifications', 'task_notifications')->toArray();
$this->package['process']['bpmn'] = $this->process->bpmn;
}
/**
* Package the process category associated with our process.
*
* @return void
*/
private function packageProcessCategory()
{
$this->package['process_categories'] = $this->process->categories->toArray();
}
/**
* Package any screens referred to in our BPMN.
*
* @return void
*/
public function packageScreens()
{
$this->package['screens'] = [];
$this->package['screen_categories'] = [];
if (! isset($this->screen)) {
$screenIds = $this->manager->getDependenciesOfType(Screen::class, $this->process, []);
} else {
$screenIds = array_merge([$this->screen->id], $this->manager->getDependenciesOfType(Screen::class, $this->screen, []));
}
if (count($screenIds)) {
$screens = Screen::whereIn('id', $screenIds)->get();
$screens->each(function ($screen) {
$this->packageScreen($screen);
});
}
}
private function packageScreen(Screen $screen)
{
$screenArray = $screen->toArray();
$screenArray['categories'] = $screen->categories->toArray();
$this->package['screens'][] = $screenArray;
}
/**
* Package any scripts referred to in our BPMN.
*
* @return void
*/
private function packageScripts()
{
$this->package['scripts'] = [];
$scriptIds = $this->manager->getDependenciesOfType(Script::class, $this->process, []);
if (count($scriptIds)) {
$scripts = Script::whereIn('id', $scriptIds)->get();
$scripts->each(function ($script) {
$scriptArray = $script->toArray();
$scriptArray['categories'] = $script->categories->toArray();
$this->package['scripts'][] = $scriptArray;
});
}
}
/**
* Package the metadata (NOT THE VALUE) of any environment variables
* referred to in our scripts.
*
* @return void
*/
private function packageEnvironmentVariables()
{
$this->package['environment_variables'] = [];
$environmentVariables = EnvironmentVariable::get();
foreach ($environmentVariables as $environmentVariable) {
foreach ($this->package['scripts'] as $script) {
$position = strpos($script['code'], $environmentVariable->name);
if ($position !== false) {
$this->package['environment_variables'][] = $environmentVariable->toArray();
}
}
}
}
/**
* Run through each step of the packaging process. We specify a file type
* and a file version in case of future changes to the file format.
*
* @return void
*/
private function packageFile()
{
$this->package['type'] = 'process_package';
$this->package['version'] = '1';
$this->removeAssignedEntities();
$this->packageProcess();
$this->packageProcessCategory();
$this->packageScreens();
$this->packageScripts();
$this->packageEnvironmentVariables();
}
/**
* Encode the file to JSON and base64.
*
* @return void
*/
protected function encodeFile()
{
$this->fileContents = json_encode($this->package);
}
/**
* Save the file to the specified path, then return true on success or
* false upon failure.
*
* @return boolean
*/
protected function saveFile()
{
$bytes = file_put_contents($this->filePath, $this->fileContents);
if ($bytes !== false) {
return true;
}
}
/**
* Save the file to the cache, then return the cache key.
*
* @return string
*/
protected function cacheFile()
{
$key = sha1($this->fileContents);
$value = $this->fileContents;
$expiresAt = now()->addHours(1);
Cache::put($key, $value, $expiresAt);
return $key;
}
/**
* Execute the job.
*
* @return boolean|string
*/
public function handle()
{
$this->manager = app(ExportManager::class);
// Package up our process
$this->packageFile();
// Encode the file
$this->encodeFile();
// If a specific file path is specified,
// export to it and return true. Otherwise,
// save to our cache and return the saved key.
if ($this->filePath) {
return $this->saveFile();
} else {
return $this->cacheFile();
}
}
}