forked from yokai-php/sonata-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkflowControllerTrait.php
188 lines (168 loc) · 6.23 KB
/
WorkflowControllerTrait.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
<?php
namespace Yokai\SonataWorkflow\Controller;
use Psr\Container\ContainerInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Exception\LockException;
use Sonata\AdminBundle\Exception\ModelManagerException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
use Symfony\Component\Workflow\Exception\LogicException;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Workflow;
/**
* @method void addFlash(string $type, string $message)
* @method NotFoundHttpException createNotFoundException(string $message = 'Not Found', \Exception $previous = null)
* @method string escapeHtml(string $s)
* @method ContainerInterface getContainer
* @method mixed get(string $id)
* @method void handleModelManagerException(\Exception $e)
* @method bool isXmlHttpRequest
* @method Response redirectTo(object $object)
* @method Response renderJson($data, int $status = 200, array $headers = [])
* @method string trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
* @property AdminInterface $admin
*
* @author Yann Eugoné <[email protected]>
*/
trait WorkflowControllerTrait
{
/**
* @var Registry
*/
private $workflowRegistry;
/**
* @param Registry $workflowRegistry
*
* @required Symfony DI autowiring
*/
public function setWorkflowRegistry(Registry $workflowRegistry)
{
$this->workflowRegistry = $workflowRegistry;
}
/**
* @param Request $request
*
* @return Response
*/
public function workflowApplyTransitionAction(Request $request)
{
$id = $request->get($this->admin->getIdParameter());
$existingObject = $this->admin->getObject($id);
if (!$existingObject) {
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}
$this->admin->setSubject($existingObject);
$this->admin->checkAccess('edit', $existingObject);
$objectId = $this->admin->getNormalizedIdentifier($existingObject);
try {
$workflow = $this->getWorkflow($existingObject);
} catch (InvalidArgumentException $exception) {
throw $this->createNotFoundException('Not found', $exception);
}
$transition = $request->get('transition', null);
if ($transition === null) {
throw new BadRequestHttpException('missing transition to apply');
}
if (!$workflow->can($existingObject, $transition)) {
throw new BadRequestHttpException(
sprintf(
'transition %s could not be applied to object %s',
$transition,
$this->admin->toString($existingObject)
)
);
}
$response = $this->preApplyTransition($existingObject, $transition);
if ($response !== null) {
return $response;
}
try {
$workflow->apply($existingObject, $transition);
$existingObject = $this->admin->update($existingObject);
if ($this->isXmlHttpRequest()) {
return $this->renderJson(
[
'result' => 'ok',
'objectId' => $objectId,
'objectName' => $this->escapeHtml($this->admin->toString($existingObject)),
],
200,
[]
);
}
$this->addFlash(
'sonata_flash_success',
$this->trans(
'flash_edit_success',
['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
'SonataAdminBundle'
)
);
} catch (LogicException $e) {
throw new BadRequestHttpException(
sprintf(
'transition %s could not be applied to object %s',
$transition,
$this->admin->toString($existingObject)
), $e
);
} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);
} catch (LockException $e) {
$this->addFlash(
'sonata_flash_error',
$this->trans(
'flash_lock_error',
[
'%name%' => $this->escapeHtml($this->admin->toString($existingObject)),
'%link_start%' => '<a href="' . $this->admin->generateObjectUrl('edit', $existingObject) . '">',
'%link_end%' => '</a>',
],
'SonataAdminBundle'
)
);
}
return $this->redirectTo($existingObject);
}
/**
* @param object $object
*
* @return Workflow
* @throws InvalidArgumentException
*/
protected function getWorkflow($object)
{
$registry = $this->workflowRegistry;
if ($registry === null) {
try {
if (method_exists($this, 'get')) {
$registry = $this->get('workflow.registry.pub');
} else {
$registry = $this->getContainer()->get('workflow.registry.pub');
}
} catch (ServiceNotFoundException $exception) {
throw new \LogicException('Could not find the "workflow.registry" service. '.
'You should either provide it via setter injection in your controller service definition '.
'or make it public in your project.',
0,
$exception
);
}
}
return $registry->get($object);
}
/**
* @param object $object
* @param string $transition
*
* @return null|Response
*/
protected function preApplyTransition($object, $transition)
{
return null;
}
}