1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pim\Bundle\CustomEntityBundle\Action; |
4
|
|
|
|
5
|
|
|
use Pim\Bundle\CustomEntityBundle\Configuration\ConfigurationInterface; |
6
|
|
|
use Pim\Bundle\CustomEntityBundle\Event\ActionEventManager; |
7
|
|
|
use Pim\Bundle\CustomEntityBundle\Manager\Registry as ManagerRegistry; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
use Symfony\Component\Routing\Router; |
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
13
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Antoine Guigan <[email protected]> |
17
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
18
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractAction implements ActionInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ActionFactory |
24
|
|
|
*/ |
25
|
|
|
protected $actionFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ActionEventManager |
29
|
|
|
*/ |
30
|
|
|
protected $eventManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ManagerRegistry |
34
|
|
|
*/ |
35
|
|
|
protected $managerRegistry; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RouterInterface |
39
|
|
|
*/ |
40
|
|
|
protected $router; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var TranslatorInterface |
44
|
|
|
*/ |
45
|
|
|
protected $translator; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ConfigurationInterface |
49
|
|
|
*/ |
50
|
|
|
protected $configuration; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $options; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ActionFactory $actionFactory |
59
|
|
|
* @param ActionEventManager $eventManager |
60
|
|
|
* @param ManagerRegistry $managerRegistry |
61
|
|
|
* @param RouterInterface $router |
62
|
|
|
* @param TranslatorInterface $translator |
63
|
|
|
*/ |
64
|
|
|
public function __construct( |
65
|
|
|
ActionFactory $actionFactory, |
66
|
|
|
ActionEventManager $eventManager, |
67
|
|
|
ManagerRegistry $managerRegistry, |
68
|
|
|
RouterInterface $router, |
69
|
|
|
TranslatorInterface $translator |
70
|
|
|
) { |
71
|
|
|
$this->actionFactory = $actionFactory; |
72
|
|
|
$this->eventManager = $eventManager; |
73
|
|
|
$this->managerRegistry = $managerRegistry; |
74
|
|
|
$this->router = $router; |
75
|
|
|
$this->translator = $translator; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function setConfiguration(ConfigurationInterface $configuration) |
82
|
|
|
{ |
83
|
|
|
$this->configuration = $configuration; |
84
|
|
|
$resolver = new OptionsResolver(); |
85
|
|
|
$this->setDefaultOptions($resolver); |
86
|
|
|
$this->eventManager->dipatchConfigureEvent($this, $resolver); |
87
|
|
|
$this->options = $resolver->resolve($configuration->getActionOptions($this->getType())); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getConfiguration() |
94
|
|
|
{ |
95
|
|
|
return $this->configuration; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function getRoute() |
102
|
|
|
{ |
103
|
|
|
return $this->options['route']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function getRouteParameters($object = null) |
110
|
|
|
{ |
111
|
|
|
$parameters = ['customEntityName' => $this->configuration->getName()]; |
112
|
|
|
if ($object && $object->getId()) { |
113
|
|
|
$parameters['id'] = $object->getId(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $parameters; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function getOptions() |
123
|
|
|
{ |
124
|
|
|
return $this->options; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $optionKey |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
* @throws \LogicException |
132
|
|
|
*/ |
133
|
|
|
protected function getOption($optionKey) |
134
|
|
|
{ |
135
|
|
|
if (isset($this->options[$optionKey])) { |
136
|
|
|
return $this->options[$optionKey]; |
137
|
|
|
} else { |
138
|
|
|
throw new \LogicException( |
139
|
|
|
sprintf('Option "%s" is not defined', $optionKey) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function execute(Request $request) |
148
|
|
|
{ |
149
|
|
|
$this->eventManager->dispatchPreExecuteEvent($this); |
150
|
|
|
$response = $this->doExecute($request); |
151
|
|
|
|
152
|
|
|
return $this->eventManager->dispatchPostExecuteEvent($this, $response); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set the default options |
157
|
|
|
* |
158
|
|
|
* @param OptionsResolver $resolver |
159
|
|
|
*/ |
160
|
|
|
protected function setDefaultOptions(OptionsResolver $resolver) |
161
|
|
|
{ |
162
|
|
|
$resolver->setRequired(['route']); |
163
|
|
|
$resolver->setDefaults(['find_options' => []]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the url for a specified action |
168
|
|
|
* |
169
|
|
|
* @param string $actionType |
170
|
|
|
* @param object $object |
171
|
|
|
* @param array $parameters |
172
|
|
|
* @param mixed $referenceType |
173
|
|
|
*/ |
174
|
|
|
protected function getActionUrl( |
175
|
|
|
$actionType, |
176
|
|
|
$object = null, |
177
|
|
|
$parameters = [], |
178
|
|
|
$referenceType = Router::ABSOLUTE_PATH |
179
|
|
|
) { |
180
|
|
|
$action = ($actionType === $this->getType()) |
181
|
|
|
? $this |
182
|
|
|
: $this->actionFactory->getAction($this->configuration->getName(), $actionType); |
183
|
|
|
|
184
|
|
|
return $this->router->generate( |
185
|
|
|
$action->getRoute(), |
186
|
|
|
$parameters + $action->getRouteParameters($object), |
187
|
|
|
$referenceType |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns the entity of the request |
193
|
|
|
* |
194
|
|
|
* @param Request $request |
195
|
|
|
* |
196
|
|
|
* @throws NotFoundHttpException |
197
|
|
|
* @return object |
198
|
|
|
*/ |
199
|
|
|
protected function findEntity(Request $request) |
200
|
|
|
{ |
201
|
|
|
$entity = $this->getManager()->find( |
202
|
|
|
$this->configuration->getEntityClass(), |
203
|
|
|
$request->attributes->get('id'), |
204
|
|
|
$this->options['find_options'] |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
if (!$entity) { |
208
|
|
|
throw new NotFoundHttpException(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $entity; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Adds a flash message |
216
|
|
|
* |
217
|
|
|
* @param Request $request |
218
|
|
|
* @param string $type |
219
|
|
|
* @param string $message |
220
|
|
|
* @param array $messageParameters |
221
|
|
|
*/ |
222
|
|
|
protected function addFlash(Request $request, $type, $message, array $messageParameters = []) |
223
|
|
|
{ |
224
|
|
|
$request->getSession()->getFlashBag() |
225
|
|
|
->add($type, $this->translator->trans($message, $messageParameters)); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the custom entity manager |
230
|
|
|
* |
231
|
|
|
* @return \Pim\Bundle\CustomEntityBundle\Manager\ManagerInterface |
232
|
|
|
*/ |
233
|
|
|
protected function getManager() |
234
|
|
|
{ |
235
|
|
|
return $this->managerRegistry->getFromConfiguration($this->configuration); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Executes the action |
240
|
|
|
* |
241
|
|
|
* @param Request $request |
242
|
|
|
* |
243
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
244
|
|
|
*/ |
245
|
|
|
abstract public function doExecute(Request $request); |
246
|
|
|
} |
247
|
|
|
|