1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pim\Bundle\CustomEntityBundle\Configuration; |
4
|
|
|
|
5
|
|
|
use Pim\Bundle\CustomEntityBundle\Event\ConfigurationEvent; |
6
|
|
|
use Pim\Bundle\CustomEntityBundle\Event\ConfigurationEvents; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Configuration for an ORM custom entity |
12
|
|
|
* |
13
|
|
|
* @author Antoine Guigan <[email protected]> |
14
|
|
|
* @copyright 2013 Akeneo SAS (http://www.akeneo.com) |
15
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
16
|
|
|
*/ |
17
|
|
|
class Configuration implements ConfigurationInterface |
18
|
|
|
{ |
19
|
|
|
/** @var EventDispatcherInterface */ |
20
|
|
|
protected $eventDispatcher; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $entityClass; |
27
|
|
|
|
28
|
|
|
/** @var string[] */ |
29
|
|
|
protected $actions = []; |
30
|
|
|
|
31
|
|
|
/** @var array[] */ |
32
|
|
|
protected $actionOptions = []; |
33
|
|
|
|
34
|
|
|
/** @var array[] */ |
35
|
|
|
protected $options = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string $entityClass |
41
|
|
|
* @param array $options |
42
|
|
|
*/ |
43
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, $name, $entityClass, array $options = []) |
44
|
|
|
{ |
45
|
|
|
$this->eventDispatcher = $eventDispatcher; |
46
|
|
|
$this->name = $name; |
47
|
|
|
$this->entityClass = $entityClass; |
48
|
|
|
$resolver = new OptionsResolver(); |
49
|
|
|
$this->setDefaultOptions($resolver); |
50
|
|
|
$this->options = $resolver->resolve($options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds an action for the current entity |
55
|
|
|
* |
56
|
|
|
* @param string $type |
57
|
|
|
* @param string $action |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
|
|
public function addAction($type, $action, array $options = []) |
61
|
|
|
{ |
62
|
|
|
$this->actions[$type] = $action; |
63
|
|
|
$this->actionOptions[$type] = $options; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getAction($type) |
70
|
|
|
{ |
71
|
|
|
return $this->actions[$type]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getActionOptions($type) |
78
|
|
|
{ |
79
|
|
|
return $this->actionOptions[$type]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getEntityClass() |
86
|
|
|
{ |
87
|
|
|
return $this->entityClass; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getName() |
94
|
|
|
{ |
95
|
|
|
return $this->name; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function hasAction($type) |
102
|
|
|
{ |
103
|
|
|
return isset($this->actions[$type]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function getOptions() |
110
|
|
|
{ |
111
|
|
|
return $this->options; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets the default options |
116
|
|
|
* |
117
|
|
|
* @param OptionsResolver $resolver |
118
|
|
|
*/ |
119
|
|
|
protected function setDefaultOptions(OptionsResolver $resolver) |
120
|
|
|
{ |
121
|
|
|
$resolver->setDefaults(['manager' => 'default']); |
122
|
|
|
$event = new ConfigurationEvent($this, $resolver); |
123
|
|
|
$this->eventDispatcher->dispatch(ConfigurationEvents::CONFIGURE, $event); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|