-
Notifications
You must be signed in to change notification settings - Fork 4
/
DefinitionStorage.php
327 lines (269 loc) · 11 KB
/
DefinitionStorage.php
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Doctrine Workflow
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace DoctrineExtensions\Workflow;
use Doctrine\DBAL\Connection;
class DefinitionStorage implements \ezcWorkflowDefinitionStorage
{
/**
* @var Doctrine\DBAL\Connection
*/
private $conn;
/**
* @var WorkflowOptions
*/
private $options;
/**
* @param Connection $conn
* @param WorkflowOptions $options
*/
public function __construct(Connection $conn, WorkflowOptions $options)
{
$this->conn = $conn;
$this->options = $options;
}
/**
* @return WorkflowOptions
*/
public function getOptions()
{
return $this->options;
}
/**
* Load a workflow definition by name.
*
* @param string $workflowName
* @param int $workflowVersion
* @return ezcWorkflow
* @throws ezcWorkflowDefinitionStorageException
*/
public function loadByName( $workflowName, $workflowVersion = 0 )
{
if ($workflowVersion == 0) {
$workflowVersion = $this->getCurrentVersion($workflowName);
}
$sql = 'SELECT workflow_id FROM ' . $this->options->workflowTable() . ' WHERE workflow_name = ? AND workflow_version = ?';
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(1, $workflowName);
$stmt->bindParam(2, $workflowVersion);
$stmt->execute();
$result = $stmt->fetchAll( \PDO::FETCH_ASSOC );
if ( $result !== false && isset( $result[0] ) ) {
$result[0] = array_change_key_case($result[0], \CASE_LOWER);
$workflowId = $result[0]['workflow_id'];
} else {
throw new \ezcWorkflowDefinitionStorageException('Could not load workflow definition.');
}
return $this->loadWorkflow($workflowId, $workflowName, $workflowVersion);
}
/**
* Load a workflow definition by ID.
*
* Providing the name of the workflow that is to be loaded as the
* optional second parameter saves a database query.
*
* @param int $workflowId
* @param string $workflowName
* @param int $workflowVersion
* @return ezcWorkflow
* @throws ezcWorkflowDefinitionStorageException
* @throws ezcDbException
*/
public function loadById( $workflowId )
{
$platform = $this->conn->getDatabasePlatform();
$sql = "SELECT workflow_name, workflow_version FROM " . $this->options->workflowTable() . " WHERE workflow_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(1, $workflowId);
$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
if (count($result) == 1) {
$result[0] = array_change_key_case($result[0], \CASE_LOWER);
$workflowName = $result[0]['workflow_name'];
$workflowVersion = $result[0]['workflow_version'];
} else {
throw new \ezcWorkflowDefinitionStorageException('Could not load workflow definition.');
}
return $this->loadWorkflow($workflowId, $workflowName, $workflowVersion);
}
/**
*
* @param int $workflowId
* @param string $workflowName
* @param int $workflowVersion
* @return \ezcWorkflow
*/
protected function loadWorkflow($workflowId, $workflowName, $workflowVersion)
{
$sql = "SELECT node_id, node_class, node_configuration FROM " . $this->options->nodeTable() . " WHERE workflow_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(1, $workflowId);
$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
// Create node objects.
foreach ( $result as $node ) {
$node = array_change_key_case($node, \CASE_LOWER);
$configuration = $this->options->getSerializer()->unserialize($node['node_configuration'], null);
if ( is_null( $configuration ) ) {
$configuration = \ezcWorkflowUtil::getDefaultConfiguration( $node['node_class'] );
}
$nodes[$node['node_id']] = $this->options->getNodeFactory()->createNode($node['node_class'], $configuration);
if ($nodes[$node['node_id']] instanceof \ezcWorkflowNodeFinally &&
!isset( $finallyNode ) ) {
$finallyNode = $nodes[$node['node_id']];
}
else if ($nodes[$node['node_id']] instanceof \ezcWorkflowNodeEnd &&
!isset( $defaultEndNode ) ) {
$defaultEndNode = $nodes[$node['node_id']];
}
else if ($nodes[$node['node_id']] instanceof \ezcWorkflowNodeStart &&
!isset( $startNode ) ) {
$startNode = $nodes[$node['node_id']];
}
}
if ( !isset( $startNode ) || !isset( $defaultEndNode ) ) {
throw new \ezcWorkflowDefinitionStorageException(
'Could not load workflow definition.'
);
}
$sql = "SELECT nc.outgoing_node_id, nc.incoming_node_id FROM " . $this->options->nodeConnectionTable() ." nc ".
"INNER JOIN " . $this->options->nodeTable() . " n ON n.node_id = nc.incoming_node_id WHERE n.workflow_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(1, $workflowId);
$stmt->execute();
$connections = $stmt->fetchAll( \PDO::FETCH_ASSOC );
foreach ( $connections as $connection ) {
$connection = array_change_key_case($connection, \CASE_LOWER);
$nodes[$connection['incoming_node_id']]->addOutNode($nodes[$connection['outgoing_node_id']]);
}
if ( !isset( $finallyNode ) || count( $finallyNode->getInNodes() ) > 0 ) {
$finallyNode = null;
}
// Create workflow object and add the node objects to it.
$workflowClassName = $this->options->workflowClassName();
$workflow = new $workflowClassName( $workflowName, $startNode, $defaultEndNode, $finallyNode );
$workflow->definitionStorage = $this;
$workflow->id = (int)$workflowId;
$workflow->version = (int)$workflowVersion;
$sql = "SELECT variable, class FROM " . $this->options->variableHandlerTable() . " WHERE workflow_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(1, $workflowId);
$stmt->execute();
$result = $stmt->fetchAll( \PDO::FETCH_ASSOC );
$nodes = array();
if ( $result !== false )
{
foreach ( $result as $variableHandler )
{
$workflow->addVariableHandler(
$variableHandler['variable'],
$variableHandler['class']
);
}
}
// Verify the loaded workflow.
$workflow->verify();
return $workflow;
}
/**
* Save a workflow definition to the database.
*
* @param ezcWorkflow $workflow
* @throws ezcWorkflowDefinitionStorageException
*/
public function save( \ezcWorkflow $workflow )
{
// Verify the workflow.
$workflow->verify();
if (strlen($workflow->name) == 0) {
throw new \ezcWorkflowDefinitionStorageException();
}
$platform = $this->conn->getDatabasePlatform();
try {
$this->conn->beginTransaction();
$workflowVersion = $this->getCurrentVersion($workflow->name) + 1;
$this->conn->update(
$this->options->workflowTable(),
array('workflow_outdated' => 1),
array('workflow_name' => $workflow->name)
);
$date = new \DateTime("now");
$this->conn->insert($this->options->workflowTable(), array(
'workflow_name' => $workflow->name,
'workflow_version' => $workflowVersion,
'workflow_created' => $date->format($platform->getDateTimeFormatString()),
'workflow_outdated' => 0,
));
$workflow->id = (int)$this->conn->lastInsertId();
$workflow->definitionStorage = $this;
// Write node table rows.
$nodeMap = array();
foreach ( $workflow->nodes as $node ) {
/* @var $node \ezcWorkflowNode */
$this->conn->insert($this->options->nodeTable(), array(
'workflow_id' => (int)$workflow->id,
'node_class' => get_class($node),
'node_configuration' => $this->options->getSerializer()->serialize( $node->getConfiguration() ),
));
$nodeId = $this->conn->lastInsertId();
$nodeMap[$nodeId] = $node;
}
foreach ($workflow->nodes AS $node) {
foreach ( $node->getOutNodes() as $outNode ) {
$incomingNodeId = null;
$outgoingNodeId = null;
foreach ( $nodeMap as $_id => $_node ) {
if ( $_node === $node ) {
$incomingNodeId = $_id;
}
else if ( $_node === $outNode ) {
$outgoingNodeId = $_id;
}
if ( $incomingNodeId !== NULL && $outgoingNodeId !== NULL ) {
break;
}
}
$this->conn->insert($this->options->nodeConnectionTable(), array(
'incoming_node_id' => $incomingNodeId,
'outgoing_node_id' => $outgoingNodeId,
));
}
}
unset($nodeMap);
foreach ($workflow->getVariableHandlers() AS $variable => $class) {
$this->conn->insert($this->options->variableHandlerTable(), array(
'workflow_id' => (int)$workflow->id,
'variable' => $variable,
'class' => $class,
));
}
$this->conn->commit();
} catch(\Exception $e) {
$this->conn->rollBack();
throw new \ezcWorkflowDefinitionStorageException("Error while persistint workflow: " . $e->getMessage());
}
}
protected function getCurrentVersion($name)
{
$sql = "SELECT MAX(workflow_version) AS version FROM " . $this->options->workflowTable() . " WHERE workflow_name = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(1, $name);
$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
if ( $result !== false && isset( $result[0]['version'] ) && $result[0]['version'] !== null ) {
$result[0] = array_change_key_case($result[0], \CASE_LOWER);
return $result[0]['version'];
} else {
return 0;
}
}
}