|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/old-town/workflow-zf2-toolkit |
|
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace OldTown\Workflow\ZF2\Toolkit\DoctrineWorkflowStory; |
|
7
|
|
|
|
|
8
|
|
|
use Zend\Serializer\AdapterPluginManager as SerializerManager; |
|
9
|
|
|
use OldTown\Workflow\ZF2\Service\Annotation as WFS; |
|
10
|
|
|
use Zend\Serializer\Adapter\AdapterInterface as Serializer; |
|
11
|
|
|
use OldTown\Workflow\ZF2\Toolkit\Entity\DoctrineWorkflowStory\ExtEntry; |
|
12
|
|
|
use OldTown\Workflow\ZF2\Toolkit\Entity\DoctrineWorkflowStory\ObjectInfo; |
|
13
|
|
|
use OldTown\Workflow\ZF2\Toolkit\Options\ModuleOptions; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class DoctrineWorkflowStoryService |
|
19
|
|
|
* |
|
20
|
|
|
* @package OldTown\Workflow\ZF2\Toolkit\DoctrineWorkflowStory |
|
21
|
|
|
*/ |
|
22
|
|
|
class DoctrineWorkflowStoryService |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Псевдоним для объектов по умолчанию |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
const DEFAULT_OBJECT = 'defaultObject'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Менеджер для получения адапторов отвечающих за сериализацию данных |
|
33
|
|
|
* |
|
34
|
|
|
* @var SerializerManager |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $serializerManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Сериалайзер по умолчанию |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $serializerName = 'json'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Настройки модуля |
|
47
|
|
|
* |
|
48
|
|
|
* @var ModuleOptions |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $moduleOptions; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* DoctrineWorkflowStoryService constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $options |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(array $options = []) |
|
58
|
|
|
{ |
|
59
|
|
|
$initOptions = [ |
|
60
|
|
|
array_key_exists('serializerManager', $options) ? $options['serializerManager'] : null, |
|
61
|
|
|
array_key_exists('moduleOptions', $options) ? $options['moduleOptions'] : null |
|
62
|
|
|
]; |
|
63
|
|
|
call_user_func_array([$this, 'init'], $initOptions); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param SerializerManager $serializerManager |
|
68
|
|
|
* @param ModuleOptions $moduleOptions |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function init(SerializerManager $serializerManager, ModuleOptions $moduleOptions) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->setSerializerManager($serializerManager); |
|
73
|
|
|
$this->setModuleOptions($moduleOptions); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @WFS\ArgumentsMap(argumentsMap={ |
|
78
|
|
|
* @WFS\Map(fromArgName="entryParam", to="entry"), |
|
79
|
|
|
* @WFS\Map(fromArgName="objectParam", to="object"), |
|
80
|
|
|
* @WFS\Map(fromArgName="objectAliasParam", to="objectAlias"), |
|
81
|
|
|
* @WFS\Map(fromArgName="storeParam", to="store") |
|
82
|
|
|
*}) |
|
83
|
|
|
* |
|
84
|
|
|
* |
|
85
|
|
|
* @param ExtEntry $entry |
|
86
|
|
|
* @param mixed $object |
|
87
|
|
|
* @param string $objectAlias |
|
88
|
|
|
* @param DoctrineWorkflowStory $store |
|
89
|
|
|
* |
|
90
|
|
|
* @throws Exception\InvalidWorkflowStoreException |
|
91
|
|
|
* @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException |
|
92
|
|
|
* @throws Exception\RuntimeException |
|
93
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
94
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException |
|
95
|
|
|
* @throws \Zend\ServiceManager\Exception\RuntimeException |
|
96
|
|
|
* @throws \Zend\Serializer\Exception\ExceptionInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
public function bindObjectToWorkflowEntry(ExtEntry $entry, $object, $objectAlias = self::DEFAULT_OBJECT, DoctrineWorkflowStory $store) |
|
99
|
|
|
{ |
|
100
|
|
|
$em = $store->getEntityManager(); |
|
101
|
|
|
|
|
102
|
|
|
$objectClass = get_class($object); |
|
103
|
|
|
$metadata = $em->getClassMetadata($objectClass); |
|
104
|
|
|
|
|
105
|
|
|
$serializerName = $this->getSerializerName(); |
|
106
|
|
|
/** @var Serializer $serializer */ |
|
107
|
|
|
$serializer = $this->getSerializerManager()->get($serializerName); |
|
108
|
|
|
|
|
109
|
|
|
$id = $metadata->getIdentifierValues($object); |
|
110
|
|
|
$serializedId = $serializer->serialize($id); |
|
111
|
|
|
|
|
112
|
|
|
$objectInfoClass = $this->getModuleOptions()->getEntityClassName('DoctrineWorkflowStory\\ObjectInfo'); |
|
113
|
|
|
|
|
114
|
|
|
$r = new ReflectionClass($objectInfoClass); |
|
115
|
|
|
/** @var ObjectInfo $objectInfo */ |
|
116
|
|
|
$objectInfo = $r->newInstance(); |
|
117
|
|
|
|
|
118
|
|
|
$objectInfo->setClassName($objectClass); |
|
119
|
|
|
$objectInfo->setObjectId($serializedId); |
|
120
|
|
|
$objectInfo->setAlias($objectAlias); |
|
121
|
|
|
$objectInfo->addEntry($entry); |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$entry->addObjectInfo($objectInfo); |
|
125
|
|
|
|
|
126
|
|
|
$em->persist($objectInfo); |
|
127
|
|
|
$em->flush(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Востановить объект привязанный к процессу |
|
132
|
|
|
* |
|
133
|
|
|
* @WFS\ArgumentsMap(argumentsMap={ |
|
134
|
|
|
* @WFS\Map(fromArgName="entryParam", to="entry"), |
|
135
|
|
|
* @WFS\Map(fromArgName="objectAliasParam", to="objectAlias"), |
|
136
|
|
|
* @WFS\Map(fromArgName="storeParam", to="store") |
|
137
|
|
|
* }) |
|
138
|
|
|
* |
|
139
|
|
|
* @WFS\ResultVariable(name="resultVariableName") |
|
140
|
|
|
* |
|
141
|
|
|
* |
|
142
|
|
|
* @param ExtEntry $entry |
|
143
|
|
|
* @param string $objectAlias |
|
144
|
|
|
* @param DoctrineWorkflowStory $store |
|
145
|
|
|
* |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
* |
|
148
|
|
|
* @throws \OldTown\Workflow\Spi\Doctrine\Exception\DoctrineRuntimeException |
|
149
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
150
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException |
|
151
|
|
|
* @throws \Zend\ServiceManager\Exception\RuntimeException |
|
152
|
|
|
* @throws \Zend\Serializer\Exception\ExceptionInterface |
|
153
|
|
|
* @throws Exception\InvalidRestoreObjectException |
|
154
|
|
|
* @throws Exception\InvalidArgumentException |
|
155
|
|
|
*/ |
|
156
|
|
|
public function restoreObjectBindingToEntry(ExtEntry $entry, $objectAlias = self::DEFAULT_OBJECT, DoctrineWorkflowStory $store) |
|
157
|
|
|
{ |
|
158
|
|
|
$objectsInfo = $entry->getObjectsInfo(); |
|
159
|
|
|
|
|
160
|
|
|
foreach ($objectsInfo as $objectInfo) { |
|
161
|
|
|
if ($objectAlias === $objectInfo->getAlias()) { |
|
162
|
|
|
$className = $objectInfo->getClassName(); |
|
163
|
|
|
|
|
164
|
|
|
$em = $store->getEntityManager(); |
|
165
|
|
|
|
|
166
|
|
|
$serializerName = $this->getSerializerName(); |
|
167
|
|
|
/** @var Serializer $serializer */ |
|
168
|
|
|
$serializer = $this->getSerializerManager()->get($serializerName); |
|
169
|
|
|
|
|
170
|
|
|
$serializedId = $objectInfo->getObjectId(); |
|
171
|
|
|
$id = $serializer->unserialize($serializedId); |
|
172
|
|
|
|
|
173
|
|
|
$object = $em->getRepository($className)->find($id); |
|
174
|
|
|
|
|
175
|
|
|
if (!is_object($object)) { |
|
176
|
|
|
$errMsg = sprintf('Invalid restore object. Alias: %s. Class: %s. Id: %s', $objectAlias, $className, $serializedId); |
|
177
|
|
|
throw new Exception\InvalidRestoreObjectException($errMsg); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $object; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$errMsg = sprintf('Invalid object alias: %s', $objectAlias); |
|
185
|
|
|
throw new Exception\InvalidArgumentException($errMsg); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return SerializerManager |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getSerializerManager() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->serializerManager; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param SerializerManager $serializerManager |
|
198
|
|
|
* |
|
199
|
|
|
* @return $this |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setSerializerManager(SerializerManager $serializerManager) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->serializerManager = $serializerManager; |
|
204
|
|
|
|
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getSerializerName() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->serializerName; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param string $serializerName |
|
218
|
|
|
* |
|
219
|
|
|
* @return $this |
|
220
|
|
|
*/ |
|
221
|
|
|
public function setSerializerName($serializerName) |
|
222
|
|
|
{ |
|
223
|
|
|
$this->serializerName = $serializerName; |
|
224
|
|
|
|
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return ModuleOptions |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getModuleOptions() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->moduleOptions; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param ModuleOptions $moduleOptions |
|
238
|
|
|
* |
|
239
|
|
|
* @return $this |
|
240
|
|
|
*/ |
|
241
|
|
|
public function setModuleOptions(ModuleOptions $moduleOptions) |
|
242
|
|
|
{ |
|
243
|
|
|
$this->moduleOptions = $moduleOptions; |
|
244
|
|
|
|
|
245
|
|
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|