|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Action\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Action\ActionInterface; |
|
6
|
|
|
use LAG\AdminBundle\Action\Event\ActionCreatedEvent; |
|
7
|
|
|
use LAG\AdminBundle\Action\Event\ActionEvents; |
|
8
|
|
|
use LAG\AdminBundle\Action\Event\BeforeConfigurationEvent; |
|
9
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
10
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
|
11
|
|
|
use LAG\AdminBundle\LAGAdminBundle; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This class allow to inject an Admin into a Controller. |
|
17
|
|
|
*/ |
|
18
|
|
|
class ActionFactory |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ConfigurationFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $configurationFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var EventDispatcherInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $eventDispatcher; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ActionFactory constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param ConfigurationFactory $configurationFactory |
|
34
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function __construct( |
|
37
|
|
|
ConfigurationFactory $configurationFactory, |
|
38
|
|
|
EventDispatcherInterface $eventDispatcher |
|
39
|
|
|
) { |
|
40
|
5 |
|
$this->configurationFactory = $configurationFactory; |
|
41
|
5 |
|
$this->eventDispatcher = $eventDispatcher; |
|
42
|
5 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Inject an ActionConfiguration into an Action controller. |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed $controller |
|
48
|
|
|
* @param Request $request |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function injectConfiguration($controller, Request $request) |
|
51
|
|
|
{ |
|
52
|
4 |
|
if (!$controller instanceof ActionInterface) { |
|
53
|
1 |
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
if (!$controller->getAdmin() instanceof AdminInterface) { |
|
57
|
1 |
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
// retrieve actions configuration |
|
60
|
|
|
$actionsConfiguration = $controller |
|
61
|
2 |
|
->getAdmin() |
|
62
|
2 |
|
->getConfiguration() |
|
63
|
2 |
|
->getParameter('actions') |
|
64
|
2 |
|
; |
|
65
|
2 |
|
$actionName = $request->get('_route_params')[LAGAdminBundle::REQUEST_PARAMETER_ACTION]; |
|
66
|
|
|
|
|
67
|
|
|
// if the current action name is not supported, we do nothing |
|
68
|
2 |
|
if (!array_key_exists($actionName, $actionsConfiguration)) { |
|
69
|
1 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
// BeforeConfigurationEvent allows users to override action configuration |
|
72
|
1 |
|
$event = new BeforeConfigurationEvent($actionName, $actionsConfiguration[$actionName], $controller->getAdmin()); |
|
73
|
1 |
|
$this |
|
74
|
|
|
->eventDispatcher |
|
75
|
1 |
|
->dispatch(ActionEvents::BEFORE_CONFIGURATION, $event) |
|
76
|
|
|
; |
|
77
|
|
|
|
|
78
|
|
|
// retrieve the current Action configuration |
|
79
|
1 |
|
$configuration = $this |
|
80
|
|
|
->configurationFactory |
|
81
|
1 |
|
->createActionConfiguration($actionName, $controller->getAdmin(), $event->getActionConfiguration()); |
|
82
|
|
|
|
|
83
|
|
|
// allow users to listen after action creation |
|
84
|
1 |
|
$event = new ActionCreatedEvent($controller, $controller->getAdmin()); |
|
85
|
1 |
|
$this |
|
86
|
|
|
->eventDispatcher |
|
87
|
1 |
|
->dispatch( |
|
88
|
1 |
|
ActionEvents::ACTION_CREATED, $event); |
|
89
|
|
|
|
|
90
|
|
|
// inject the Action to the controller |
|
91
|
1 |
|
$controller->setConfiguration($configuration); |
|
92
|
1 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|